Chapter 3
CSS
CSS (Cascading Style Sheets) controls the visual presentation of HTML. It sets colours, fonts, spacing, layout, and how a page looks on different screen sizes.
How the cascade works
The “cascading” part of CSS describes how the browser decides which style wins when two rules target the same element. It works through three criteria in order:
- Origin: browser defaults lose to author styles, which lose to inline styles.
- Specificity: a more specific selector beats a less specific one (ID beats class beats element).
- Source order: if two rules have equal specificity, the last one in the file wins.
Understanding the cascade helps you predict which styles apply and fix conflicts without guessing.
Before you start
You should be comfortable with basic HTML before learning CSS:
- How to write HTML elements (
<p>,<div>,<h1>, etc.) - How attributes work (
class,id,src,href) - The difference between block elements (
<div>,<p>) and inline elements (<span>,<a>)
If those feel unfamiliar, spend some time with the HTML section first.
Where to start
Work through these pages in order:
- Basics : selectors, the cascade, specificity, and the box model
- Colors : named colours, hex, rgb(), hsl(), and modern oklch()
- Fonts : font families, sizes, weights, and loading web fonts
- Text : alignment, spacing, decoration, and text overflow
- Border : border styles, radius, and box-shadow
- Margin and Padding : spacing inside and outside elements
- Position : static, relative, absolute, fixed, and sticky
- Flexbox : one-dimensional layout for rows and columns
- Grid : two-dimensional layout for complex page structures
- Pseudo-classes and Pseudo-elements : hover, focus, nth-child, ::before, ::after
- Responsive Design : media queries, viewport, and fluid units
- Transform : translate, rotate, scale, and CSS transitions
- Custom Properties : CSS variables for reusable values and theming