liveBook · Manning

Declaration

As explained in CSS in Depth, Second Edition

A CSS declaration is a property-value pair that specifies one styling instruction.

A CSS declaration is one styling instruction: a property paired with a value, such as `color: black;`. Declarations sit inside a declaration block, the curly-braced part of a rule. The selector before that block identifies which elements the rule targets. A CSS property is not an HTML attribute: `color` is a property, while `href` on a link is an attribute. section 1.0

When declarations conflict

Suppose a heading matches `h1 { font-family: serif; }`, `#page-title { font-family: sans-serif; }`, and `.title { font-family: monospace; }`. All three declarations address the same property on the same element. The browser uses the cascade to choose one; here, the ID selector makes `sans-serif` win. A declaration’s position matters too: when the other cascade criteria are equal, the later declaration takes precedence. section 1.1 section 1.1.4

Later does not always mean better. A later `a.featured { background-color: orange; }` declaration can give a featured navigation link its orange background. But if the same class appears on a link outside the navigation, that link gets the orange background without the navigation rule’s text color, padding, or rounded corners. Check which rules match each element, not just which declaration wins. section 1.1.4

aside {
  background-color: #333333;     #1
  background-color: #333333aa;         #2
}

Two `background-color` declarations provide a fallback: browsers that understand eight-digit hex use the later, partly transparent color; browsers that do not understand it ignore that declaration and retain the earlier color. section 1.5.1

How CSS in Depth, Second Edition covers it

How deep it goes
Defines the property-value syntax, then works through conflicts, source order, and unsupported declarations rather than cataloging properties.
Examples
Compares competing heading fonts, a featured navigation link reused elsewhere, and paired solid and transparent background colors.
What it assumes
Introduces declaration, property, value, selector, and declaration block terminology while using familiar HTML and CSS rules in its examples.
What you can do after
Diagnose/debug with it by identifying competing declarations, checking which selectors match, and recognizing when an unsupported declaration is ignored.
How it connects
Connects declarations to selectors, declaration blocks, specificity, source order, and progressive enhancement.

Also explained in

Compare how 4 books explain declaration →

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.