When you are using absolute
position on an element, you can use top
, right
, bottom
, and left
CSS attributes to position it in relation to the nearest parent which has a defined position (e.g. relative
).
translate
, whether its translateX
, translateY
or just translate
, moves your element from its calculated position in relations to itself. A good example for its usage would be if you wanted to center an absolute positioned element inside its parent.
Lets see an example:
#parent {
position: relative;
width: 200px;
height: 200px;
background: blue;
}
#parent #child {
position: absolute;
left: 50%;
top: 50%;
}
#parent2 {
position: relative;
width: 200px;
height: 200px;
background: green;
}
#parent2 #child2 {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
<div id="parent">
<label id="child">LABEL</label>
</div>
<div id="parent2">
<label id="child2">LABEL2</label>
</div>
As you can see, on child2
I use translate
in order to really center it, since top
and left
places its upper left corner on the center of parent
. By using translate(-50%, -50%)
, I'm moving it half of its height up and left, in order to position it.