Transform: translate(-50%, -50%)
The reason why transform: translate(-50%, -50%)
is required is because you want the center of the element to line up with the center of its parent. In simple terms, it can be boiled down to translateX(-50%) translateY(-50%)
, which means:
- move me leftwards by 50% of my width, along the x-axis, and
- move me upwards by 50% of my height, along the y-axis
This effectively moves the center of the element to its original top left corner. Remember than when you set left: 50%; top 50%
on the element, you are moving its top left corner to the center of its parent (which means it is not visually centered at all). By moving the element back leftwards and upwards by half of its width and height respectively, you sure that its center now aligns with the parent's center, making it visually horizontally + vertically centered.
As a proof-of-concept, see the code snippet below: hover over the parent to cause the child element's "ghost" to reposition itself by means of transform: translate(-50%, -50%)
:
body {
margin: 0;
padding: p;
}
.parent {
background-color: #ccc;
width: 100vw;
height: 100vh;
position: relative;
}
.child {
background-color: rgba(0,0,255,0.5);
width: 50px;
height: 50px;
position: absolute;
top: 50%;
left: 50%;
}
.child::before {
background-color: rgba(255, 0, 0, 0.5);
position: absolute;
top: 0;
left: 0;
width: 50px;
height: 50px;
content: '';
transition: all .5s ease-in-out;
}
body:hover .child::before {
transform: translate(-50%, -50%);
}
<div class="parent">
<div class="child"></div>
</div>
TL;DR version
let's say, there is a .container
and an .item
inside
this code below is positioning .item
relatively to .container
; meaning .item
top left corner is in the center of it's container
.item {
top: 50%;
left: 50%;
}
while below is positioning .item
relatively to it's own width and height; meaning minus 50% of it's width and height
.item {
transform: translate(-50%, -50%);
}
if two code below combined, then the expected center will show up