webdevpuneet.com

CSS text-box-trim Demo — Free Font Leading Trim Comparison

CSS text-box-trim Demo · Visualizers · Plain HTML, CSS & JS · Live preview

CategoryVisualizers

Copy & paste

Source Code

<div class="demo-wrap">
  <p class="demo-note">Every font has invisible padding above and below its glyphs, baked into its metrics &mdash; it's why a heading never sits flush against a border or icon. The new CSS <code>text-box-trim</code> / <code>text-box-edge</code> properties trim that space off directly. Compare the two boxes below; both use the exact same font, size, and border.</p>

  <div class="compare-row">
    <div class="compare-box">
      <span class="compare-label">Default (untrimmed)</span>
      <div class="sample-box default-box">
        <span class="icon-chip" aria-hidden="true">&#9733;</span>
        <h2>Featured</h2>
      </div>
      <p class="compare-caption">Notice the gap above "Featured" and below it &mdash; that's leading from the font's own metrics, not padding you set.</p>
    </div>

    <div class="compare-box">
      <span class="compare-label">text-box-trim: both</span>
      <div class="sample-box trimmed-box">
        <span class="icon-chip" aria-hidden="true">&#9733;</span>
        <h2>Featured</h2>
      </div>
      <p class="compare-caption">The heading's text now sits flush against the box edges, aligned with the icon and border with no manual offset hacks.</p>
    </div>
  </div>

  <div class="fallback-note">
    <strong>Support note:</strong> <code>text-box-trim</code> is very new and not yet supported everywhere. The <code>@supports not</code> fallback below simply leaves the default spacing in place &mdash; nothing breaks, it just isn't trimmed there.
  </div>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Removes reserved font leading
Trims the invisible ascent/descent space baked into every font.
text-box-edge: cap alphabetic
Trims to cap-height on top, the alphabetic baseline on bottom.
Flush icon/border alignment
Headings align with adjacent icons and borders with no manual offsets.
No negative-margin hacks needed
Replaces fragile, font-specific pixel-offset workarounds.
Side-by-side comparison
Identical font/size/border, only the trim property differs.
Honest no-op fallback
Unsupporting browsers simply keep default, still-readable spacing.
Zero JavaScript
A pure CSS property — nothing to feature-detect in script.
Works on any text element
Not limited to headings — apply to any inline or block text box.

About this UI Snippet

CSS text-box-trim Demo — Why Headings Never Quite Touch a Border

Screenshot of the CSS text-box-trim Demo snippet rendered live

Every font reserves invisible vertical space above and below its glyphs as part of its own metrics — room for accents on capital letters, room for descenders on letters like "g" and "y", sized generously enough to cover every character the font ships, even on a line that uses none of them. That reserved space, historically called leading, is why a heading dropped into a tightly-bordered card, next to an icon, or against a horizontal rule never sits perfectly flush no matter how precisely you zero out margin and padding — the gap isn't spacing you added, it's baked into the font itself. text-box-trim is a new CSS property that removes it directly.

What's actually happening in the two boxes

Both boxes in this demo use the identical font, size, weight, and border — the only difference is that .trimmed-box h2 adds text-box-trim: trim-both alongside text-box-edge: cap alphabetic. In the default box, look closely at the gap between the top of the heading's border and the visible top of the "F" in "Featured," and the gap below the baseline before the border — that's the font's reserved ascent/descent space, present in every browser's default text rendering. In the trimmed box, that space is removed from both edges, so the heading's visible glyphs align flush with the icon beside it and the border around it.

How text-box-edge chooses the metric

text-box-trim decides which edges to trim (trim-start, trim-end, or trim-both), while text-box-edge decides which font metric to trim *to* on each edge — this demo uses cap alphabetic, meaning the top trims to the font's cap-height (the top of capital letters) and the bottom trims to the alphabetic baseline, which is usually what you want for a heading sitting in a tightly designed card or aligned next to an icon.

Why this has been a real, persistent design pain point

