left
Example
.tooltip {
position: absolute;
left: 50%;
}.tooltip {
position: absolute;
left: 50%;
translate: -50% 0;
}left: 50% moves the left edge to the center; translate shifts it back by half its own width.
Common mistakes
left: 50%puts the element's left edge, not its center, in the middle.leftis ignored becausepositionis still 'static'.
Key points
left: 50%centering also shifts the element back withtranslate: -50%ortransform: translateX(-50%).- Layouts that support right-to-left text use
inset-inline-startinstead ofleft. - Elements using
lefthave apositionother than 'static'.
Interview questions
Does left: 50% center an element?
No. It moves the element's left edge to the middle; add a translate of '-50%' to center it.
What happens if I set both left and right?
On absolute elements with 'auto' width, the element stretches between both; with a fixed width in left-to-right text, left wins.
What is the logical version of left?
inset-inline-start, which maps to right in right-to-left text.