Beginner · CSS basics · topic 14 of 64 · 20 min
How CSS works
Explanation
The shape of a CSS rule
h1 {
color: navy;
font-size: 2rem;
}
/* selector { property: value; } */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.
Three ways to add CSS
| Way | Example | Use 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 |
<head>
<meta charset="utf-8">
<title>Home</title>
<link rel="stylesheet" href="css/styles.css">
</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
- It reads the HTML and builds the page tree.
- It reads all CSS: its own default styles, then yours.
- For every element it finds the rules that match.
- If rules disagree, the cascade decides which one wins (next topic).
- It draws the page.
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 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)
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).
This page should use an external stylesheet. Add a <link> in the head with rel="stylesheet" and href="css/styles.css".
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Selectors, specificity and the cascadeCSS basics · same module
- The box modelCSS basics · same module
- Colours, backgrounds and unitsCSS basics · same module
- Modern CSS: variables, clamp, container queries and moreCSS layout and responsive design
- Browser, server, URL and HTTPHow the web works
- CSS GridCSS layout and responsive design
Questions people ask about this
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.
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.
Which selector picks every element with class="card"?Related
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
- 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.
- Exam: CSS basics · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, JavaScript basics. 64 topics left in the course before the final exam.