Before this property existed, designers and developers worked around the reserved leading with hand-tuned negative margins or fixed pixel offsets calculated per font — a fragile hack that breaks the moment the font, its version, or its fallback changes, since different fonts reserve different amounts of space. text-box-trim solves the actual problem at its source instead of compensating for it after the fact, which is why design tools like Figma have had an equivalent "trim" concept in their text engines for years while CSS had no way to express it.

Genuinely new — treat the fallback seriously

This is one of the newest properties in this batch of snippets: support is still rolling out unevenly across engines as of 2026, and it should not be assumed to be universally available yet. The @supports not (text-box-trim: trim-both) block in this snippet is deliberately a no-op — an unsupporting browser simply keeps the small reserved gap around the heading, which is still a completely valid, readable layout, just without the flush alignment. Never let the trimmed state be load-bearing for legibility; treat it purely as a polish layer. Pair this with the CSS reading-flow demo or container query units for more of the newest native CSS layer.

Step by step

How to Use

  1. 1
    Compare the two boxesLook at the gap above and below "Featured" in the left (default) box versus the right (trimmed) box.
  2. 2
    Check icon alignmentIn the trimmed box, the heading's cap-height lines up with the star icon; in the default box it sits slightly lower.
  3. 3
    Inspect text-box-trim: trim-bothApplied only to .trimmed-box h2 — the rest of the layout is identical CSS.
  4. 4
    Check text-box-edge: cap alphabeticTrims the top to cap-height and the bottom to the alphabetic baseline.
  5. 5
    Test in an unsupporting browserThe @supports not fallback is a no-op — the heading just keeps its default spacing.
  6. 6
    Try it on your own headingsApply the same two properties anywhere a heading sits inside a tight border or beside an icon.

Real-world uses

Common Use Cases

Tightly bordered cards and badges
Align heading text flush against borders by trimming the invisible space fonts reserve above and below their glyphs.
Icon and label rows
Match text cap-height to an adjacent icon precisely, using text-box-edge: cap alphabetic instead of guessing pixel offsets.
Design system typography tokens
Standardise flush text alignment across design system components, replacing fragile negative-margin hacks that depend on one specific font.
Pixel-perfect design handoff
Match a Figma trimmed text setting exactly, so developers reproduce the designer's spacing without manual adjustments.
Buttons and modern CSS showcases
Remove excess vertical space from pills with tight padding, or pair with the CSS reading-flow demo and CSS subgrid demo to present modern CSS.
Related: Feature Spotlight Tabs
See the Feature Spotlight Tabs for a related layouts pattern worth pairing with this one.

Got questions?

Frequently Asked Questions

Every font reserves vertical space above and below its glyphs as part of its own metrics — room for accents on capital letters and descenders like "g" or "y" — sized to cover every character the font supports, regardless of what a given line of text actually uses. That reserved space, historically called leading, remains even after you zero out margin and padding, because it lives inside the text's own line-height box, not in spacing you control directly.

text-box-trim decides which edge(s) of the text box to trim — trim-start, trim-end, or trim-both. text-box-edge decides which font metric each trimmed edge should align to, such as cap-height for the top or the alphabetic baseline for the bottom. You typically set both together, as this demo does with trim-both and cap alphabetic.

No — this is one of the newest CSS text properties as of 2026, and support is still rolling out unevenly across browser engines. Treat it as a genuinely experimental property rather than something you can assume is universally available, and always pair it with a fallback.

This snippet's @supports not (text-box-trim: trim-both) block is intentionally a no-op — an unsupporting browser simply keeps the font's default reserved spacing around the heading. That default state is still a completely valid, readable layout; it just isn't flush-aligned the way the trimmed box is, so nothing breaks or looks obviously wrong.

The negative-margin approach approximates the trim by guessing a fixed pixel offset for one specific font, which breaks the moment that font, its version, or a fallback font changes, since different fonts reserve different amounts of leading. text-box-trim solves the problem at the metric level using the font's own cap-height and baseline data, so it stays correct across font changes without hand-tuned numbers.

Design your website