CSS Reference ยท Box Model and Sizing
The margin-trim property
Reference for the CSS margin-trim property, which removes margins of children that touch a container edge. Syntax, values, examples and limited browser support.
The margin-trim property tells a container to cut off the margins of its children where they touch the container's edges. A list of cards that each have margin-bottom: 1rem normally leaves 1rem of extra space under the last card; with margin-trim: block-end on the container that space disappears, while the gaps between the cards stay.
.stack { margin-trim: block-end; }
margin-trim has limited browser support. Safari was the first to ship it, and browsers that do not implement it simply ignore it. Treat it as progressive enhancement and keep a fallback such as :last-child { margin-bottom: 0 }.
Try it
In a browser that supports the property, the second yellow box is 20px shorter at the bottom than the first. Elsewhere both boxes look the same.
Syntax
margin-trim: none | [ block || inline ] | [ block-start || block-end || inline-start || inline-end ]
- Initial value:
none - Applies to: block containers, flex containers and grid containers
- Inherited: no
- Animatable: no (discrete)
- Computed value: as specified
Values
| Value | Description |
|---|---|
none |
No trimming. Margins behave as usual. |
block |
Trims child margins at both the block-start and block-end edges. |
inline |
Trims child margins at both the inline-start and inline-end edges. |
block-start, block-end, inline-start, inline-end |
Trims only the named edge. Keywords can be combined, for example block-start inline-end. |
Usage notes
- Only touching margins are trimmed. Margins between siblings are kept; only the margin that would sit against the container edge is removed.
- The trim is a layout effect, not a style change. The child's computed
marginvalue stays the same. - Alternatives that work everywhere. Use
gapon flex or grid containers, a* + *spacing rule, or:last-child { margin-bottom: 0 }. - Do not depend on it for layouts that must look identical in all browsers until support is wider.
Browser support
Limited: Safari supports margin-trim, but it is not supported across all major browsers, so it is not part of Baseline. Check current support tables before relying on it.
Related
marginโ the shorthandmargin-bottomโ the margin that is usually trimmedpadding- Lesson: Margins