CSS Fundamentals

Control how pages look with selectors, the cascade, the box model, units and custom properties.

Beginner · about 8 hours · difficulty 2/5

Learn first: Semantic HTML

Even if you later use Tailwind, you are still writing CSS. Utility classes only make sense once you understand what they compile to.

The core model

  • Selectors and specificity: why one rule beats another. Prefer classes; avoid IDs and !important for styling.
  • The cascade and inheritance: color and font inherit; margin and border do not.
  • Box model: content, padding, border, margin. Set box-sizing: border-box globally.
  • Units: px for borders, rem for type and spacing, % and viewport units for layout.

Modern CSS you should use

  • Custom properties (CSS variables) for colours and spacing, which also make dark mode easy.
  • Pseudo-classes like hover, focus-visible and disabled for interactive states.
  • Transitions for small, purposeful motion.

Debugging

Use DevTools to inspect an element, see which rules apply, which are crossed out and why, and edit values live. This one habit saves hours.

Pitfalls

  • Fighting specificity with more and more specific selectors instead of simplifying.
  • Removing focus outlines without a replacement, which hides where keyboard users are.
  • Hard-coding colours everywhere instead of using variables.

You are ready to move on when you can style a card component from a design screenshot and explain, using DevTools, why each rule applies.

Key concepts

  • Selectors and specificity
  • Cascade and inheritance
  • Box model
  • Units: px, rem, %, vw
  • Custom properties
  • Pseudo-classes and states
  • Transitions
  • DevTools styles panel

Mini project

Style your fest registration page with a colour theme defined in CSS variables, clear hover and focus states, and a light and dark mode toggle driven by a single class on the body.

Check yourself

Two rules set the colour of the same button and the one you expected does not win. What three things decide which rule applies?

Free resources