HTML & CSS · 14-day planAll days
Start styling. Learn how a CSS rule is built, the three ways to add CSS to a page, how to target elements by type, class and id, and how to set colours, fonts, sizes and backgrounds.
By the end of today you can
- Read and write a CSS rule with a selector, properties and values
- Add CSS with a style element, a linked stylesheet or a style attribute
- Select elements by type, class and id
- Set colours, backgrounds, fonts and text sizes
- Write CSS comments and group selectors
Your pages have structure. Now let's make them look good. CSS describes how HTML elements should be displayed: colours, fonts, spacing, layout. The same HTML can look completely different with different CSS.
Your first CSS
Try color: tomato; or font-size: 30px;.
A rule has:
- a selector: which elements (
h1); - declarations inside
{ }: what to change, each asproperty: value;.
Forget a semicolon or a brace and the rule (or the next one) may be ignored, with no error message, so check your punctuation when something doesn't work.
Three ways to add CSS
<!-- 1. A linked stylesheet: the normal way for real sites -->
<head>
<link rel="stylesheet" href="style.css">
</head>
<!-- 2. A <style> element in the page: fine for one page (and for these lessons) -->
<style>
p { color: navy; }
</style>
<!-- 3. A style attribute on one element: avoid, except for quick tests -->
<p style="color: red;">Just this paragraph</p>With a linked stylesheet, every page shares one style.css: change it once and the whole site updates. In these lessons we'll use <style> so everything fits in one editor.
Selectors: type, class and id
| Selector | Matches | Example |
|---|---|---|
p | Every element of that type | p { } |
.note | Every element with that class | <p class="note"> |
#intro | The element with that id | <p id="intro"> |
h1, h2 | Several selectors (a group) | |
* | Every element | * { } |
An element can have several classes (class="note warning") and gets the styles of each. Classes are the workhorse of CSS: reusable and flexible. Use ids sparingly.
/* … */ is a CSS comment.
Colours and backgrounds
Colours can be names (teal, tomato), hex (#0f766e), rgb() or hsl(). Day 9 covers them properly.
Fonts and text
font-familylists fonts in order of preference; the browser uses the first one it has. End with a generic family:serif,sans-serif,monospaceorsystem-ui(the device's own font).font-size,font-weight(400 normal, 700 bold),font-style(italic).text-align: left, center, right.text-decoration: underline or none, often for links.
Recap
- A rule = selector
{ property: value; }. - Add CSS with a linked
.cssfile (best), a<style>element, or astyleattribute. - Select by type (
p), class (.note), id (#intro); group with commas. - Colours, backgrounds and fonts:
color,background-color,font-family,font-size,font-weight,text-align.
Tomorrow: more powerful selectors, and what happens when two rules disagree.
Knowledge check
Test what you learned
5 questions · pick an answer to see if you're right
- 1
In p { color: teal; }, what is "color"?
- 2
How do you select every element with class="card"?
- 3
What's the usual way to add CSS to a real website with many pages?
- 4
What does h1, h2 { color: navy; } do?
- 5
Which is NOT a valid way to write a colour in CSS?
Finished day 6?
Mark complete and continue to Day 7: Selectors & the CascadeWas this lesson helpful?
Need a breather? Take a two-minute break