A CSS rule picks some elements with a selector, then sets properties:
h1 {
color: #08783f;
font-size: 2rem;
}
Read it as: for every h1, make the text green and 2rem big. Each property: value; pair is a declaration and ends with a semicolon.
Where CSS goes
Put it in a separate file, style.css, and link it in the <head>:
<link rel="stylesheet" href="style.css">
One stylesheet can style every page of your site — change a colour once, and it changes everywhere. (You'll see <style> blocks inside pages in the Try-it boxes; that's only for convenience.)
Selectors
| Selector | Selects | Example |
|---|---|---|
p | every <p> | p { line-height: 1.6; } |
.price | every element with class="price" | .price { font-weight: 700; } |
#cart | the element with id="cart" | #cart { position: sticky; } |
nav a | <a> elements inside <nav> | nav a { color: white; } |
a:hover | a link while the mouse is over it | a:hover { text-decoration: underline; } |
h1, h2 | both | h1, h2 { font-family: Georgia, serif; } |
<style>
body { font-family: system-ui, Arial, sans-serif; background: #f8faf9; color: #1d2b24; }
h1 { color: #08783f; }
.price { font-weight: 700; color: #c2410c; }
.badge { background: #fde68a; padding: 2px 8px; border-radius: 99px; font-size: 0.8rem; }
a { color: #2563eb; }
a:hover { color: #08783f; }
</style>
<h1>Mama Gold Rice 5kg</h1>
<p><span class="badge">Best seller</span></p>
<p class="price">₦9,500</p>
<p>Long-grain parboiled rice. <a href="#">See more rice</a></p>Colours
- Names:
red,white,teal. - Hex:
#08783f(most common — copy from any design tool or DevTools colour picker). - RGB with transparency:
rgb(8 120 63 / 0.5).
Keep a small palette: one main brand colour, one accent, and greys for text and borders.
Fonts and text
body {
font-family: system-ui, "Segoe UI", Roboto, Arial, sans-serif; /* fallbacks, in order */
font-size: 16px;
line-height: 1.6; /* space between lines — makes text readable */
}
h1 { font-weight: 800; letter-spacing: -0.02em; }
.small { font-size: 0.875rem; color: #5d6b66; }
For custom fonts, Google Fonts gives you a <link> to paste into your <head> and a font-family name to use.
The cascade
When two rules set the same property on an element, the more specific selector wins (#id beats .class beats element); if equal, the later rule wins. When a style “isn't working”, inspect the element in DevTools: overridden rules appear crossed out.
Check yourself
Answer in your head first, then open each question to see the answer and why.
Which selector targets every element with class="price"?
- A .price
- B #price
- C price
- D *price
Show the answer
A) .price — A dot means class.
Two rules with equal specificity set different colours on the same element. Which wins?
- A The one that comes later
- B The first one
- C Neither
- D The shorter one
Show the answer
A) The one that comes later — With equal specificity, the later rule wins.
How do you connect an external stylesheet?
- A <link rel="stylesheet" href="style.css"> in the head
- B <script src="style.css">
- C <style href="style.css">
- D <css>style.css</css>
Show the answer
A) <link rel="stylesheet" href="style.css"> in the head — The link element loads a stylesheet.
More free lessons: What front-end developers do · Set up VS Code, Chrome and Live Server · Text, links and images · Mobile-first design and media queries · Variables, data types and operators · The DOM: find and change page elements · Git and GitHub