Home
All posts

Stop backgrounds from growing with line-height in flex and grid

⬪4 min read
csstil

When placing an element with a backgorund or border in a flex or grid container you might be surprised why it suddenly looks different.
The background is suddenly affected by the container’s line-height when the element is unsed in a flex or grid context. In inline layout the background only covers the text, but a flex or grid item is as tall as its line box.

The Problem

In in an inline context the box that background, border or outline is based on always hugs the characters, no matter the line-height. In a flex or grid container, the box grows gets as tall as the line.

Elements with a visible box around text like tags, pills, badges, buttons should usually not be affected by the line-height of their container.

<div class="container">
  <span class="bg">Element with Background</span>
  <span>Some text next to it.</span>
</div>

<style>
  .container {
    display: flex;
    align-items: baseline; /* start, center etc. behave the same */
  }

  .bg {
    background: rebeccapurple;
  }
</style>

As the following demo shows, each items background is affected by line height in a way we don’t want. Switch the layout to inline to compare, and change the line-height to see the boxes grow in flex and grid. (It animates until you interact with the demo.)

Problem Demo

NewRelease notes
Tags:cssa11y
Save draftLast saved 2 minutes ago
PressCmd+Fto search

Why this happens

  1. A text run in inline layout sits in an inline box. Its background covers only the box’s content area, which is derived from the font (ascent and descent). line-height adds extra space (the half-leading) above and below the content area, but that space is not painted.
  2. Flex and grid items are blockified. The <span> becomes a block-level box (a block container), and its height is the height of the line box inside it, which includes the half-leading from line-height.

Workarounds

  1. A tighter line-height, like line-height: 1, on the element. It shrinks the box, but it overrides your typography and is not tied to the actual glyph size.
  2. A wrapper element around the span (<div><span class="bg">…</span></div>). The span is inline again, so the background is tight. It works, but adds markup only to dodge layout behavior.

Solution

The text-box-trim CSS property trims the half-leading above the first and below the last line of a block container. The box hugs the text in flex, grid or anywhere else.

.bg {
  text-box-trim: trim-both; /* the shorthand `text-box` also works */
}

Toggle it in the demo:

Solution Demo

NewRelease notes
Tags:cssa11y
Save draftLast saved 2 minutes ago
PressCmd+Fto search

By default it trims to the font’s ascent and descent, which matches the inline box. For a tighter fit around the letters, set the edges explicitly, for example text-box: trim-both cap alphabetic (top of capital letters to the baseline). Those explicit edge keywords are not supported in Firefox yet, so stick to the default edges for now.

Browser support: As of writing this in November 2026, text-box-trim with the default edges is Baseline Newly available The explicit edge keywords like cap and alphabetic are not Baseline yet. Check caniuse before relying on it. Older browsers ignore it and show the taller box, so treat it as a progressive enhancement.

Further Reading

  1. MDN: text-box and text-box-trim
  2. CSS Inline Layout Module Level 3: text-box-trim
  3. Original CodePen