CSS Reference ยท Box Model and Sizing
The margin-top property
Reference for the CSS margin-top property: syntax, values, percentages, margin collapsing, negative values, examples and browser support.
The margin-top property sets the space above an element's border edge. The space is transparent and pushes the element away from whatever comes before it, such as the previous sibling or the top of the container.
CSS
.card { margin-top: 2rem; }
Try it
Syntax
CSS
margin-top: <length> | <percentage> | auto
- Initial value:
0 - Applies to: all elements except internal table display types; no effect on non-replaced inline elements
- 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 20px or 1.5rem. Negative values pull the box upward. |
<percentage> |
A share of the containing block's width. |
auto |
Computes to 0 in normal block flow. In flex layouts, an auto top margin pushes the item to the bottom of the free space. |
Usage notes
- Collapsing. In normal flow, the top margin of a block can merge with the bottom margin of the previous sibling, and with the top margin of its own parent if the parent has no padding or border. See
margin. - Percentages use width.
margin-top: 10%grows with the container's width, not its height. - Inline elements ignore vertical margins. Make them
inline-blockorblockif you need the space. - A consistent rhythm. Many stylesheets use a "lobotomized owl" selector such as
* + * { margin-top: 1rem }or agapon a flex column instead of adding margins to each element. - Logical equivalent. In horizontal writing modes,
margin-block-startmaps tomargin-top.
Browser support
Supported in every browser.
Related
marginโ the shorthandmargin-bottom,margin-left,margin-rightpadding-topโ space inside the border- Lesson: Margins