CSS: Declarative Styling
What you'll learn: You'll see how CSS rules declare what visual properties elements should have, rather than providing step-by-step rendering instructions.
The Core Idea
CSS (Cascading Style Sheets) is a declarative language for styling web pages. Instead of writing imperative commands like "loop through all paragraphs, set each font size to 16 pixels, then set each color to blue," you simply declare the desired end state:
p {
font-size: 16px;
color: blue;
}
You're telling the browser what you want—"all paragraphs should be blue and 16 pixels"—not how to achieve it. The browser's rendering engine figures out the implementation details: traversing the DOM tree, computing inheritance, applying specificity rules, and painting pixels to the screen.
Why This Matters
This declarative approach separates intent from mechanism. You describe the visual properties that should exist; the browser handles the complex rendering pipeline. Just like SQL lets you declare what data you want (not how to fetch it) and HTML lets you declare document structure (not how to parse it), CSS lets you declare appearance.
Consider responsive design: you declare breakpoints and rules for different screen sizes, and the browser automatically applies the correct styles when conditions match. No manual checking or imperative branching required on your part.
The Declarative Pattern
Each CSS rule consists of:
- Selector: which elements this applies to (declarative query)
- Properties: what visual characteristics they should have (declarative state)
The rendering engine resolves conflicts, computes values, and executes the low-level drawing—all hidden from you.
Key Takeaway: CSS declares the desired visual state of elements without specifying the imperative steps to render them, letting the browser handle the "how" while you focus on the "what."