CSS: Styling & the Box Model

Give your HTML skin — colour, typography, spacing and borders — and master the box model (content, padding, border, margin) that governs how every element is sized.

1. What CSS does

HTML gives a page structure; CSS (Cascading Style Sheets) gives it appearance. With CSS you control colour, fonts, spacing, size, position — everything visual. You write rules that target elements and declare how they should look:

selector {
  property: value;
  property: value;
}
h1 {
  color: #f59e0b;
  font-size: 2rem;
}

This rule says: “find every <h1> and make its text amber and 2rem tall.”

2. Selectors — choosing what to style

  • Tag: p { } — every paragraph.
  • Class: .card { } — every element with class="card". Reusable; your main tool.
  • Id: #header { } — the one element with id="header". Unique.
<div class="card">…</div>
.card { background: #161b22; }

3. The box model — the heart of layout

Every element is a box made of four layers, from the inside out:

┌─────────── margin ───────────┐   ← space OUTSIDE, pushes others away
│  ┌──────── border ────────┐  │
│  │  ┌───── padding ─────┐  │  │   ← space INSIDE, around the content
│  │  │     content       │  │  │   ← your text / image
│  │  └───────────────────┘  │  │
│  └────────────────────────┘  │
└──────────────────────────────┘
  • padding — breathing room inside the border (between content and edge).
  • border — the line around the box.
  • margin — space outside the border, separating this box from its neighbours.

One rule you’ll see in almost every real codebase:

* { box-sizing: border-box; }

This makes an element’s declared width include its padding and border, so a width: 300px box is actually 300px wide — not 300px plus padding. Far more predictable.

4. Units you’ll use constantly

  • px — absolute pixels.
  • rem — relative to the root font size (scales with user preferences — great for accessibility).
  • % — relative to the parent.

5. Style a card

Turn a plain box into a styled card. Edit style.css — the preview updates live.

Style a card
PREVIEW

Your tasks

  • ○.card has a non-default (non-transparent) background
  • ○.card has padding of at least 1rem (16px)
  • ○.card has a visible border
  • ○.card has rounded corners (border-radius)
  • ○.card has a max-width set
  • ○The <h2> is not the default black

6. What you learned

You styled your first component and — more importantly — you now understand the box model, the single most important concept for controlling how elements are sized and spaced. Padding inside, margin outside, border between; box-sizing: border-box to keep widths sane.

Next: arranging many boxes on a page with Flexbox and Grid.

🧪 Want to practise this on your own computer? See how to set up your lab →

🤖

Need clarification? Ask GT

Hi, I'm GT — your AI tutor for CSS: Styling & the Box Model. What would you like to know?
📝

Knowledge Check

1 of 3

In the box model, what sits BETWEEN the border and neighbouring elements?

Report a problem

About: /lessons/css-styling-and-the-box-model