From what I understand, top:
and left:
really mean "how far the object's top edge is from the top of its container" (container refers to the closest parent element with a relative position) and "how far the object's left edge is from the left of its container". Specifically, top: 50%
means that the object is shifted by 50% of the container's height, and left: 50%
means the object is shifted 50% of the container's width.
Once the origin of the element is at the center, you can see that by shifting the element to the left by half of its width and up by half of its height, the center of the object will be at the origin rather than its upper left corner.
If we did right: 50%
instead, then the right side of the element would be shifted from the right side of the container by 50% of the container's width, meaning that its upper-right edge is on the origin. Therefore, by shifting it to the right by 50% of its width and up by 50% of its height (transform(50%, -50%)
), we will center the object.