CSS Reference ยท Container Queries and Logical Properties
The margin-inline property
Reference for the CSS margin-inline shorthand: logical margins along the inline axis, centering with auto, direction awareness, examples and notes.
The margin-inline shorthand sets the outer space at the start and end of the inline axis, the direction in which text runs within a line. In English that means the left (start) and right (end) margins. In Arabic or Hebrew the start is the right side, and in vertical writing modes the inline axis is vertical.
It sets two longhands: margin-inline-start and margin-inline-end. Its best-known use is margin-inline: auto, which centers a block with a set width in any language and writing mode.
.container { max-inline-size: 60rem; margin-inline: auto; } .icon { margin-inline-end: 0.5rem; }
Try it
Syntax
margin-inline: <'margin-left'>{1,2};
- Initial value:
0 - Applies to: the same elements as
margin - Inherited: no
- Percentages: relative to the inline size of the containing block
- Animatable: yes (as a length or percentage)
- Computed value: as specified, with lengths made absolute
Values
| Value | Description |
|---|---|
| One value | Applied to both the inline-start and inline-end margins. |
| Two values | The first is inline-start, the second inline-end. |
<length> |
A fixed size. Negative values are allowed. |
<percentage> |
A share of the containing block's inline size. |
auto |
Absorbs available space. With auto on both sides, a block box with a definite inline size is centered. |
Usage notes
- Centering needs a size.
margin-inline: autoonly visibly centers when the box is narrower than its container, for example throughinline-size,max-inline-sizeorwidth. A block with no width already fills the line. - Direction aware. The start side flips with the
dirattribute or the CSSdirectionproperty, so icons and gutters mirror automatically in right-to-left languages. - Writing modes. In a vertical mode the inline axis is vertical, so
margin-inline: autocenters the box vertically instead. - One side only. Use
margin-inline-startormargin-inline-end. In a two-value shorthand, write0to keep a side unchanged. - Margins can be negative. Negative inline margins widen a box beyond its container, a handy trick for full-bleed images inside a narrow column.
Browser support
Supported in all current browsers.
Related
margin-blockโ the block-axis counterpartmarginโ the physical shorthandpadding-inlineโ logical padding on the same axisdirectionโ sets the text direction- Logical properties
- Lessons: Margins, Centering