liveBook · Manning

Font-size property

As explained in CSS in Depth, Second Edition

The CSS font-size property specifies the size of an element's text.

The CSS `font-size` property sets an element’s text size. It can also control the scale of surrounding UI when padding and other dimensions use relative units. A useful approach is to set a page-wide size, then override it only where the design needs a different scale. section 2.2 section 2.3.1

Why two em values produce different sizes

Given `body { font-size: 16px; }` and `.slogan { font-size: 1.2em; padding: 1.2em; }`, the slogan’s font size is 16 × 1.2 = 19.2 px. The browser calculates that first. Padding then uses the slogan’s own font size, so it becomes 19.2 × 1.2 = 23.04 px. For a target font size in ems, divide the target pixel size by the inherited pixel size. section 2.2.1

Choose the scope of a font-size change deliberately:

  • For one component, change its `font-size` and use ems for dimensions that should scale with its text. A 1em padding and border radius will follow the component’s size. section 2.2
  • For a page-wide default, set `:root { font-size: 0.875em; }`. With a 16 px inherited browser default, that yields 14 px while remaining relative to user font settings. Descendants can inherit it; exceptions can use units such as rem. section 2.3.1
  • Watch repeated nesting: `ul { font-size: 0.8em; }` makes each nested list 80% of its parent’s size. Use `ul ul { font-size: 1em; }` when deeper lists should stop shrinking. section 2.2.1
Listing 2.12 Responsive base font-size
:root {                    #1
  font-size: 0.85em;      #1
}                 #1         

@media (min-width: 800px) {      #2
  :root {                        #2
    font-size: 1em;              #2
  }                      #2
}                                #2

@media (min-width: 1200px) {   #3
  :root {                      #3
    font-size: 1.15em;         #3
  }                            #3
}                              #3

A responsive root size starts at `0.85em`, then changes to `1em` at 800 px and `1.15em` at 1200 px. Components built with em and rem dimensions can scale without their own font-size media queries; dimensions fixed in pixels do not scale this way. section 2.3.2

How CSS in Depth, Second Edition covers it

How deep it goes
Works through inherited em calculations, nested-size compounding, and responsive root overrides rather than treating font-size as a property-value catalog.
Examples
Uses a padded box, nested lists, and a panel that scales at three screen-width ranges.
What it assumes
Assumes familiarity with CSS rules and HTML elements while explaining em-based font-size calculations from a 16 px starting value.
What you can do after
Apply it via CSS to set inherited defaults, prevent nested text from shrinking, and scale relative-unit components responsively.
How it connects
Connects font-size to inheritance, computed values, ems, rems, media queries, and responsive design.

Also explained in

Compare how 9 books explain font-size property →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.