In this lesson you will
- Add CSS to a page with a stylesheet, a style element or a style attribute, and know which to use
- Write type, class, ID, attribute, descendant and child selectors
- Predict which rule wins using specificity and source order
- Tell inherited properties from those that are not inherited
A rule: selector, property and value
Every CSS rule has the same shape. The selector says which elements to style. Inside the curly braces come one or more declarations, each a property and a value separated by a colon and ending with a semicolon. In p { color: #333; line-height: 1.6; } the selector is p, and the rule sets two properties on every paragraph.
There are three places to write CSS. A separate file linked in the head with <link rel="stylesheet" href="styles.css"> is the normal choice for real sites: one file styles every page and the browser caches it. A <style> element in the head is handy for a single page or a lesson like this one. The style attribute on one element works too, but it mixes design into the content and is hard to override, so keep it for rare cases.
If a declaration is invalid, for example a misspelled property, the browser skips just that declaration and carries on. Nothing breaks and no error appears on the page, which is why the browser developer tools are your best friend: they show every rule that applies to an element and cross out the ones that lost or were invalid.
Console
- Console messages, errors, form data and clicked links appear here.
Change #0f766e to another colour, or fix the spelling of colr and watch the paragraph turn red.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
Should I use an external stylesheet or a style element?
For a real site, use an external .css file linked from every page: one change updates the whole site, and the browser downloads it once. A <style> element is fine for one-page demos, emails and lessons.
Class or ID: which should I style with?
Classes. They can be reused and keep specificity low. Keep IDs for things that need a unique name, such as link targets (href="#prices") and labels in forms.
Why is my CSS not working?
Check four things in order: the file is linked (no 404 in developer tools), the selector really matches the element, the punctuation is complete, and no more specific rule overrides it. Developer tools show all four when you inspect the element.
What does cascading mean in Cascading Style Sheets?
It is the process that combines rules from many places (browser defaults, your stylesheets, inline styles) and settles every conflict in a fixed order: importance, then specificity, then source order.
Is CSS a programming language?
It is a style sheet language: you describe how things should look and the browser works out the result. Modern CSS has variables, functions such as calc() and conditions such as media queries, but you do not write step-by-step instructions as in JavaScript.
Sources
Facts in this lesson were checked against these official pages on 21 October 2026.
- MDN: What is CSS?
- MDN: Introduction to the CSS cascade
- MDN: Specificity
- MDN: Inheritance
- W3C: CSS Cascading and Inheritance Level 5
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.