CSS Reference ยท Container Queries and Logical Properties
The margin-block property
Reference for the CSS margin-block shorthand: logical margins along the block axis, one- and two-value syntax, examples and usage notes.
The margin-block shorthand sets the outer space at the start and end of the block axis. In horizontal text that means the top and bottom margins, so margin-block: 1rem 2rem is the logical way to write margin-top: 1rem; margin-bottom: 2rem. In a vertical writing mode, the same declaration puts the space at the sides instead.
It sets two longhand properties: margin-block-start and margin-block-end.
CSS
.card { margin-block: 1.5rem; } .lead { margin-block: 0 2rem; }
Try it
Syntax
CSS
margin-block: <'margin-top'>{1,2};
- Initial value:
0 - Applies to: the same elements as
margin(all elements except internal table elements such as rows) - 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 block-start and block-end margins. |
| Two values | The first is block-start, the second block-end. |
<length> |
A fixed size. Negative values are allowed and pull the box towards its neighbors. |
<percentage> |
A share of the containing block's inline size (its width in horizontal text), even on the block axis. |
auto |
Lets the browser compute the margin. In block layout, auto block margins resolve to 0; in flex and grid items they absorb free space. |
Usage notes
- Margin collapsing still applies. Adjacent block-axis margins of block boxes in normal flow can collapse into the larger of the two, exactly as top and bottom margins do. Flex and grid items never collapse.
- Percentages use the inline size. A
margin-block: 5%is therefore based on width in horizontal text, not height. This catches many people out. - Direction does not matter. Switching between left-to-right and right-to-left text does not change block margins. Only the writing mode (horizontal or vertical) does.
- Use the longhands for one side.
margin-block-startandmargin-block-endset a single side. There is no way to skip one side in the shorthand other than writing0. - Pair with
margin-inline. Together they cover all four sides, likemargin.
Browser support
Supported in all current browsers.
Related
margin-inlineโ the inline-axis counterpartmarginโ the physical shorthandpadding-blockโ logical padding on the same axis- Logical properties
- Lessons: Margins, Writing modes