Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

What does unitless line-height do differently from a length value?

Quick answer

A unitless line-height is a multiplier: each element uses it with that element’s own font size. A length such as 30px computes to an absolute length on the parent and is inherited as that length. So a smaller child can get proportionally tighter spacing with the unitless value, but retain the parent’s line height with the length. Unitless values are usually the safer default when descendants have different font sizes. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I’m styling a parent with a heading and a smaller caption inside it. What is the practical difference between line-height: 1.5 and line-height: 30px?

Geez

The key difference is what the child inherits. A unitless number stays a number, so each element applies that multiplier to its own font size. A length computes to an absolute length on the parent, and the child inherits that length rather than recalculating it for its own font size. [S1]

For example, assume the parent’s font size is 20px and the caption’s is 12px:

.parent {
  font-size: 20px;
  line-height: 1.5;
}

.caption {
  font-size: 12px;
}

The parent’s line-height is 1.5 × 20px = 30px. The caption uses the inherited multiplier with its 12px font size, giving an 18px line-height. If the parent instead has line-height: 30px, that length is inherited, so the caption’s line-height remains 30px. The two declarations can therefore produce different spacing around smaller child text. [S1]

Follow-up question

When you say “computed value,” what is computed for each version? I want to understand why the child behaves differently.

Geez

With line-height: 1.5, the computed value remains the number 1.5. The child inherits that number, then applies it to its own font size. With line-height: 30px, the parent’s computed value is an absolute length; that length is inherited by the child. [S1]

In the example, the parent’s 20px text has a 30px line-height either way. But the child’s result differs: with the unitless value, 12px × 1.5 gives 18px; with the length, it inherits 30px. That second result may leave more vertical space around the smaller caption than you intended. These calculations explain the inherited line-height values; they do not by themselves determine every detail of how a particular design will look.

Follow-up question

Could you show both versions side by side, and tell me which one you’d start with for a mixed-size component?

Geez

Here is the comparison using the same assumed font sizes:

/* Multiplier: the 12px child uses an 18px line-height. */
.unitless {
  font-size: 20px;
  line-height: 1.5;
}

.unitless .caption {
  font-size: 12px;
}

/* Length: the 12px child inherits a 30px line-height. */
.length {
  font-size: 20px;
  line-height: 30px;
}

.length .caption {
  font-size: 12px;
}

I’d start with the unitless value when descendants have different font sizes: it scales with each element’s text size and avoids inheriting the parent’s absolute line-height. The reference recommends unitless values in most cases for this reason. [S1] Choose a length when you specifically want the inherited line-height to stay fixed; check the result on the child text rather than assuming the parent’s value will suit it.

Follow-up question

Can you give me a quick rule I can reuse when reviewing a component stylesheet?

Geez

Ask whether the child should scale its line spacing with its own font size. If yes, prefer a unitless multiplier, such as 1.5. If the child should inherit the same absolute line-height as its parent, a length may be intentional. Compare the parent’s and child’s resulting line-height values: in the example, expect 30px on the parent and 18px on a 12px child for the unitless case, versus 30px on both for the length case. [S1]

Those values follow the stated assumptions and inheritance behavior; the source does not establish which spacing will look best for every design. Check your actual component and adjust the value to suit its typography.

A practical checklist

  • Identify the parent and child font sizes you are comparing.

  • For a unitless value, multiply the number by each element’s own font size.

  • For a length, check whether the child inherits the parent’s absolute line-height.

  • Compare the parent and child results in the component before settling on a value.

  • Use a unitless multiplier as the usual starting point for mixed-size descendants.

A few more useful answers

Does `1.5` mean 1.5 pixels?

No. It is a unitless multiplier, not a length. Each element applies it to its own font size. [S1]

Will a length always be wrong for a child with smaller text?

No. It can be intentional when you want the child to inherit the same absolute line-height. Check whether that fixed spacing suits the smaller text. [S1]

Which value is the better default when font sizes vary?

A unitless value is generally the safer starting point because descendants can scale the line-height with their own font sizes. [S1]

Sources

References used for this guide.

  1. [S1] MDN: line-height values and inheritance
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work