Lesson 1 of 7 · 5 min

How CSS works

Rules, selectors and declarations, three ways to add CSS to a page, and how the cascade and inheritance decide what you see.

On this page
  1. Three ways to add CSS
  2. The cascade
  3. Inheritance
  4. Units you will use most
  5. Try it

CSS stands for Cascading Style Sheets. A stylesheet is a list of rules. Each rule picks some elements with a selector and sets declarations on them.

h1 {
  color: #0a0a0a;
  font-size: 2rem;
}

Here h1 is the selector, and color: #0a0a0a; is a declaration made of a property (color) and a value (#0a0a0a). Every declaration ends with a semicolon.

Three ways to add CSS

1. An external stylesheet (use this). One file, shared by every page, cached by the browser.

<head>
  <link rel="stylesheet" href="styles.css">
</head>

2. A <style> element inside the page. Handy for a quick experiment or a single-page demo.

<style>
  body { font-family: system-ui, sans-serif; }
</style>

3. An inline style attribute. It only affects one element and is hard to override, so keep it for values set by JavaScript.

<p style="margin-top: 0">Inline styles win over most rules.</p>

The cascade

When two rules set the same property on the same element, the browser has to pick one. It compares, in order:

  1. Importance: a declaration marked !important beats a normal one. Avoid it; it makes later changes painful.
  2. Specificity: a more specific selector wins (covered in the next lesson).
  3. Source order: if everything else is equal, the rule that comes later wins.
p { color: black; }
p { color: gray; } /* later, same specificity: paragraphs are gray */

Inheritance

Some properties pass down from a parent to its children. Text properties such as color, font-family, font-size and line-height inherit. Box properties such as margin, padding, border and width do not.

That is why you set the font once on body:

body {
  font-family: "Geist", system-ui, sans-serif;
  line-height: 1.6;
  color: #0a0a0a;
}

Every paragraph, list and link inside now uses that font, unless a more specific rule says otherwise.

Units you will use most

Unit Relative to Good for
px Nothing, a fixed size Borders, small details
rem The root font size (usually 16px) Font sizes, spacing
em The element's own font size Padding that grows with text
% The parent's size Widths
vw / vh The viewport width / height Full-screen sections

Prefer rem for type and spacing. If a reader raises their browser's default font size, a rem-based page scales with it.

Try it

Make a page with an h1 and two paragraphs. Set the font on body, then give the second paragraph a different colour with a class:

body { font-family: system-ui, sans-serif; }
.note { color: #666; }
<p>Normal text.</p>
<p class="note">Muted text.</p>

Open the browser's developer tools (right-click, Inspect) and select the second paragraph. The Styles panel shows every rule that matches it, with overridden declarations crossed out. That panel is the best way to answer "why does this look like that?".

More from CSS Crash Course