Course outline · 0% complete

0/30 lessons0%

Course overview →

Your first stylesheet

lesson 4-1 · ~9 min · 10/30

From lesson 3-3, the element holding the page's dominant content, and the landmark screen readers jump to first, is main.

LandmarkRegion
headerthe banner
navthe menu
mainthe primary content
footerthe contact and copyright line

Styling those landmark regions starts now, so their names matter twice over. They double as CSS selectors.

Anatomy of a CSS rule

CSS exists to keep appearance out of the markup. Before it, styling lived inside HTML itself, with font tags on every element, so restyling a site meant editing every page by hand.

With CSS one stylesheet styles a thousand pages, and one edit changes them all. That leverage is the point of this unit.

CSS is a list of rules, where each rule names which elements through a selector and which styles through declarations.

h1 {
  color: darkblue;
  font-size: 40px;
}
PieceExample
selectorh1, targeting every h1 on the page
propertycolor
valuedarkblue

Each declaration is a property: value; pair inside the braces, and the semicolons are not optional.

The usual way to attach CSS is a separate file linked from the head, using the skeleton from lesson 2-1:

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

Quick experiments can also embed rules directly in the head between <style> and </style> tags. The examples in this course keep CSS in its own labelled block, which behaves like a linked stylesheet.

h1 {color: darkblue;font-size: 40px;}selector: which elementsproperty and value, one declarationthe semicolon ends each oneA rule with two declarations styles every h1 on the page in one place.
One CSS rule: the selector picks the elements, and each declaration inside the braces sets one property to one value.

The three selectors you will use daily

p { color: gray; }             /* type: every <p> */
.warning { color: red; }       /* class: every element with class="warning" */
#site-title { color: black; }  /* id: the ONE element with id="site-title" */

And in the HTML:

<h1 id="site-title">Hack University</h1>
<p class="warning">This action cannot be undone.</p>
SelectorSyntaxMatches
typea bare tag nameevery element of that kind
classa dot plus the nameevery element carrying the class
id# plus the idthe single element with that id

The class selector is the workhorse, because an element can carry several classes as in class="warning small", and one class can be reused on many elements. An id must be unique on the page, which limits how useful it is for styling.

Two combinators cover most of the rest. A comma shares styles across selectors, as in h1, h2 { font-family: Georgia; }, and a space targets descendants, so nav a { ... } means links inside the nav.

Selecting a class

Every element with class="card" is selected by .card.

SelectorLooks for
.cardclass="card"
#cardid="card"
carda <card> element, which does not exist

Class selectors start with a dot, and forgetting it produces a rule that silently matches nothing. The third row explains why the mistake is so quiet, since a selector for a nonexistent element type is valid CSS that simply never applies.

One rule per kind of selector

Three rules, one for each selector kind, with the HTML left untouched.

CSS

p {
  color: gray;
}

.warning {
  color: red;
  font-weight: bold;
}

#site-title {
  color: darkblue;
}
SelectorKindMatches
ptypeevery paragraph
.warningclassanything with class="warning"
#site-titleidthe one element with that id

A rule is always selector { property: value; }, and the class selector needs its dot.

The warning paragraph is targeted by two rules at once, and the red wins. The next two lessons explain exactly why that happens, since guessing at it is how CSS starts to feel unpredictable.