A card has 24 pixels of padding all round, and still the text inside looks off at the top and bottom. The cause is the margins on paragraphs and headings: on the first and last child they add up with the padding. The usual fix is a pair of rules with :first-child and :last-child that every project writes again. margin-trim handles it in one place. The property sits on the container and sets the children's margins to zero where they meet its edge. Safari has supported it since version 16.4 of 27 March 2023, Chrome and Edge since version 155 in October 2026, Firefox not yet. This guide shows the problem, the values, the difference from :first-child, the route in Tailwind and a fallback for Firefox.
- margin-trim belongs on the container, not on the element with the margin. margin-trim: block sets the top and bottom margins to zero where children meet the edge. The container's own margin stays untouched.
- Chrome 155 only understands none, block, block-start and block-end. The values inline, inline-start and inline-end exist only in Safari so far, and the current syntax on MDN no longer lists them. For projects that means: use the block values only.
- Unlike :first-child, margin-trim also reaches nested elements whose margin meets the edge, such as an image inside a figure. Selectors only reach the direct children.
- Firefox does not support margin-trim yet, so the property is not Baseline. A block with @supports not (margin-trim: block) and the old rules for first and last child covers Firefox.
Why does a card have more space at the top and bottom than at the sides?
Because the margin of the first and last child adds up with the card's padding. If the card has 24 pixels of padding and every paragraph 16 pixels of margin at the top and bottom, the text sits 24 pixels from the edge at the sides but 40 at the top and bottom. The eye reads that as off, even if nobody measures.
Without padding and without a border, the first child's margin collapses with the container's and slips out of it. With padding that no longer happens, and the margin stays inside. So exactly when you deliberately give a card room, more room gets added.
The usual answer is two rules that set the first and last child to zero. They sit in almost every project, often more than once, and they only apply to direct children. If the first paragraph is wrapped in another element, its margin stays.
.card { padding: 24px;} .card p { margin-block: 16px;}
How does margin-trim remove the margins at the edge?
With one line on the container: margin-trim: block sets the children's top and bottom margins to zero where they meet the edge. The margins between the children stay, and so does the container's own margin. MDN describes it as trimming the margins at the edge to zero without affecting the margins given to the container itself.
The place matters. Jen Simmons writes on the WebKit blog on 1 May 2025 that you always put margin-trim on the container and never on the element that has the margin. That is the difference from every earlier solution: it is not the child that decides whether it sits at the edge, but the container that decides what happens at its edge.
According to MDN the property applies to block containers and multi-column containers, its initial value is none, and it is not inherited. A nested container therefore needs its own declaration.
.card { padding: 24px; margin-trim: block;} .card p { margin-block: 16px;}
The child does not clear its margin away. The container clears its edge.
Which values does margin-trim accept, and which does Chrome support?
Chrome 155 understands four: none, block, block-start and block-end. Since version 16.4 Safari also understands three values for the inline direction: inline, inline-start and inline-end. In the syntax MDN gives for the current state of the CSS Box Model Level 4 specification, only none, block and a combination of block-start and block-end remain.
block-start and block-end can be combined. margin-trim: block-start block-end acts like block; written on its own, each value only affects its side. That is useful when a container meets a heading at the top whose margin is intended but should end cleanly at the bottom.
- none Nothing is trimmed. The initial value.
- block Top and bottom, so block-start and block-end together. The right value in most cases.
- block-start Only the first child's margin at the top edge, in horizontal writing.
- block-end Only the last child's margin at the bottom edge.
- inline, inline-start, inline-end Margins in the inline direction. Only in Safari so far, not in Chrome and Edge.
What can margin-trim do that :first-child and :last-child cannot?
It reaches nested elements too. A selector like .card > :first-child only reaches the first direct child. margin-trim, by contrast, trims every margin that meets the container's edge, including a grandchild's.
The example from the WebKit blog shows it: a figure with margin-top: 1em, and inside it an image that also has margin-top: 1em. The :first-child rule only sets the figure's margin to zero; the image's stays. margin-trim removes both.
The second difference is maintenance. Rules for the first and last child are written again in every component and have to match the structure. When the structure changes, for instance because a wrapper is added, they stop matching, and nobody notices until the gap shows.
- Two rules per component that have to match the structure.
- Direct children only: an image inside a figure keeps its margin.
- A new wrapper silently makes the rules ineffective.
- One line, no matter which element comes first.
- Every margin that meets the edge, nested ones included.
- The structure inside may change; the edge stays clean.
How do you use margin-trim with Tailwind?
As an arbitrary property: in Tailwind 4 the class [margin-trim:block] produces exactly the line margin-trim: block. There is no dedicated utility, and for most components you do not need one.
Because where you set the spacing yourself, the problem rarely comes up in Tailwind. In Tailwind 4.3, space-y-4 puts a bottom margin on every direct child except the last one; the selector is :where(.space-y-4 > :not(:last-child)). So nothing is left at the edge. In flex and grid layouts gap is the standard, and gap only ever appears between items.
margin-trim pays off where the spacing arrives with the content: formatted text from a CMS, Markdown, embedded descriptions. The Typography plugin does set the top margin of the first and the bottom margin of the last direct child of .prose to zero, but only for direct children. For a card with padding and text from another source, [margin-trim:block] is the shorter answer.
export function NoteCard({ html }: { html: string }) { return ( <article className="rounded-xl bg-white p-6 [margin-trim:block]" dangerouslySetInnerHTML={{ __html: html }} /> );}
Which browsers support margin-trim, and what does the fallback look like?
Safari since version 16.4 of 27 March 2023, Chrome since version 155 of 6 October 2026, Edge since version 155 of 8 October 2026 and Opera since version 139. According to MDN's compatibility data, Firefox does not support margin-trim yet (as of 12 November 2026). That is why the property is not Baseline.
The fallback is small and at its core comes from the WebKit blog: a block with @supports not (margin-trim: block) holds the old rules for the first and last child. Browsers with margin-trim skip the block, Firefox applies it. It matters to ask for block and not inline, because Chrome does not know the inline values and would otherwise get the fallback too.
Without a fallback the result in Firefox is not a bug, just the old state: a little more room at the top and bottom of the card. If you can live with that, write only margin-trim and leave the block out.
.card { padding: 24px; margin-trim: block;} @supports not (margin-trim: block) { .card > :first-child { margin-block-start: 0; } .card > :last-child { margin-block-end: 0; }}
First the one line that carries in three engines. Then two rules for the fourth.






