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.
Welcome back!
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;
}h1is 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.
- 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
pstyles every<p>. - Class selector
.cardstyles every element withclass="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
#signupstyles the one element withid="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).
- p
- .warning
- #logo
- nav a
- h1, h2
Step 6 of 11Diagram
Which rule wins? Selector strength
- 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, from00(none) toff(full).#000000is black,#ffffffis white,#ff0000is 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.
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?
Lesson complete!