Sub topic 8
From declared to computed to used values
This chapter assumes you can read a CSS rule, know that the cascade picks a winner among conflicting declarations, and can open the Styles and Computed panels in browser developer tools. The cascade tells you which declaration wins. This chapter covers what the browser does with the winning value before it paints anything.
Six stages of a value
A value passes through several stages. Each stage has a name in the CSS specifications.
| Stage | What happens | Example for `font-size: 1.5em` |
|---|---|---|
| Declared | Every declaration that matches the element, written by an author, user or browser | `font-size: 1.5em` and the browser's default |
| Cascaded | The single winner of the cascade for each property | `1.5em` |
| Specified | The cascaded value, or a fallback if there is none (inherited value or initial value) | `1.5em` |
| Computed | Relative values resolved as far as possible without doing layout | `30px` |
| Used | Remaining dependencies, such as percentages of a box size, resolved during layout | `30px` |
| Actual | The used value adjusted for real limits, such as rounding to whole device pixels | `30px` |
You will mostly work with three of these: the cascaded value (the output of the cascade), the computed value (what inheritance passes down) and the used value (what layout produces).
The specified step: filling in missing values
After the cascade, some properties have no winner because no rule set them. The browser then picks a value by property type:
- An inherited property takes the parent's computed value.
- A non-inherited property takes its initial value, the default defined for that property.
This step is the bridge to inheritance, which the next chapter covers in detail. The key point for now is that the child receives the parent's computed value, not the parent's specified value.
Computing relative values
Computing turns relative values into values that do not depend on anything else, where that is possible without layout.
html { font-size: 20px; }
.card {
font-size: 1.5em;
padding: 1em;
width: 50%;
}For .card, the browser computes:
font-size: 1.5embecomes30px. For thefont-sizeproperty,emrefers to the parent's computed font size, so 1.5 × 20px.padding: 1embecomes30pxon each side. For every other property,emrefers to the element's own computed font size, which is now30px.width: 50%stays50%. A percentage of the containing block's width cannot be resolved until layout.
Keywords and colors are also normalized. A color such as rebeccapurple computes to an rgb() value, and bolder computes to a numeric weight based on the parent.
Used values come from layout
The used value is the computed value with the layout-dependent parts resolved. Once the containing block is 800px wide, width: 50% has a used value of 400px. The same applies to width: auto, which has no fixed number until the browser lays out the box.
Developer tools and getComputedStyle() report a mix of both stages. They return the computed value for most properties and the used value for layout-dependent ones such as width and height.
const card = document.querySelector(".card");
const style = getComputedStyle(card);
console.log(style.fontSize); // "30px"
console.log(style.paddingTop); // "30px"
console.log(style.width); // used value, for example "400px"The Computed panel in your browser shows the same numbers. Despite its name, it often shows used values.
Why the computed value matters for inheritance
Children inherit the parent's computed value. This produces a well-known difference in line-height:
.a { font-size: 20px; line-height: 1.5; }
.b { font-size: 20px; line-height: 150%; }Both parents compute to a 30px line height for their own text. A child with font-size: 40px behaves differently in each case:
- Inside
.a, the unitless number1.5is inherited as a number. The child's line height is 1.5 × 40px = 60px. - Inside
.b, the percentage was computed to30pxand the child inherits that fixed length. The child's line height stays 30px, which can make large text overlap.
Use unitless line-height values unless you want the fixed-length behavior.
Common errors
- Expecting
emto mean the same thing infont-sizeandpadding. Infont-sizeit refers to the parent, elsewhere to the element itself. - Expecting a child to inherit a percentage. It inherits the computed result, as in the
line-height: 150%case. - Reading a
getComputedStyle().widthand assuming you wrote it. It is a used value from layout, not necessarily anything in your CSS.
Exercise
The root has font-size: 16px. A paragraph inside it has font-size: 2em; margin-bottom: 0.5em; line-height: 1.5. A <span> inside the paragraph sets nothing. What are the computed font size and margin-bottom of the paragraph, and what line height does the span use?
Show answer
The paragraph's font size is 2 × 16px = 32px. Its margin-bottom uses the element's own computed font size, so 0.5 × 32px = 16px. The span inherits line-height: 1.5 as a number and inherits the paragraph's computed font size of 32px, so its line height is 1.5 × 32px = 48px.