CSS Fundamentals
Selectors, the cascade, specificity and inheritance, the box model, units, colours and typography, custom properties, cascade layers, transitions and animations, and modern CSS features.
CSS (Cascading Style Sheets) controls how HTML looks: colour, typography, spacing, layout and motion. A stylesheet is a list of rules, each a selector plus declarations:
.card > h2:hover { /* selector */
color: navy; /* declaration: property: value */
margin-block: 0 0.5rem;
}Selectors
| Selector | Matches |
|---|---|
p | All <p> elements (type) |
.card | Elements with class="card" |
#main | The element with id="main" |
[type="email"] | Attribute match |
nav a | a anywhere inside nav (descendant) |
ul > li | Direct children |
h2 + p / h2 ~ p | Next sibling / any later sibling |
a:hover, :focus-visible, li:nth-child(2n), :not(.x) | Pseudo-classes: state and position |
p::first-line, .req::after | Pseudo-elements: parts of elements |
.card:has(img) | Parent selector: cards that contain an image |
:is(h1, h2, h3), :where(…) | Grouping (:where adds zero specificity) |
The cascade
When several rules set the same property on an element, the cascade decides which wins, in this order:
- Origin and importance: user-agent defaults < author styles;
!importantflips the order. - Cascade layers (
@layer): later layers beat earlier ones. - Specificity: the more specific selector wins.
- Order of appearance: the later rule wins.
Specificity
Specificity is compared as a triple (IDs, classes/attributes/pseudo-classes, types/pseudo-elements):
| Selector | Specificity |
|---|---|
p | (0, 0, 1) |
.card p | (0, 1, 1) |
nav .link:hover | (0, 2, 1) |
#main .card | (1, 1, 0) |
style="…" inline | Beats all selectors |
#sidebar a { color: gray; } /* (1,0,1) */
.nav-link { color: blue; } /* (0,1,0) — loses, even though it comes later */Specificity wars (!important everywhere, long selector chains) make CSS unmaintainable. Keep specificity low and flat — mostly single classes — which is what methodologies like BEM and utility frameworks like Tailwind effectively do.
@layer reset, base, components, utilities; /* explicit priority, independent of specificity */
@layer components { .btn { padding: 0.5rem 1rem; } }
@layer utilities { .p-0 { padding: 0; } } /* wins over .btn regardless of specificity */Inheritance
Some properties inherit from parent to child (mostly text-related: color, font-*, line-height, visibility); others don't (margin, border, background, width). Control it with inherit, initial, unset and revert.
The box model
Every element is a rectangular box:
By default (box-sizing: content-box), width sets only the content, so padding and border are added — a 200px box with 20px padding becomes 240px wide. Almost everyone resets this:
*, *::before, *::after { box-sizing: border-box; } /* width includes padding and border */Margin collapsing: adjacent vertical margins of block elements merge into the larger one (not inside flex or grid containers).
display decides how a box participates in layout: block (full width, stacks vertically), inline (flows in text; width/height ignored), inline-block, flex, grid, none (CSS layout).
Units
| Unit | Relative to | Use for |
|---|---|---|
px | CSS pixel (not device pixel) | Borders, fine details |
rem | Root font size (usually 16px) | Font sizes, spacing — respects user font settings |
em | Current element's font size | Padding that scales with text |
% | Parent's size | Fluid widths |
vw / vh, dvh | Viewport (dynamic viewport on mobile) | Full-screen sections |
ch | Width of "0" | Line length: max-width: 65ch |
fr | Free space in a grid | Grid tracks |
Fluid values without media queries: font-size: clamp(1rem, 0.8rem + 1vw, 1.5rem);
Colour and typography
:root {
color-scheme: light dark;
--text: oklch(25% 0.02 260); /* perceptual colour space */
--brand: #4f46e5;
}
body {
font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
font-size: 1rem;
line-height: 1.6;
color: var(--text);
}
a { color: var(--brand); }
a:hover { color: color-mix(in oklch, var(--brand), black 20%); }Custom properties (CSS variables)
:root { --radius: 8px; --gap: 1rem; --bg: white; --fg: #111; }
@media (prefers-color-scheme: dark) {
:root { --bg: #0b0b0f; --fg: #eee; } /* theming by redefining variables */
}
.card { border-radius: var(--radius); padding: var(--gap); background: var(--bg); color: var(--fg); }
.card.compact { --gap: 0.5rem; } /* scoped override, inherited by children */Unlike preprocessor variables (Sass), custom properties are live: they cascade, inherit, and can be changed at runtime with JavaScript (el.style.setProperty("--gap", "2rem")).
Transitions and animations
.button {
transition: background-color 150ms ease, transform 150ms ease;
}
.button:hover { background-color: var(--brand); transform: translateY(-1px); }
@keyframes spin { to { transform: rotate(1turn); } }
.spinner { animation: spin 1s linear infinite; }
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}Animate transform and opacity where possible: they can run on the compositor without re-layout or repaint (Browser rendering).
Modern CSS worth knowing
- Nesting:
.card { & h2 { … } &:hover { … } }— native, no preprocessor. - Container queries: style a component by its container's width, not the viewport (
@container (min-width: 400px)). :has(): the long-awaited parent selector.- Logical properties:
margin-inline,padding-block— work for right-to-left languages. - View transitions, scroll-driven animations, anchor positioning,
@propertyfor typed variables.
- Rules = selector + declarations; the cascade resolves conflicts by origin, layer, specificity, then order.
- Specificity is an (ID, class, type) triple; keep it low and flat; @layer orders whole groups.
- The box model: content, padding, border, margin; use border-box sizing.
- Prefer rem for sizing, ch for line length, clamp for fluid values.
- Custom properties cascade and can change at runtime — ideal for theming.
- Animate transform and opacity, and respect prefers-reduced-motion.