Intro to CSS: Colours, Fonts and Your First Stylesheet
HTML gives the content. CSS makes it look good. Let us style our first page.
How a CSS rule looks
h1 {
color: green;
font-size: 40px;
}h1is the selector: which elements to style.coloris the property: what to change.greenis the value: how to change it.- Every line ends with a semicolon
;.
Where to write CSS
There are three places. The best one is a separate file.
<link rel="stylesheet">.<!-- 1. Separate file (best). Put this inside <head> -->
<link rel="stylesheet" href="style.css">
<!-- 2. Inside a <style> tag in <head> (ok for small tests) -->
<style>
p { color: gray; }
</style>
<!-- 3. Inline on one element (avoid) -->
<p style="color: red;">Hello</p>With one style.css file, all your pages share the same design. Change it once, and every page updates.
Colours
p { color: tomato; } /* colour name */
h1 { color: #2563eb; } /* hex code */
h2 { color: rgb(0, 168, 107); } /* red, green, blue */
div { background-color: #f1f4f8; } /* background */Hex codes are the most common. Many free tools online help you pick colours. Keep good contrast: dark text on a light background is easiest to read.
Backgrounds
.hero {
background-color: #1f2937;
background-image: url("images/mountains.jpg");
background-size: cover; /* fill the box */
background-position: center; /* keep the middle visible */
color: white;
}background-size: cover makes the image fill the whole box, cutting off edges if needed. Always set a background-color too. It shows while the image loads, and it keeps text readable if the image fails.
Borders, round corners and shadows
.card {
border: 1px solid #e5e7eb; /* width, style, colour */
border-radius: 12px; /* round corners */
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
padding: 20px;
}For shadows, small and soft looks best. The numbers mean: move right 0px, move down 4px, blur 12px, and a black colour that is only 8% visible.
Text styles
body {
font-family: Arial, sans-serif;
font-size: 18px;
line-height: 1.6;
}
h1 {
font-weight: bold;
text-align: center;
}Centered bold heading
Body text with comfortable line spacing is easy to read.
Units: px, rem and %
| Unit | Meaning | Good for |
|---|---|---|
px | Fixed pixels | Borders, small details |
rem | Times the main font size (usually 16px). 2rem = 32px | Font sizes, spacing |
% | Part of the parent's size | Widths |
vw / vh | Part of the screen width / height | Full-screen sections |
Save colours in variables
If your main colour is used in 30 places, changing it means 30 edits. CSS variables fix this. Define the value once, then use it everywhere.
:root {
--main: #2563eb;
--text: #1f2937;
}
a { color: var(--main); }
.btn { background: var(--main); color: white; }
body { color: var(--text); }Now change --main in one place and the whole site updates. Variable names start with two dashes.
The cascade (who wins?)
CSS means Cascading Style Sheets. If two rules say different things, the later rule wins (when they are equally specific).
p { color: blue; }
p { color: red; } /* this one wins: text is red */Some properties also pass down from parent to child. For example, if you set font-family on body, all text inside uses it. This is called inheritance.
Try CSS live in the browser
Right-click any element and choose Inspect. On the right side, the Styles panel shows every CSS rule for that element.
- Click a value to change it, and watch the page update.
- Untick the checkbox next to a line to turn it off.
- Crossed-out lines are rules that lost to a stronger rule.
Nothing you change here is saved. It is a safe place to experiment, then copy the code you like into your style.css.
Mini project: style your About page
Create style.css next to about.html, link it in the head, and add:
:root { --main: #2563eb; }
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #1f2937;
background: #f6f7f9;
max-width: 700px;
margin: 40px auto;
padding: 0 16px;
}
h1 { color: var(--main); }
h2 { border-bottom: 2px solid #e5e7eb; padding-bottom: 6px; }Reload the page. With 15 lines of CSS, the plain page now looks clean and readable.
Common mistakes
| Problem | Fix |
|---|---|
| No style works at all | Check the href in <link>. Is style.css really in that folder? |
| One rule and the rules after it stop working | A missing ; or }. Check the line before it. |
| A property does nothing | Check spelling. CSS uses American spelling: color, not colour. |
| You changed CSS but see the old design | The browser kept an old copy. Press Ctrl + Shift + R to reload fully. |
Practice time
- Make a
style.cssfile and link it to your page. - Give the body a light background colour and dark text.
- Make all h2 headings green and 28px.
- Add a
--maincolour variable and use it for links and headings. - Make a card with a border, round corners and a soft shadow.
Quick summary
- CSS rule = selector + property + value.
- Use one external
style.cssfor the whole site. - Later rules win; some styles pass down to children.
Comments in CSS
CSS comments start with
/*and end with*/. Use them to label parts of your stylesheet so you can find things later.