Sub topic 8
From Declared to Computed to Used Values
This chapter assumes you can write CSS rules and know the cascade's ordering criteria from earlier chapters: origin and importance, layers, specificity, and source order. It also assumes you can open the Computed panel in browser developer tools. Here you follow a value after the cascade picks it, through the stages that turn it into the number the browser paints.
Why a value has several names
You write font-size: 1.5em. The browser can't draw "1.5em", so it has to reduce that to pixels. Some values, such as width: 50%, can't become pixels until layout runs. Others, such as border-width: 5px with border-style: none, change meaning depending on other properties.
CSS handles this with a fixed pipeline. Each stage has its own name, and each stage answers a different question. Knowing which stage you are looking at explains why getComputedStyle() sometimes returns 200px for a width you wrote as 50%. It also explains why a child sometimes inherits a different font size than you expect.
The six stages
| Stage | What happens | Example for `width: 50%` |
|---|---|---|
| Declared | Every declaration that matches the element, from any origin | `width: 50%` and `width: 300px` from two rules |
| Cascaded | The cascade picks one winner per property | `width: 50%` |
| Specified | If nothing was cascaded, a default fills in | `50%` |
| Computed | Relative values become absolute where possible without layout | `50%` (it needs the container's width) |
| Used | Layout resolves what was left over | `200px` if the container is 400px wide |
| Actual | The browser adjusts for its own limits | `200px` |
The sections below take these in turn.
Declared and cascaded values
A declared value is any declaration that applies to the element. Several can compete for one property. The cascade rules you already know select the cascaded value. The other declarations are discarded for that property.
If no declaration applies, the property has no cascaded value, and the next stage fills the gap.
The specified value and defaulting
The specified value is the cascaded value if there is one. If there isn't, defaulting applies:
- If the property is inherited, the specified value is the parent's computed value.
- If the property is not inherited, the specified value is the property's initial value.
This is why a <p> with no color rule still gets its parent's color, while the same <p> gets margin from its own initial value (or the browser's default stylesheet) and never from its parent.
The CSS-wide keywords (inherit, initial, unset, revert, revert-layer) are handled here. They are cascaded like any other value, then replaced by the value they stand for.
The computed value
Computing turns a specified value into a form that can be inherited. The aim is to resolve everything that does not depend on layout. Typical conversions:
emandremlengths become absolute pixel lengths.- Relative URLs become absolute URLs.
- Keywords such as
bolderbecome numbers (font-weight: bolderon a parent of weight 400 computes to 700). - Some keywords become lengths.
border-width: thincomputes to a fixed length, typically1px. - Percentages stay percentages when they need layout.
width: 50%computes to50%. autooften staysauto, as withwidth: auto.
Here is a worked example of how em is resolved:
html { font-size: 20px; }
.parent {
font-size: 1.5em; /* 1.5 x the parent's computed 20px = 30px */
}
.child {
font-size: 0.5em; /* 0.5 x the parent's computed 30px = 15px */
padding: 1em; /* for non-font-size properties, em uses this element's own computed font-size: 15px */
}em in font-size is relative to the parent's computed font size. In every other property, em is relative to the element's own computed font size. Both rules depend on computed values.
Inheritance passes the computed value
When a child inherits a property, it receives the parent's computed value, not the parent's written value. This is the main practical consequence of the pipeline.
.box {
font-size: 20px;
line-height: 150%;
}
.box .big {
font-size: 40px;
}For .box, a percentage line-height computes to an absolute length: 150% of 20px is 30px. The .big child inherits 30px, so its text has a 30px line height even though its font is 40px. Lines can then look cramped.
Now use a unitless number:
.box {
font-size: 20px;
line-height: 1.5;
}
.box .big {
font-size: 40px;
}A unitless line-height computes to the number itself. The child inherits 1.5, then multiplies by its own font size, giving 60px. This is why a unitless line height is the usual recommendation.
Computing depends on other properties
Some computed values change when another property changes:
.frame {
border: 5px none red;
}Because border-style is none, the computed border-top-width (and the other three sides) is 0px, not 5px. The declared width is still there, but the computed value ignores it. If you later change the style to solid, the width becomes 5px again.
The used value
The used value is the computed value after layout has resolved anything that was still relative. This is where percentages, auto widths, and similar values become pixel lengths.
<div class="container">
<div class="item">Half the container</div>
</div>
<style>
.container { width: 400px; }
.item { width: 50%; }
</style>
<script>
const item = document.querySelector(".item");
console.log(getComputedStyle(item).width); // "200px"
</script>The computed value of .item's width is 50%. The used value is 200px, because the container is 400px wide. Used values are not inherited. Only computed values flow down the tree.
Percentage widths are a clear case. A child of an element with width: 50% that also declares width: inherit receives 50% (the computed value of the parent), not 200px. It then resolves that percentage against its own container.
The actual value
The actual value is the used value after the browser applies practical limits. A browser might round a border width to a whole device pixel, or substitute a fallback font size. You rarely meet this stage directly, and developer tools usually do not show it. It is mentioned mainly so the name is not a surprise.
What `getComputedStyle()` returns
The name is misleading. getComputedStyle() returns resolved values, and a resolved value is defined per property:
- For most properties, the resolved value is the computed value. Examples are
color,font-size, anddisplay. - For layout-dependent properties such as
width,height,margin, andpadding, the resolved value is the used value, but only if the element is being rendered. - If the element has
display: none, there is no layout, so the same properties return the computed value.width: autois then reported asauto, not as a pixel length.
This script shows the difference:
<div id="box" style="width: 50%;">Box</div>
<script>
const box = document.getElementById("box");
console.log(getComputedStyle(box).width); // a pixel length, such as "512px"
box.style.display = "none";
console.log(getComputedStyle(box).width); // "50%"
</script>The first log shows the used value. After hiding the element there is no layout, so the second log shows the computed value 50%.
Custom properties
Custom properties follow the same pipeline in a reduced form. An unregistered custom property such as --gap is stored as a sequence of tokens. Its computed value is the specified value after any var() references are substituted. Lengths in it are not resolved to pixels:
.parent {
font-size: 20px;
--space: 2em;
}
.child {
font-size: 10px;
padding: var(--space);
}--space stays 2em as it is inherited. The child's padding therefore uses 2em at the child's own 10px font size, giving 20px, not the 40px the parent would get. A custom property registered with @property and a length syntax would instead be computed to 40px on the parent, and the child would inherit 40px. Later chapters cover custom properties in more detail.
Common errors
- Expecting
emto be inherited asem. Children inherit pixels. A child of a1.5emparent inherits the computed pixel size, not the multiplier. - Compounding
emfont sizes. Nested elements withfont-size: 0.9emshrink at every level, because each level multiplies the parent's computed size. Useremwhen you want a fixed reference. - Using a percentage or length
line-heighton a container. Descendants with larger text inherit a line height that is too small. Use a unitless number. - Reading
getComputedStyle().widthand expecting the stylesheet value. It reports the used value for rendered elements. - Setting a border width without a style. The computed width is
0pxuntilborder-styleis something other thannoneorhidden. - Expecting an inherited percentage to track the child. A percentage that needs layout (such as
width) stays a percentage on inheritance and is resolved against the child's own containing block.
Working through a full example
Take this markup and CSS:
<section class="card">
<p class="lead">Text</p>
</section>
<style>
html { font-size: 16px; }
.card { font-size: 1.25rem; line-height: 1.5; width: 50%; }
.lead { font-size: 1.5em; padding: 1em; }
</style>Assume the <section> is in a container 800px wide. Following each stage:
.cardfont-size: declared1.25rem, computed20px(1.25 × 16px root size)..cardline-height: computed1.5..cardwidth: computed50%, used400px..leadfont-size: declared1.5em, computed30px(1.5 × 20px parent)..leadline-height: no declaration, so it is inherited as1.5. The used line box is45px(1.5 × 30px)..leadpadding: declared1em, computed30pxon each side, becauseemhere uses the element's own computed font size..leadwidth: not declared and not inherited, so the initial valueautois the computed value. Layout then makes the used width the available space minus padding, which is340px(400px minus 30px on each side).
Summary
- A value passes through declared, cascaded, specified, computed, used, and actual stages.
- The cascade picks the cascaded value. Defaulting supplies inherited or initial values when none exists.
- The computed value resolves what can be resolved without layout, and it is the value children inherit.
- The used value comes from layout. It is not inherited.
getComputedStyle()returns the used value for layout properties on rendered elements and the computed value otherwise.
Exercise
A container has font-size: 16px; line-height: 150%;. A child inside it sets font-size: 32px and declares no line-height. What line height does the child's text use, and what would it be if the container used line-height: 1.5 instead?
Show answer
With 150%, the container's computed line height is 24px (150% of 16px). The child inherits that computed length, so its line height is 24px, which is smaller than its own 32px font. With 1.5, the computed value is the number 1.5, which the child inherits as a number. Its line height becomes 1.5 × 32px = 48px.
Exercise
An element has width: 25% and sits in a 600px-wide containing block. A script reads getComputedStyle(el).width. What does it return while the element is displayed, and what does it return after el.style.display = "none" is set? Which stage does each result correspond to?
Show answer
While displayed, it returns 150px, the used value (25% of 600px). After the element is set to display: none, there is no layout, so it returns the computed value 25%.