liveBook · Manning

CSS

As explained in CSS in Depth, Second Edition

CSS is a stylesheet language for describing the presentation of structured documents such as HTML pages.

CSS declares how elements should appear under particular conditions. A selector identifies elements; a declaration supplies a property and value, such as `color: black`. The browser determines which rules apply and renders the page. That simple syntax can become difficult to predict when several rules affect the same element. section 1.0 section 1.1

Why didn’t the heading use that font?

Suppose a heading matches `h1`, `#page-title`, and `.title`, each assigning a different `font-family`. The ID selector wins, so the heading renders in sans serif. When a declaration appears to have no effect, check the other rules targeting the element rather than treating each rule in isolation. section 1.1

Listing 1.2 Conflicting declarations
h1 {                 #1
  font-family: serif;
}


#page-title {             #2
  font-family: sans-serif;
}


.title {                   #3
  font-family: monospace;
}

Compare the three matching selectors and their conflicting font declarations. section 1.1

Predictable CSS depends on more than making a selector win today. A general link rule may outrank a simpler button class even when the button style better expresses your intent. Consider what will happen when markup changes or the stylesheet is reused on another page. section 8.1 section 1.1

Two other constraints shape practical stylesheets:

  • The final page is assembled after content and styles are authored. A stylesheet can serve many pages, while users can change font size or browser-window size; avoid assuming a fixed canvas. section 2.1
  • For reusable interfaces, divide styles into modules responsible for small sets of elements. Aim for changes to one component not to produce unexpected effects in another. section 9.1
  • For a newer selector, give it a separate ruleset from a supported fallback. If one selector in a combined list is unsupported or invalid, the browser ignores the whole ruleset. section 1.5.2

How CSS in Depth, Second Edition covers it

How deep it goes
Works from declaration syntax through conflicting selectors and practical stylesheet organization, rather than treating CSS as a property reference.
Examples
Builds a page header with conflicting heading fonts and contrasts separate fallback rules for a newer selector.
What it assumes
Assumes some familiarity with CSS properties while defining declarations and selectors and starting the worked page with HTML.
What you can do after
Diagnose/debug with it when rules conflict, and organize reusable component styles to limit unexpected effects.
How it connects
Connects CSS to HTML, the cascade, selector specificity, progressive enhancement, and encapsulation.

Also explained in

Compare how 13 books explain CSS →

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.