Show contents
Chapter 2 of 4 · 15 min
CSS: adding style
By the end of this chapter, you'll be able to
- Write a CSS rule with a selector, properties and values
- Target elements by tag, by class and by id
- Explain the box model: content, padding, border, margin
- Predict which rule applies when several contradict each other
A CSS rule
CSS (Cascading Style Sheets) is written as rules. Each rule has a selector (which elements?) and declarations property: value; between braces:
h1 {
color: #0b3b7a; /* text colour */
font-size: 2rem; /* size: twice the base size */
}
.price {
font-weight: bold; /* every element with class="price" */
}
#basket {
border: 2px solid #f28c28; /* the single element with id="basket" */
}
- a tag name (
h1) targets every tag of that type; .pricetargets the elements that have the attributeclass="price": a class can be reused anywhere;#baskettargets the element that hasid="basket": an id is unique in the page.
The box model
For the browser, each element is a box made of layers, from the inside out: the content, the padding (inner space), the border, then the margin, which keeps it away from its neighbours.
.card {
padding: 16px; /* space between the text and the border */
border: 1px solid #e2dccf;
margin: 24px 0; /* 24px top and bottom, 0 left and right */
border-radius: 12px; /* rounded corners */
background: #fdebd8;
}
The cascade: who wins?
When several rules give a different value to the same property, the browser applies priority rules. The two main ones: the most specific rule wins (an id beats a class, which beats a tag), and with equal specificity, the last one written wins.
Result: specificity comes before the order of writing.
Text description of the animation
- A single heading, with the class important. Three rules want to choose its colour.
- Rule 1: the h1 tag selector gives black.
- Rule 2: a class is more specific than a tag. orange wins.
- Rule 3: written later, but less specific. It doesn't beat the class.
- Result: specificity comes before the order of writing.
Your turn
Mini-lab: a product cardHTML · CSS
Preview
The preview is isolated from the page and runs no scripts · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Worked exercise
Worked exercise: the button
Write the CSS for a class .button: orange background #f28c28, bold navy text #061a36, 12px of inner space at the top and bottom and 20px on the left and right, no border, with very rounded corners (999px).
Hint 1
padding: 12px 20px; applies the first value to the top and bottom, the second to the left and right.Hint 2
border: none; removes the border.Show the solution and common mistakes
.button {
background: #f28c28;
color: #061a36;
font-weight: bold;
padding: 12px 20px;
border: none;
border-radius: 999px;
}
Common mistakes:
- Writing
button { … }without the dot: that targets the<button>tags, not the elements with the class. - Choosing white text on an orange background: the contrast is too low to be read easily. Navy on orange is much more readable.
Summary
- 1A CSS rule = a selector + declarations property: value;.
- 2You target by tag, by class (.name) or by id (#name).
- 3Each element is a box: content, padding, border, margin.
- 4The most specific rule wins; with equal specificity, the last one written wins.
- 5CSS silently ignores what it doesn't understand: check the spelling.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.