2. Modern CSS Layout & Design Systems

CSS Selectors, Colors & The Box Model

Style web pages cleanly with CSS selectors, specificity, typography, and the CSS Box Model (margin, border, padding, content) with border-box.

3 min readUpdated 2026-11-29
In This Lesson
  • Target HTML elements using element and class CSS selectors
  • Visualize Content, Padding, Border, and Margin in the CSS Box Model
  • Prevent unexpected layout overflow using box-sizing: border-box

If HTML is the bare wooden frame and walls of a house, CSS (Cascading Style Sheets) is the paint, spacing, typography, and layout that make the house pleasant to live in. Without CSS, every website defaults to plain black serif text on a white sheet.

Let's learn how CSS targets HTML elements, how colors and spacing work, and the single most important layout concept on the web: the Box Model.

How CSS Targets Elements (Selectors & Specificity)

Every CSS rule follows one clean pattern: a selector picks which HTML elements to style, and curly braces { ... } hold property-value pairs describing how they should look:

css
/* Element selector: styles every <body> tag */
body {
  font-family: system-ui, sans-serif;
  color: #0f172a;
  line-height: 1.6;
}

/* Class selector (starts with a dot): reusable across any element */
.card {
  background-color: #ffffff;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  padding: 20px;
}

In your HTML, you attach a class using <div class="card">...</div>. Notice why class selectors are a superpower: you write .card once in your stylesheet and reuse it on five cards, three badges, or twenty product tiles—and if a class rule and a generic element rule both style color, the more specific class selector wins automatically.

The CSS Box Model: Every Element Is a Box

Even circular avatars and rounded buttons are rectangular boxes under the hood. Every element in CSS consists of four concentric layers wrapping outward from the center:

The CSS Box Model (Outside → Inside)
Margin (outside space)
Border + Padding (inside cushion)
Content (text / image)

With `box-sizing: border-box`, Padding and Border stay inside your declared width.

  1. Content: The innermost core—your text, icon, or image.
  2. Padding: Cushioning inside the box, pushing the element's border away from its own text so text doesn't touch the edges.
  3. Border: The visible (or invisible) frame drawn around the padding.
  4. Margin: Invisible elbow room outside the border that pushes neighboring elements away.

A classic CSS surprise catches almost everyone on day one: by default (content-box), setting width: 300px plus padding: 20px and border: 2px actually makes your element 344 pixels wide—causing cards to overflow narrow mobile screens!

Always place this universal border-box rule at the top of your stylesheets so width: 300px includes padding and borders:

css
*,
*::before,
*::after {
  box-sizing: border-box;
}
Quick Mental Shortcut for Margin vs Padding

Need a card's background color or clickable button area to feel roomier? Reach for padding. Need space between two separate cards? Reach for margin (or gap inside Flex/Grid).

Experiment Live with Padding, Borders & Colors

Try editing the padding, border-radius, and colors in the HTML/CSS playground below to watch the live preview respond immediately:

HTML / CSS / JSLive Web — Experiment with the Box Model
Live Browser Preview

Typography & Spacing Rules of Thumb

A handful of consistent numbers instantly elevates any page's visual polish:

  • Use rem (1rem = 16px default) for font sizes and spacing so visitors who increase browser zoom scale smoothly.
  • Keep body copy line-height between 1.5 and 1.8 so eyes track cleanly from line to line.
  • Cap long paragraphs at max-width: 65ch (about 65 characters wide) so text stays effortless to scan on wide monitors.

Quick Checkpoints

Check Your Understanding

Which CSS property adds space INSIDE an element's border (between its text and its border)?