Find a course

Module 3: Styling with CSSLesson 5 of 1112 min

CSS Rules, Colors and Fonts

Learn how a CSS rule is built, connect a style sheet to your page, target elements with selectors and choose readable colors and fonts.

Step 1 of 11Learn

Anatomy of a CSS rule

Your HTML page works, but it looks like a document from 1995: black text, white background, blue underlined links. CSS changes that. A CSS file is a list of rules, and every rule has the same shape:

h1 {
  color: darkslateblue;
  font-size: 40px;
}
  • h1 is the selector: which elements to style.
  • Inside the curly braces are declarations, each a property and a value separated by a colon and ending with a semicolon.

Put your rules in a separate file, say style.css, and connect it inside the <head> of every page:

<link rel="stylesheet" href="style.css">

One style sheet can style a hundred pages. Change the heading color once in the CSS file and every page updates. That's the main reason to keep CSS separate rather than sprinkling style="..." attributes through your HTML.

A missing semicolon or curly brace is the most common CSS bug. When it happens, the browser quietly ignores the broken declaration, and often the next one too, without any error message. If a style "just doesn't work," check the punctuation first.

Step 2 of 11Practice · Diagram

Anatomy of a CSS rule

Tap the part that decides which elements get styled.

Tap the shape that answers the question, then press Check.

Anatomy of a CSS ruleParts diagram: h1 { color: navy; }, Selector: h1, Property: color, Value: navy, Braces, colon, semicolon.h1 { color:navy; }Selector: h1Property:colorValue: navyBraces, colon,semicolon
h1 { color: navy; }
Selector: h1

Which elements to style.

Property: color

What to change.

Value: navy

What to change it to.

Braces, colon, semicolon

Missing one of these breaks the rule.

Step 3 of 11Practice · Spot the mistake

Find the lines in this style sheet that break CSS syntax.

Step 4 of 11Learn

Selectors pick the target

The three selectors you'll use most:

  • Element selector p styles every <p>.
  • Class selector .card styles every element with class="card". A class can be reused on as many elements as you want, and one element can have several classes: class="card featured".
  • ID selector #signup styles the one element with id="signup". An id must be unique on the page.

You can also combine them. nav a means "any <a> inside a <nav>," which lets you style menu links differently from links in your paragraphs.

When two rules disagree, CSS uses the cascade to decide who wins. Roughly: an id beats a class, and a class beats an element selector. If two rules are equally specific, the one that comes later in the file wins. So if p { color: gray; } appears and then .intro { color: navy; }, a paragraph with class intro is navy.

Beginners tend to use ids for styling everything. Prefer classes: they're reusable and easier to override later.

Step 5 of 11Practice · Match the pairs

Match each selector to what it styles.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. p
  2. .warning
  3. #logo
  4. nav a
  5. h1, h2

Step 6 of 11Diagram

Which rule wins? Selector strength

Which rule wins? Selector strengthScale: Element: p, Class: .note, ID: #top, Inline style attribute.1101001,0001Element: p10Class: .note100ID: #top1,000Inline styleattribute
A simplified picture of specificity: a more specific selector beats a less specific one; if two are equal, the later rule wins.
Element: p

1

Class: .note

10

ID: #top

100

Inline style attribute

1,000

Step 7 of 11Learn

Colors and fonts that people can read

CSS accepts colors in several formats:

  • Names: tomato, navy, white. Easy, but limited to about 140 choices.
  • Hex codes: #ff6347. A hash and six hexadecimal digits, two each for red, green and blue, from 00 (none) to ff (full). #000000 is black, #ffffff is white, #ff0000 is pure red.
  • rgb(): rgb(255, 99, 71), the same idea with decimal numbers from 0 to 255.

Whatever you choose, keep contrast high. Light gray text on a white background looks stylish in a design tool and is miserable to read on a phone in sunlight. Accessibility guidelines ask for a contrast ratio of at least 4.5:1 for normal body text, and free online contrast checkers will test any pair of colors for you.

For fonts, font-family takes a list of choices in order, ending with a generic family as a backup:

body { font-family: Georgia, "Times New Roman", serif; }

If the visitor's device lacks Georgia, the browser tries the next one. For body text, a size around 16 to 18 pixels and a line-height of about 1.5 make long paragraphs comfortable. Two font families per site, one for headings and one for text, is plenty.

Step 8 of 11Practice · Chart

Contrast ratio of text colors on white

Tap the color that only just misses the 4.5 minimum.

Tap the bar that answers the question, then press Check.

Contrast ratio of text colors on whiteBar chart with 5 categories: Black #000: 21; Dark gray #222: 15.9; Gray #777: 4.48; Light gray #aaa: 2.32; Yellow #ff0: 1.07.0510152025Black #00021Dark gray #22215.9Gray #7774.48Light gray #aaa2.32Yellow #ff01.07
Guideline for normal body text: at least 4.5 (written 4.5:1). Ratios rounded.
Contrast ratio of text colors on white
ItemContrast ratio
Black #00021
Dark gray #22215.9
Gray #7774.48
Light gray #aaa2.32
Yellow #ff01.07

Step 9 of 11Practice · Fill in the blanks

Fill in the blanks

In the hex color #00ff00, the first two digits control , and since the middle pair is ff, the color is pure .

Step 10 of 11Note

Step 11 of 11Practice · Choose one

A paragraph has class="note". The style sheet says p { color: gray; } and later .note { color: green; }. What color is the paragraph?