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.

Beginner 3 min readLesson 2 of 14

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:

CSS
.card > h2:hover {          /* selector */
  color: navy;              /* declaration: property: value */
  margin-block: 0 0.5rem;
}

Selectors

SelectorMatches
pAll <p> elements (type)
.cardElements with class="card"
#mainThe element with id="main"
[type="email"]Attribute match
nav aa anywhere inside nav (descendant)
ul > liDirect children
h2 + p / h2 ~ pNext sibling / any later sibling
a:hover, :focus-visible, li:nth-child(2n), :not(.x)Pseudo-classes: state and position
p::first-line, .req::afterPseudo-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:

  1. Origin and importance: user-agent defaults < author styles; !important flips the order.
  2. Cascade layers (@layer): later layers beat earlier ones.
  3. Specificity: the more specific selector wins.
  4. Order of appearance: the later rule wins.

Specificity

Specificity is compared as a triple (IDs, classes/attributes/pseudo-classes, types/pseudo-elements):

SelectorSpecificity
p(0, 0, 1)
.card p(0, 1, 1)
nav .link:hover(0, 2, 1)
#main .card(1, 1, 0)
style="…" inlineBeats all selectors
CSS
#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.

CSS
@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:

marginspace outside the border (transparent; vertical margins can collapse)borderthe edgepaddingspace inside the border (shows the background)contentwidth × height

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:

CSS
*, *::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

UnitRelative toUse for
pxCSS pixel (not device pixel)Borders, fine details
remRoot font size (usually 16px)Font sizes, spacing — respects user font settings
emCurrent element's font sizePadding that scales with text
%Parent's sizeFluid widths
vw / vh, dvhViewport (dynamic viewport on mobile)Full-screen sections
chWidth of "0"Line length: max-width: 65ch
frFree space in a gridGrid tracks

Fluid values without media queries: font-size: clamp(1rem, 0.8rem + 1vw, 1.5rem);

Colour and typography

CSS
: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)

CSS
: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

CSS
.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, @property for typed variables.
Key takeaways
  • 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.