Representative interview topic

Frontend Interview: How would you use text-box-trim to fix font-metric vertical alignment?

FrontendHard
Offer.cc Editorial TeamPublished Updated

Question

How would you use text-box-trim to fix font-metric vertical alignment?

Prompt and use case

Buttons, badges, and headings in a design system are often aligned to a visual center, while CSS line boxes retain the font's upper and lower half-leading. Explain how to use text-box-trim and text-box-edge to control the text box, how to ship it when support is limited, and how to account for font loading, writing modes, and accessibility.

What the interviewer is testing

  • Distinguishing glyphs, font metrics, line boxes, and the element box model.
  • Explaining start, end, and two-sided trimming precisely.
  • Choosing cap, ex, text, alphabetic, or leading edges with text-box-edge.
  • Handling logical block direction, font fallback, and progressive enhancement.
  • Verifying the result with real browsers, fonts, and accessibility evidence.

Questions to clarify first

  • Is the goal optical centering, cross-font stability, or a fixed line-height and baseline?
  • May the component enable the feature only in supporting browsers?
  • What fonts, sizes, languages, writing modes, and zoom range are in scope?
  • Can the text wrap, and must the focus ring and hit area remain unchanged?

Thirty-second answer

Traditional line boxes include font half-leading, so identical padding can look vertically different across fonts. text-box-trim trims block-axis edges and text-box-edge selects the font metric used as the edge; text-box is the shorthand. I would keep the existing dimensions as a fallback, progressively enhance inside a feature query, preserve a minimum button hit area, and test fonts, languages, writing modes, zoom, and assistive technology.

Deep-dive answer, step by step

1. Draw the four boxes first

Separate the painted glyph area, font metrics, line box, and the element's padding and border. Trimming the text box changes content occupancy on the block axis; it should not be described as cutting the glyph outline or shrinking the hit area.

2. Explain the source of half-leading

When line-height exceeds the font's own line height, the extra space is generally distributed on both sides of the line box. Ascenders, descenders, cap height, and x-height differ by font, so the same line-height can produce a different optical center.

3. Choose the trim edge

text-box-trim accepts none, trim-start, trim-end, and trim-both. A button often needs both sides trimmed; a heading may trim only the block-start edge to preserve rhythm with the preceding paragraph.

4. Choose the font metric

text-box-edge can select edges such as cap, ex, text, alphabetic, or leading. cap can suit an uppercase heading's visual reference, while text is closer to the font text boundary. Test the actual font and language; no metric is a universal truth for every font.

5. Use the shorthand locally

css
.button {
  text-box: trim-both cap alphabetic;
  padding: 0.625rem 1rem;
}

The shorthand expresses trim and edge together. Scope it to components that need it so global trimming does not damage paragraph rhythm, icon alignment, or multiline content.

6. Handle logical writing directions

start and end are logical block-axis edges and change with writing-mode. Measure horizontal and vertical layouts with different direction values; do not substitute a physical top/bottom intuition for the logical definition.

7. Design progressive enhancement

Keep the existing line-height, padding, and minimum height by default, then enable the enhancement in @supports (text-box-trim: trim-both). If a browser ignores the declaration, text must remain readable and the button must retain touch and keyboard dimensions.

8. Verify font loading and accessibility

Measure after the target font has loaded, covering fallback fonts, slow networks, zoom, system fonts, and multilingual strings. Check focus indication, clipping, screen-reader order, and the actual hit rectangle; optical centering cannot trade away operability.

Trade-offs and boundaries

The feature can remove magic negative margins and per-font offsets, but browser support is still limited and font metrics still vary. It does not replace line-height, padding, flex alignment, or a sensible minimum height. Use caution with multiline body text, dynamic fonts, and complex inline content. For design parity, record the target font, metric, browser version, and tolerance instead of keeping only a screenshot.

Rollout plan and evidence

  1. Record the component font, size, language, writing mode, and tolerance.
  2. Keep the current style as the baseline, add a feature-query branch, and lock the minimum interactive size.
  3. Test Chrome 133 and the other browsers in the project matrix; MDN marks the property as limited availability rather than Baseline.
  4. Measure optical offset, focus ring, wrapping, and zoom with real and fallback fonts.
  5. Store screenshots, measurements, browser versions, and the fallback policy in the component acceptance record.

Common mistakes and follow-ups

Mistake 1: Treating trim as glyph clipping

It primarily adjusts the text-box edge; it does not slice the glyph outline by pixels. Return to font metrics, line boxes, and the element box model.

Mistake 2: Setting trim-both globally

Paragraphs, lists, and multiline content need normal rhythm. State the component scope and fallback boundary.

Mistake 3: Capturing only the default font

A failed font load, language change, or system font changes metrics. Include fallback and slow-network tests.

Mistake 4: Ignoring logical axes

trim-start is not always the physical top. Verify block-axis behavior in vertical writing and different direction values.

Mistake 5: Checking visuals but not operation

Trimming must not reduce touch, keyboard focus, or readable area. Give the minimum-size, focus-ring, and assistive-technology checks.

Public sources

Related questions