🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Line-height

AI & DATA SCIENCE // line-height

The line-height property sets the height of each line of text, controlling the vertical spacing between lines within a block of text.

Syntax

line-height: normal | number | length | percentage;

Deep Dive Course

A unitless numeric value, like line-height: 1.5, is generally the recommended approach, since it's calculated as a multiple of the element's own font-size and is inherited as that unitless ratio to descendants, meaning nested elements with a different font-size still get a proportionally scaled line-height rather than inheriting a fixed pixel value that wouldn't scale correctly. Setting line-height equal to a fixed pixel height matching an element's total height, like line-height: 50px on a 50px-tall single-line element, is also a classic technique for vertically centering a single line of text within that element.

1Understanding Line-height

A unitless numeric value, like line-height: 1.5, is generally the recommended approach, since it's calculated as a multiple of the element's own font-size and is inherited as that unitless ratio to descendants, meaning nested elements with a different font-size still get a proportionally scaled line-height rather than inheriting a fixed pixel value that wouldn't scale correctly. Setting line-height equal to a fixed pixel height matching an element's total height, like line-height: 50px on a 50px-tall single-line element, is also a classic technique for vertically centering a single line of text within that element.

💡

Prefer a unitless line-height value, like 1.5, over a fixed pixel or percentage value — a unitless value is inherited as a ratio and recalculates correctly against each descendant's own font-size, while a fixed value inherits as that same fixed size regardless of a nested element's different font-size.

editor.html
p {
  font-size: 16px;
  line-height: 1.6;
}
localhost:3000

2Practical Example

Here is a real-world application of Line-height showing how it is used in production CSS code.

editor.html
.button {
  height: 50px;
  line-height: 50px;
  text-align: center;
}
localhost:3000

3Best Practices

Follow these guidelines when working with Line-height:

1. Use a unitless line-height value, like 1.5 or 1.6, as the default choice, since it scales correctly and predictably with each element's own font-size, including for nested elements with different sizes

2. Set line-height equal to a container's fixed height to vertically center a single line of text within it, a simple, classic centering technique

3. Increase line-height for large blocks of body text, typically to somewhere between 1.4 and 1.8, to improve readability compared to a font's often-cramped default line-height

⚠️

Tip: Prefer a unitless line-height value, like 1.5, over a fixed pixel or percentage value — a unitless value is inherited as a ratio and recalculates correctly against each descendant's own font-size, while a fixed value inherits as that same fixed size regardless of a nested element's different font-size.

editor.html
p {
  font-size: 16px;
  line-height: 1.6;
}
localhost:3000

Examples

Example 01Basic Usage
p {
  font-size: 16px;
  line-height: 1.6;
}
Example 02Advanced Example
.button {
  height: 50px;
  line-height: 50px;
  text-align: center;
}

Best Practices

  • Use a unitless line-height value, like 1.5 or 1.6, as the default choice, since it scales correctly and predictably with each element's own font-size, including for nested elements with different sizes
  • Set line-height equal to a container's fixed height to vertically center a single line of text within it, a simple, classic centering technique
  • Increase line-height for large blocks of body text, typically to somewhere between 1.4 and 1.8, to improve readability compared to a font's often-cramped default line-height

Interview Question

Why is a unitless line-height value, like 1.5, generally recommended over an equivalent fixed pixel value, like 24px, especially in a component meant to be reused with different font sizes?

Hint: Think about how each type of value is inherited by nested elements, particularly ones that might have a different font-size than their parent.

A unitless line-height value is inherited by descendant elements as that same unitless ratio, meaning each descendant recalculates its own actual line height by multiplying that inherited ratio against its own specific font-size, so a nested element with a larger or smaller font-size than its parent still receives a proportionally appropriate, correctly-scaled line height automatically. A fixed pixel value, by contrast, is inherited as that literal computed pixel amount, not as a ratio, so a nested element with a significantly larger font-size than its parent would inherit that same fixed, now too-small pixel line-height, potentially causing lines of larger text to visually overlap or crowd together uncomfortably, since the inherited spacing no longer bears any correct proportional relationship to the actual, larger font-size being displayed. This is exactly why a unitless value is considered the safer, more broadly correct default, particularly in reusable components or contexts where nested elements might reasonably have varying font sizes.

Exercises

MediumPractice using Line-height in a real scenario.
View Solution
p {
  font-size: 16px;
  line-height: 1.6;
}

Frequently Asked Questions

Why is a unitless line-height value, like 1.5, generally recommended over an equivalent fixed pixel value, like 24px, especially in a component meant to be reused with different font sizes?

A unitless line-height value is inherited by descendant elements as that same unitless ratio, meaning each descendant recalculates its own actual line height by multiplying that inherited ratio against its own specific font-size, so a nested element with a larger or smaller font-size than its parent still receives a proportionally appropriate, correctly-scaled line height automatically. A fixed pixel value, by contrast, is inherited as that literal computed pixel amount, not as a ratio, so a nested element with a significantly larger font-size than its parent would inherit that same fixed, now too-small pixel line-height, potentially causing lines of larger text to visually overlap or crowd together uncomfortably, since the inherited spacing no longer bears any correct proportional relationship to the actual, larger font-size being displayed. This is exactly why a unitless value is considered the safer, more broadly correct default, particularly in reusable components or contexts where nested elements might reasonably have varying font sizes.

Related Functions

Font-sizeFont-familyText-align