Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

WebKit 6 min read How-to mediumAdept

Easier layout with margin-trim

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.

Related posts

  • Release Notes for Safari Technology Preview 243

    WebKit mediumAdept

    Safari Technology Preview 243 ships WebKit changes 310600@main through 312007@main for macOS Tahoe and Sequoia. The bulk of the release is CSS layout and selector fixes, covering flexbox, grid, aspect-ratio, anchor positioning, `:has()` invalidation and CJK line breaking. It also adds `contain: style` for quote counters, the `insert` keyword for `text-autospace` and the `shadowrootslotassignment` attribute. Other changes include accessibility fixes for VoiceOver, an O(n²) fix for parsing large `<select>` lists, an ES module loader rewrite for top-level await correctness, and several regular expression fixes.

  • WebKit Features in Safari 17.6

    WebKit mediumAdept

    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. Fixes cover media `loadeddata` events, AudioWorklet distortion, PDF previews, scrolling in end-aligned flex containers, Web Extension background pages, Web Inspector and `getDisplayMedia()` in WKWebView.

  • Make creative borders with background-clip border-area

    WebKit mediumAdept

    `background-clip: border-area`, available in Safari Technology Preview and Safari 18.2 beta, clips a background image or gradient to the border region. That lets a single image, linear, conic or repeating gradient fill any border style, width, radius or subset of sides, unlike nine-slice `border-image`. The recipe is a transparent border, a background with `background-origin: border-box` (otherwise the padding-box origin repeats the background under the border), and the new clip value. Multiple background layers let it combine with `background-clip: text`, and feature queries provide progressive-enhancement fallbacks. Pending CSSWG changes will allow `border-area text` in one layer and default the origin to border-box.

  • WebKit Features for Safari 27.2

    WebKit mediumAdept

    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. Grid Lanes renames `flow-tolerance` to `fit-tolerance` (old name kept as a deprecated alias); the property sets how close column heights must be to count as tied, and raising it well above the 1em default keeps item order steadier for keyboard users. The release also ships 20 bug fixes spanning accessibility, media/WebCodecs, cookies under CNAME, WebGPU indirect draws, and WebRTC.

  • WebKit Features in Safari 16.4

    WebKit mediumAdept

    Safari 16.4 ships 135 new web features and over 280 polish updates. The headline additions are Web Push and the Badging API for Home Screen web apps on iOS and iPadOS, Declarative Shadow DOM and ElementInternals for Web Components, and a large batch of CSS features. The CSS work includes margin-trim, lh/rlh units, font-size-adjust, Relative Color Syntax, @property, Media Queries level 4 range syntax and CSS Typed OM. JavaScript gains RegExp lookbehind, and HTML gains lazy-loaded iframes. Most features come with short code examples showing typical usage.

The quest for software engineering knowledge.

Look:
Layout: