CSS basics: colours, fonts and your first stylesheet
Write your first CSS rules, link an external stylesheet to your HTML, and change text colour, background colour, fonts, font sizes, line spacing and alignment.
🎯 By the end of this lesson you will be able to:
- Read and write a CSS rule: selector, property and value
- Link an external stylesheet to an HTML page
- Style text with colours, fonts, sizes, line height and alignment
Outline
What you'll learn and why it matters
Your pages so far use the browser's default look: black Times New Roman text on white. CSS lets you choose colours, fonts, spacing and layout. One stylesheet can style every page of a website, so changing a colour in one place changes it everywhere.
Your first CSS rule
A CSS rule has a selector that picks elements, and a declaration block in curly braces with one or more declarations: a property, a colon, a value and a semicolon.
h1 {
color: navy;
font-size: 2rem;
}Show the HTML this CSS styles
<h1>Green Leaf Café</h1>
<p>Fresh snacks and the best chai in town.</p>
<p>Open every day from 8am to 8pm.</p>Browser preview
The selector h1 picks every <h1> on the page. The two paragraphs are not affected, because the rule only selects h1 elements.
Link a stylesheet to your page
The best place for CSS is its own file, usually called styles.css, linked from the <head> of every page. Let's set it up:
- In VS Code, create a new file called
styles.cssin yourmy-websitefolder, next toindex.html. - Add the
<link>line below inside the<head>ofindex.html. - Put your CSS rules in
styles.css, save both files and refresh the browser.
<head>
<meta charset="utf-8">
<title>Green Leaf Café</title>
<link rel="stylesheet" href="styles.css">
</head>| Where CSS can go | Example | When to use it |
|---|---|---|
| External file (best) | <link rel="stylesheet" href="styles.css"> | Almost always: one file styles every page |
<style> in the head | <style> h1 { color: navy; } </style> | Quick tests, or a single-page example |
Inline style attribute | <p style="color: red;"> | Avoid: hard to find and change later |
Colours
color sets the text colour and background-color the background. You can write colours in several ways:
| Way | Example | Notes |
|---|---|---|
| Named colour | navy, white, tomato | About 140 names; easy to read |
| Hex code | #1d4ed8 | Red, green and blue as two hex digits each; the most common in design tools |
| rgb() | rgb(29 78 216) | Red, green, blue from 0 to 255 |
body {
background-color: #f0fdf4;
color: #14532d;
}
h1 {
color: #166534;
}Show the HTML this CSS styles
<h1>Green Leaf Café</h1>
<p>Fresh snacks and the best chai in town.</p>
<p>Open every day from 8am to 8pm.</p>Browser preview
Fonts and text
font-family chooses the typeface. Give a list: the browser uses the first one installed on the visitor's device, and the last item should be a general family such as sans-serif (no little feet on the letters) or serif.
body {
font-family: "Segoe UI", Arial, sans-serif;
font-size: 1.125rem;
line-height: 1.6;
}
h1 {
font-size: 2.5rem;
font-weight: 700;
text-align: center;
}
p {
max-width: 40ch;
}Show the HTML this CSS styles
<h1>Green Leaf Café</h1>
<p>Fresh snacks and the best chai in town.</p>
<p>Open every day from 8am to 8pm.</p>Browser preview
| Property | Example | What it does |
|---|---|---|
font-family | Arial, sans-serif | The typeface, with backups |
font-size | 1.125rem | Text size. 1rem is the browser's base size (usually 16px) |
font-weight | 700 or bold | Thickness: 400 is normal, 700 is bold |
line-height | 1.6 | Space between lines, as a multiple of the font size. 1.5 to 1.7 is comfortable for paragraphs |
text-align | center | left, center, right or justify |
max-width | 40ch | Limits line length. ch is about one character wide |
Some properties are inherited: set font-family or color on body and every element inside uses it too, unless a more specific rule says otherwise. That is why we style body first.
Comments in CSS
/* Main colours for the whole site */
body {
color: #14532d; /* dark green text */
}CSS comments go between /* and */. They are for people reading the code; the browser ignores them.
Practice
Guided exercise
Style a profile
Style the profile page: a light blue background for the page, dark blue text, a sans-serif font, a centred heading in a larger size, and comfortable line spacing.
- Write a
bodyrule withbackground-color,color,font-familyandline-height. - Write an
h1rule withfont-sizeandtext-align: center. - Check the preview: every paragraph should inherit the font and colour from
body.
Show the solution
body {
background-color: #eff6ff;
color: #1e3a8a;
font-family: Arial, sans-serif;
line-height: 1.6;
}
h1 {
font-size: 2.25rem;
text-align: center;
}Show the HTML this CSS styles
<h1>Meera Iyer</h1>
<p>Student, artist and future web developer.</p>
<p>I love painting, cricket and building websites.</p>Browser preview
How to get there: Colours and fonts set on body are inherited by everything inside it, so you only write them once. The h1 rule then changes just the heading.
Your turn
Two colours for two elements
On the café page, make the heading white on a dark green background (#166534), and make the paragraphs dark grey (#374151).
Show the solution
h1 {
color: white;
background-color: #166534;
}
p {
color: #374151;
}Show the HTML this CSS styles
<h1>Green Leaf Café</h1>
<p>Fresh snacks and the best chai in town.</p>
<p>Open every day from 8am to 8pm.</p>Browser preview
How to get there: Each selector picks different elements. The heading's background fills its whole width because headings are block elements; you will control that width in lesson 8.
Common mistakes
Missing semicolon between declarations
You see: The declaration after it is ignored
Fix: End every declaration with ;.
Wrong path in the link's href, or styles.css saved in another folder
You see: None of your CSS works
Fix: Put styles.css next to index.html and check the spelling. Press F12 and look at the Console for a file-not-found message.
Writing colour (British spelling)
You see: The colour does not change
Fix: CSS uses American spelling: color and background-color.
Putting a space in a value like 1.5 rem
You see: The size is ignored
Fix: Numbers and units go together: 1.5rem, 16px.
Forgetting the closing }
You see: Every rule after it stops working
Fix: Check each rule has { and }. VS Code highlights the matching brace when you click next to one.
Recap
- A rule is a selector plus declarations:
selector { property: value; }. - Put CSS in
styles.cssand link it with<link rel="stylesheet" href="styles.css">. - Colours can be names, hex codes or rgb(); keep strong contrast between text and background.
- Style
bodyfirst: fonts and colours are inherited. Useremfor font sizes and 1.5 to 1.7 line height.
Key terms
- Selector
- The part of a CSS rule that chooses which elements to style.
- Declaration
- One property: value; pair inside a rule.
- Inheritance
- Some properties, such as color and font-family, pass from a parent element to its children.
- rem
- A unit equal to the root font size, usually 16px, that follows the user's text-size setting.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. In `p { color: green; }`, what is `color`?
2. Which line links an external stylesheet called styles.css?
3. You set `font-family` on body. Why do the paragraphs use that font too?
4. Why is `rem` a good unit for font sizes?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.