A CSS property is a defined parameter of presentation or layout, such as
color, width or margin-left. Its definition specifies allowed values, an initial value, whether it inherits, and how values are computed. A CSS declaration proposes a value for a property on the elements matched by its rule.[ref]CSS Cascading and Inheritance Level 5 by World Wide Web Consortium. Value Processing and Defaulting: property values, initial values and inheritance
For example,
width: 50% names the width property and supplies a percentage value. If that percentage resolves against a containing width of 400 CSS pixels, the resulting width is 200 CSS pixels in the simple case. The same declaration in a 600-pixel containing width can yield 300 pixels. A property name therefore identifies a parameter, while its eventual layout value can depend on context.Different properties have different value grammars and defaulting behavior. A color word can be valid for
color but invalid for width. Foreground color normally inherits, while width does not inherit by default. The absence of a width declaration is therefore different from automatically copying the parent's width.[ref]Assigning property values, Cascading, and Inheritance in CSS 2.2 by W3C. §§6.1–6.2, assigning property values and inheritance
A property also differs from an HTML attribute with similar wording. Attribute interpretation is part of the element's document semantics; a CSS property participates in the styling value process, including the CSS cascade. Keeping the property definition, individual declarations and the resulting CSS used value separate makes styling behavior much easier to explain.
A CSS property value is a value considered or assigned for a CSS property. Its representation changes through value processing: declarations supply candidates, the CSS cascade chooses among applicable candidates, defaulting supplies missing values, and computation and layout resolve further details. The CSS computed value and CSS used value consequently describe different stages.[ref]
CSS Cascading and Inheritance Level 5 by World Wide Web Consortium. Value Processing, declared, cascaded, specified, computed, used and actual values
Take
width: 50%. In a simple block layout, its computed representation can retain a percentage, while the used width depends on the containing width. A 400-CSS pixel containing block gives 200 pixels; changing that block to 600 pixels gives 300. Copying the source string alone would miss why the physical layout changed.Inheritance can resolve another value before that stage. If a parent's computed font size is
20px and its child specifies font-size: 150%, the child can compute to 30px. That child's descendant inheriting the font size receives the computed result, rather than repeatedly multiplying by the original percentage merely because it is further down the tree.[ref]CSS Cascading and Inheritance Level 5 by World Wide Web Consortium. Computed Values and Inheritance: inherited values are generally computed values
Validity is property-specific. A syntactically readable
width: blue is not a valid width value, and its failure does not establish a used width of blue or zero. Initial values, CSS inheritance and other applicable declarations determine the result under the relevant rules. Stating the value stage prevents a source token, a computed representation and a measured rendering from being conflated.A CSS used value is a value obtained after applying the calculations needed for layout. It can depend on geometry unavailable when the CSS computed value was established, such as the size of a CSS containing block or the sizes of related content.[ref]
CSS Cascading and Inheritance Level 5 by World Wide Web Consortium. Value Processing, Used Values
For example, an ordinary block whose computed width remains 50 percent can have a used content width of 200 CSS pixels in a 400-pixel containing block. Changing that containing block to 600 pixels yields 300 pixels, with the same computed percentage and with other constraints held fixed. The changed result follows from layout context rather than from a new source declaration.
A used width is also not necessarily the size of the whole outer box. With content-box sizing, padding, borders and margins are accounted for outside the content width. With
box-sizing: border-box, the sizing edge is different. CSS minimum size and CSS maximum size constraints can alter the final result as well.[ref]CSS Box Sizing Module Level 3 by World Wide Web Consortium. Preferred size, minimum/maximum sizes and box-sizing
Even after the used value has been determined, drawing on a particular device can require rounding or other permitted approximation. A fractional CSS length and a count of illuminated physical pixels are consequently different measurements. Stating the property, value stage, sizing edge and coordinate units makes a layout calculation reproducible.
A CSS computed value is the result of processing a property's specified value as far as possible without performing the layout-dependent calculations reserved for later stages. Computation resolves details such as inheritance and many relative units; the exact rule belongs to each CSS property.[ref]
CSS Cascading and Inheritance Level 5 by World Wide Web Consortium. Value Processing, Computed Values
For example, if a parent has a computed font size of 20 CSS pixels and its child specifies
font-size: 150%, the child's computed size is 30 pixels. A descendant inheriting that font size receives the computed result, rather than the original percentage expression.Not every relative expression disappears at this stage. A percentage width can remain a percentage until the containing width is known. Similarly, a unitless
line-height: 1.5 computes as a number under that property's definition; when inherited by text with a 24-pixel font size, it can yield a 36-pixel used line height. Copying the parent's final pixel height would be a different operation.[ref]CSS Cascading and Inheritance Level 5 by World Wide Web Consortium. Computed Values and Inheritance
[ref]The computed stage therefore supplies a stable representation for inheritance without promising that every value is a final numeric measurement. To turn it into a CSS used value, the formatter can still need containing dimensions, content measurements and the relevant box-layout algorithm.
A CSS initial value is the value defined as a property's starting point by its specification. It participates in defaulting when an element has no other specified value under the applicable rules. It is distinct from a browser's default stylesheet declaration and from the value obtained by CSS inheritance.[ref]
CSS Cascading and Inheritance Level 5 by World Wide Web Consortium. Defaulting, Initial Values and Inheritance
For example, the initial value of
display is inline, while a browser normally supplies styling that makes a paragraph block-level. Setting p { display: initial; } requests the property's initial value, rather than restoring the browser's usual paragraph appearance. The paragraph's HTML element semantics remain separate from that display choice.The keyword
initial explicitly requests this starting value. inherit requests the parent's computed value instead, and unset chooses inheritance for inherited properties and the initial value for non-inherited properties. Thus these keywords can produce different results even when they appear in the same selector and win the same CSS cascade comparison.[ref]CSS Cascading and Inheritance Level 5 by World Wide Web Consortium. Explicit Defaulting: initial, inherit and unset
Initial values also need their normal processing. A width initially set to auto still requires the appropriate sizing algorithm; initial does not mean that every property begins as zero or already has a final pixel size. The sequence from defaulting through computation to the CSS used value explains how a specification-defined starting value becomes a concrete rendering.
Articles by others on the same topic
There are currently no matching articles.