Lesson 11 of 52CSS foundations
CSS Basics
Learn what CSS is, how a rule is built, the three ways to add it, how the cascade decides a winner and how to inspect styles in devtools.
HTML says what things are: this is a heading, this is a list, this is a button. CSS (Cascading Style Sheets) says how they look: colours, fonts, spacing, sizes and layout. Keeping the two apart is one of the best ideas on the web. You can restyle an entire site by changing one file, without touching a single heading or paragraph, and the same HTML can look different on a phone, a big monitor and a printed page. This lesson covers the grammar of CSS, where it lives, and how the browser decides which rule wins when two disagree.
Your first rule
CSS is a list of rules. Each rule picks some elements and says how they should look.
h1 {
color: darkgreen;
font-size: 40px;
}Read it out loud: "For every h1, set the colour to dark green and the font size to 40 pixels." Every part has a name:
A rule (also called a ruleset) is one selector plus a block of declarations in curly braces.
The punctuation matters. A missing colon or semicolon does not crash anything; the browser just ignores that declaration (and sometimes the next one) and carries on. That forgiving behaviour is handy, but it means typos fail silently. When a style "does nothing", check the punctuation first.
Preview
Try these: change darkgreen to tomato. Change 40px to 64px. Add text-transform: uppercase; inside the h1 rule. Then delete the semicolon after darkgreen and watch the colour (and the font size after it) stop working, because the browser can no longer tell where one declaration ends.
The browser already has styles
Even a page with no CSS at all is not unstyled. Headings are big and bold, links are blue and underlined, lists have bullets, and the body has a small margin. These come from the user agent stylesheet, the default CSS every browser ships with. Your CSS does not replace it; it is layered on top, and your rules win where they disagree. That is why many projects start with a small reset or base stylesheet that evens out the defaults, for example removing the body margin. You will see one in the CSS architecture lesson. In devtools, default rules appear at the bottom of the Styles panel labelled "user agent stylesheet".
A few selectors to start with
You will meet many selectors in the next lesson. Three cover most early needs:
| Selector | Example | Matches |
|---|---|---|
| Type | p | Every p element |
| Class | .note | Every element with class="note" |
| ID | #intro | The one element with id="intro" |
Classes are the workhorse. An element can have several (class="note warning"), and many elements can share one. You can also group selectors with a comma to give them the same styles: h1, h2, h3 { font-family: Georgia, serif; }.
font-size: 18px;, what is font-size?Comments
CSS comments start with /* and end with */. The browser ignores everything between them. Use them to explain why, not what, and to switch a declaration off while experimenting.
/* Brand colours. Keep in sync with the logo file. */
.button {
background: #1565c0;
/* border-radius: 999px; */
}There is no // single-line comment in plain CSS. Writing one breaks the next rule in confusing ways, a common trap for people coming from JavaScript.
Three ways to add CSS
1. Inline styles go in a style attribute on one element:
<p style="color: red; font-weight: bold;">Sale ends today</p>2. A style element goes in the head (or anywhere, as in the live editors on this site) and applies to the whole page:
<style>
p { color: #333; }
</style>3. An external stylesheet is a separate .css file linked from the head:
<link rel="stylesheet" href="styles.css">- my-site/
- index.htmllinks styles.css
- about.htmllinks styles.css
- contact.htmllinks styles.css
- styles.cssevery page's look in one place
External files win for real sites, for three reasons. One change, every page: fix a colour in styles.css and all pages update. Caching: the browser downloads the file once and reuses it on every page, so later pages load faster. Separation: HTML stays about content, and the CSS file can be organised on its own. Inline styles are the opposite: repeated on every element, impossible to reuse, and they beat almost everything else in the cascade, which makes them painful to override later. Use a style element for quick experiments, single-page demos, or small bits of critical CSS.
Match each way of adding CSS to its best use.
The cascade in one paragraph
What happens when two rules disagree about the same property on the same element? The browser runs the cascade, a fixed set of tie-breakers. In short: importance first (!important beats normal, though you should avoid it), then where the rule came from (your styles beat the browser's defaults), then specificity (an ID selector beats a class, a class beats a type selector, and an inline style attribute beats them all), and finally source order: if everything else is equal, the rule written later wins. A separate idea, inheritance, means some properties like color and font-family flow down from a parent to its children unless a child sets its own. The next lesson scores specificity in detail.
Preview
The first paragraph is purple, not blue, because the second p rule comes later. The second is green: .tip is more specific than p, so source order does not even come into it. The third is orange because inline styles beat both. Now swap the order of the two p rules and see only the first paragraph change. Then add id="hero" to the first paragraph and a rule #hero { color: crimson; } at the top of the style element, and see it win despite coming first.
What colour is the text?
<style>
.price { color: red; }
span { color: blue; }
</style>
<span class="price">9 EUR</span>Inspecting styles in devtools
Every modern browser has developer tools. Right-click any element and choose Inspect (or press F12, or Cmd+Option+I on a Mac). The Elements panel shows the HTML; select an element and the Styles panel shows every rule that applies to it, most specific at the top.
- A declaration that lost the cascade is shown with a line through it. That is usually the answer to "why is my colour not working".
- Click a value to edit it live, or tick the checkbox next to a declaration to switch it off. Nothing is saved to your file, so experiment freely.
- The Computed tab shows the final value of every property after the cascade, and which rule it came from.
- Colour values have a small swatch. Click it for a colour picker, which also shows contrast information.
Your button text should be white but shows black. Put a sensible debugging routine in order.
- Tap the steps below in the right order.
- CSS
- Cascading Style Sheets: the language that describes how HTML elements look.
- rule
- A selector plus a block of declarations in curly braces.
- selector
- The part of a rule that picks which elements it applies to.
- declaration
- A property and a value separated by a colon and ended by a semicolon.
- external stylesheet
- A separate .css file linked with link rel='stylesheet', shared and cached across pages.
- cascade
- The algorithm that decides which declaration wins when several target the same property.
- specificity
- A score based on the selector's parts. IDs beat classes, classes beat type selectors.