---
title: "Easier layout with margin-trim"
description: "The CSS `margin-trim` property, set on a container, removes any child or descendant margins that butt up against the container's edges, fixing the common problem of padding plus first/last-child margins creating uneven spacing."
url: "https://swe.quest/articles/easier-layout-with-margin-trim-d1b2ebb964b4437e"
canonical: "https://webkit.org/blog/16854/margin-trim/"
source: "WebKit"
published: "2025-05-01"
difficulty: "medium"
kind: "How-to"
reading_minutes: 6
tags: ["css","frontend","browsers"]
---

# Easier layout with margin-trim

A post on [WebKit](https://swe.quest/sources/webkit.md), published 2025-05-01, 6 min read, How-to. Original: [https://webkit.org/blog/16854/margin-trim/](https://webkit.org/blog/16854/margin-trim/)

## Summary

The CSS \`margin-trim\` property, set on a container, removes any child or descendant margins that butt up against the container's edges, fixing the common problem of padding plus first/last-child margins creating uneven spacing. Unlike the \`:first-child\`/\`:last-child\` hack, it also trims margins on nested descendants (e.g. an image inside a figure) and works regardless of what content is placed inside. It is Safari-only so far, so the recommended approach is to use \`margin-trim\` with a fallback inside \`@supports not (margin-trim: block)\`. Values are logical (\`block\`, \`inline\`, \`block-start\`, etc.), and the CSSWG has approved combining \`block inline\`, which WebKit has implemented.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [CSS](https://swe.quest/tags/css.md) (Web & mobile)
- [Frontend](https://swe.quest/tags/frontend.md) (Web & mobile)
- [Browsers](https://swe.quest/tags/browsers.md) (Web & mobile)

## Prerequisites

- CSS box model and margins ([easy posts on CSS](https://swe.quest/tags/css.md?difficulty=easy))
- CSS logical properties ([easy posts on CSS](https://swe.quest/tags/css.md?difficulty=easy))

## Keywords

margin-trim, Safari, WebKit, CSS feature queries, CSSWG

## Related posts

- [Release Notes for Safari Technology Preview 243](https://swe.quest/articles/release-notes-for-safari-technology-preview-243-b654215c3d2767b1.md): WebKit · 2026-05-07 · Medium · 18 min read · Browsers, CSS, Accessibility, JavaScript
  Safari Technology Preview 243 ships WebKit changes 310600@main through 312007@main for macOS Tahoe and Sequoia.
- [WebKit Features in Safari 17.6](https://swe.quest/articles/webkit-features-in-safari-17-6-89a0520bc89eb9cd.md): WebKit · 2024-07-29 · Medium · 3 min read · CSS, Browsers
  Safari 17.6 adds one feature and seven bug fixes. The new feature is the \`safe\` keyword for Flexbox alignment: \`justify-content: safe center\` start-aligns items that overflow their container, so the start of a word isn't clipped, while still centering items that fit. \`unsafe\` restores the default always-center behavior.
- [Make creative borders with background-clip border-area](https://swe.quest/articles/make-creative-borders-with-background-clip-border-area-cdba7fcc9b39e6d8.md): WebKit · 2024-11-20 · Medium · 10 min read · CSS, Browsers, Frontend
  \`background-clip: border-area\`, available in Safari Technology Preview and Safari 18.2 beta, clips a background image or gradient to the border region.
- [WebKit Features for Safari 27.2](https://swe.quest/articles/webkit-features-for-safari-27-2-f545f4ccec69d87b.md): WebKit · 2026-10-26 · Medium · 5 min read · Browsers, CSS, Frontend, Accessibility
  Safari 27.2 refines Customizable Select with a new \`::field-content\` pseudo-element for styling the displayed selected option without \`\<selectedcontent\>\`, adjusts \`appearance: base-select\` UA styles (flex-based button spacing, inherited \`text-overflow\`, simpler \`::picker-icon\`), and uses \`safe\` alignment so large pickers stay within the viewport.
- [WebKit Features in Safari 16.4](https://swe.quest/articles/webkit-features-in-safari-16-4-4fda9eefaadc5c33.md): WebKit · 2023-03-27 · Medium · 38 min read · Browsers, CSS, Frontend, Mobile
  Safari 16.4 ships 135 new web features and over 280 polish updates.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
