Skip to content
React & Tailwind

Removing edge margins with margin-trim

Why cards have more room at the top and bottom than at the sides, how margin-trim removes the margins at the edges of a container, which values Chrome and Safari understand, what it does better than :first-child and how it fits with Tailwind.

8 min readBy Amelie RoesmannUpdated: 16 November 2026
CSSSpacingLayoutTailwind

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.

The short version
  • 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.
01

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;}
24 pixels at the sides, 40 at the top and bottom: the paragraph margins are added at the edge.
02

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;}
Now 24 pixels all round. Between the paragraphs the 16 stay.

The child does not clear its margin away. The container clears its edge.

03

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.

  1. none Nothing is trimmed. The initial value.
  2. block Top and bottom, so block-start and block-end together. The right value in most cases.
  3. block-start Only the first child's margin at the top edge, in horizontal writing.
  4. block-end Only the last child's margin at the bottom edge.
  5. inline, inline-start, inline-end Margins in the inline direction. Only in Safari so far, not in Chrome and Edge.
Inline values in one browser onlyIf you write margin-trim: inline, Safari shows the intended result and Chrome none at all, because Chrome drops the whole declaration. For spacing between items in a row, gap is the better route anyway, because it never appears at the edge.
04

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.

:first-child and :last-child
  • 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.
margin-trim on the container
  • 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.
05

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 }}    />  );}
A card with text from the CMS. The edge stays even all round.
gap first, then margin-trimIf you write the children yourself, give them no margin and put flex flex-col gap-4 on the container. Then there is nothing to trim at the edge. margin-trim is for content whose spacing you do not control.
06

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;  }}
Only active when the browser does not know margin-trim.

First the one line that carries in three engines. Then two rules for the fourth.

Share
Written by
Amelie RoesmannCreative direction

We build websites and web apps at Systra Studios in Münster and put the building blocks from that work here. What you read comes from real projects, not from a brochure.

Systra Studios: web design from Münster

A new guide every Monday.

Plus one free section with a preview. One email a week, unsubscribe with one click.

You will get a confirmation email. Unsubscribe with one click. Privacy

Long copy, clean edges.
The journals for it are ready to take.

Blog and journal sections for posts: a list with image strips in the margin, a magazine grid, one large post with rows below. Copy them and settle the spacing inside the cards with gap or margin-trim.

  • One file per component, as source code in your own project.
  • Built with React, TypeScript and Tailwind CSS v4.
  • All three respect reduced motion.
Browse blog sections
FAQ

Frequently asked questions

Didn't find your question? Write to us, we answer ourselves.

Get in touch

margin-trim sets the margins of child elements to zero where they meet the edge of their container. You write the property on the container, not on the children: with margin-trim: block the top margin of the first element and the bottom margin of the last one disappear, while the margins between elements stay. The container's own margin does not change. That way text in a card with padding sits as far from the edge at the top and bottom as at the sides, without rules using :first-child and :last-child. According to MDN, the property applies to block containers and multi-column containers.

Safari since version 16.4 of 27 March 2023, on the iPhone as well. Chrome since version 155, released on 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 the property yet (as of 12 November 2026), so margin-trim is not Baseline. On top of that the values differ: Chrome only understands none, block, block-start and block-end, Safari also inline, inline-start and inline-end. If you want the same result in every browser, use only the block values and add a fallback with @supports for Firefox.

You should not rely on it. MDN lists block containers and multi-column containers as what the property applies to, and according to web.dev Chrome 155 implements margin-trim exactly there. In flexbox and grid there is a better route anyway: gap puts space only between the items and never at the edge, so the children need no margin at all. margin-trim is meant for the case where you do not set the spacing yourself but it arrives with the elements, for instance text from a CMS or from Markdown, where every paragraph and heading brings its own margin.

For elements you write yourself, usually not. In Tailwind 4, 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 no space appears at the edge. Content that brings its own spacing is different, such as formatted text from a CMS. For that the Typography plugin sets 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 everything else you write margin-trim as an arbitrary property: the class [margin-trim:block] produces exactly that one line of CSS.