CSS Reference ยท Box Model and Sizing
The margin-left property
Reference for the CSS margin-left property: syntax, values, auto margins for alignment, negative indents, examples and browser support.
The margin-left property sets the space to the left of an element's border edge. It indents a box from the left edge of its container or separates it from a preceding item on the same line.
CSS
.nested { margin-left: 2rem; }
Try it
The last box has a fixed width, so margin-left: auto absorbs all the free space and pushes it to the right edge.
Syntax
CSS
margin-left: <length> | <percentage> | auto
- Initial value:
0 - Applies to: all elements except internal table display types
- Inherited: no
- Percentages: relative to the width of the containing block
- Animatable: yes
- Computed value: the percentage as specified or the absolute length;
autostaysauto
Values
| Value | Description |
|---|---|
<length> |
A fixed size such as 2rem. A negative value pulls the box to the left, even outside its container. |
<percentage> |
A share of the containing block's width. |
auto |
Absorbs free space on the left. On a block with a width, it pushes the box to the right. With margin-right: auto too, it centers the box. In a flex row it pushes this item and the ones after it to the end. |
Usage notes
- Pushing flex items apart. In a navigation bar,
margin-left: autoon one item moves it and everything after it to the right edge, with no extra wrapper. - Over-constrained boxes. In left-to-right text, a block whose width and margins do not add up has its right margin adjusted, not the left one. A fixed-width block with
margin-left: 0therefore stays on the left. - Negative values are handy for hanging indents or pulling a box to the container edge, but can create horizontal scrolling if content ends up outside the viewport.
- Right-to-left languages. Use
margin-inline-startso the indent follows the text direction.
Browser support
Supported in every browser.
Related
marginโ the shorthandmargin-right,margin-top,margin-bottompadding-left- Lessons: Margins, Centering in CSS