Chapter 4 of 15

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

CSS rule h1 color green with labels selector, property and value
Every CSS rule has the same three parts.
CSS
h1 {
  color: green;
  font-size: 40px;
}
  • h1 is the selector: which elements to style.
  • color is the property: what to change.
  • green is the value: how to change it.
  • Every line ends with a semicolon ;.

Comments in CSS

CSS comments start with /* and end with */. Use them to label parts of your stylesheet so you can find things later.

CSS
/* ===== Header ===== */
header { background: #1f2937; }

/* ===== Buttons ===== */
.btn { padding: 10px 20px; }

Where to write CSS

There are three places. The best one is a separate file.

One style.css file with arrows to three HTML pages
One stylesheet, linked from every page with <link rel="stylesheet">.
HTML
<!-- 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

CSS
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

CSS
.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

CSS
.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;
}
Result in the browser
A simple card with a soft shadow.

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

CSS
body {
  font-family: Arial, sans-serif;
  font-size: 18px;
  line-height: 1.6;
}
h1 {
  font-weight: bold;
  text-align: center;
}
Result in the browser

Centered bold heading

Body text with comfortable line spacing is easy to read.

Units: px, rem and %

UnitMeaningGood for
pxFixed pixelsBorders, small details
remTimes the main font size (usually 16px). 2rem = 32pxFont sizes, spacing
%Part of the parent's sizeWidths
vw / vhPart of the screen width / heightFull-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.

CSS
: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).

A tree: body passes its font down to h1, p, ul and li
Some styles (like fonts and text colour) pass down from parent to children. This is called inheritance.
CSS
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:

CSS
: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

ProblemFix
No style works at allCheck the href in <link>. Is style.css really in that folder?
One rule and the rules after it stop workingA missing ; or }. Check the line before it.
A property does nothingCheck spelling. CSS uses American spelling: color, not colour.
You changed CSS but see the old designThe browser kept an old copy. Press Ctrl + Shift + R to reload fully.

Practice time

  1. Make a style.css file and link it to your page.
  2. Give the body a light background colour and dark text.
  3. Make all h2 headings green and 28px.
  4. Add a --main colour variable and use it for links and headings.
  5. Make a card with a border, round corners and a soft shadow.

Quick summary

  • CSS rule = selector + property + value.
  • Use one external style.css for the whole site.
  • Later rules win; some styles pass down to children.