Skip to content

Beginner · CSS basics · topic 14 of 64 · 20 min

How CSS works

Explanation

CSS is a list of rules that tell the browser how elements should look. Each rule has a selector that picks elements and declarations that set properties like colour and size. The best place for CSS is a separate .css file linked from every page.

The shape of a CSS rule

css
h1 {
  color: navy;
  font-size: 2rem;
}
/* selector { property: value; } */
One rule: the selector h1 and two declarations

The selector (h1) says which elements to style. Inside the curly braces is a declaration block. Each declaration is a property, a colon, a value and a semicolon. A comment is written between /* and */ and is ignored by the browser.

Think of it like this: Think of instructions to a painter: "All the doors (selector): paint them blue (property: value), and fit brass handles." The painter walks through the house and applies the instructions to every door.

Three ways to add CSS

WayExampleUse it for
Inline style<p style="color: red">Almost never; hard to reuse and override
<style> in the head<style> p { color: red; } </style>Small single pages, demos, tasks
External file<link rel="stylesheet" href="css/styles.css">Real websites: one file styles every page
html
<head>
  <meta charset="utf-8">
  <title>Home</title>
  <link rel="stylesheet" href="css/styles.css">
</head>
Linking an external stylesheet from the head

An external file is best because every page shares it. Change the brand colour once, and the whole site updates. The browser also caches (saves) the file, so the next page loads faster.

How the browser applies CSS

  1. It reads the HTML and builds the page tree.
  2. It reads all CSS: its own default styles, then yours.
  3. For every element it finds the rules that match.
  4. If rules disagree, the cascade decides which one wins (next topic).
  5. It draws the page.
Tip: Every browser has default styles: headings are big and bold, links are blue and underlined. Your CSS changes those defaults.
Careful: One missing semicolon or curly brace can silently break the rules after it. If a style does not apply, look in DevTools: a struck-through or warning-marked line often shows a typo.

Property names are in English with hyphens: background-color, font-size, text-align. Values can be words (center), numbers with units (16px), or colours (#1d4ed8).

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

How CSS works · 1/6

CSS = rules for the look

CSS tells the browser how things should look. It is a list of rules.

Prefer a video by a teacher? Find free videos on “css basics tutorial for beginners how css works” on YouTube ↗

In real life: Changing a festival theme

An online sweet shop wants an orange Diwali look for two weeks. Because all its pages link one styles.css, the developer changes the brand colour in that one file. Every page, from the home page to the checkout, shows the new colour after a refresh.

Practice (0/2 done)

Task 1 · Code · HTML

Inside the <style> tag, write a rule that makes all <p> elements have the colour "navy", and another rule that makes the <h1> text centred (text-align: center).

Task 2 · Code · HTML

This page should use an external stylesheet. Add a <link> in the head with rel="stylesheet" and href="css/styles.css".

Quick quiz

Question 1

1.In the rule p { color: red; }, what is "p"?

Question 2

2.Which way of adding CSS is best for a real multi-page website?

Question 3

3.What ends each declaration?

Question 4

4.How do you write a comment in CSS?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

In the rule p { color: red; }, what is "p"?

Answer: The selector

The selector picks which elements the rule applies to.

Which way of adding CSS is best for a real multi-page website?

Answer: An external .css file linked from each page

One shared file is easy to change and is cached by the browser.

What ends each declaration?

Answer: A semicolon

Each property: value pair ends with a semicolon.

How do you write a comment in CSS?

Answer: /* comment */

CSS comments go between /* and */.

Which language decides the colours and spacing of a page?Related

Answer: CSS

CSS controls the look: colours, sizes, spacing and layout.

From HTML, CSS and JavaScript: three jobs

Which selector styles a .field that contains an invalid input?Related

Answer: .field:has(input:invalid)

:has() matches an element based on its descendants.

From Modern CSS: variables, clamp, container queries and more

Which unit grows when a user raises the default text size in their browser?Related

Answer: rem

rem is relative to the root font size, which follows the user setting.

From Colours, backgrounds and units

Which selector picks every element with class="card"?Related

Answer: .card

A dot before the name selects by class.

From Selectors, specificity and the cascade

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 4 more topics in “CSS basics” before its exam: Selectors, specificity and the cascade, The box model, Colours, backgrounds and units, Typography: fonts and readable text.
  2. Exam: CSS basics · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, JavaScript basics. 64 topics left in the course before the final exam.

← Previous: Accessibility basics