Thuta Learning
CSS
ExercisesWeb DevelopmentBeginner

Exercises: Selector & Box Model Practice

CSSLesson 3230 min

What you'll walk away with

  • Calculate a card's rendered width with and without box-sizing: border-box
  • Explain with specificity why a scoped selector like .card h3 beats h3
  • Centre cards in a row with display: flex, gap and justify-content
  • Handle the narrow-screen edge case with flex-wrap

Explain it simply

Explain it simply

This lesson teaches nothing new. You reuse the selectors, colour, box model and flexbox from the Basic and Intermediate chapters on one card layout. Combining them, instead of drilling each one alone, tests whether "I understand it" really means "I can write it myself".

A football player does not improve by reading the rules; they improve on the pitch. Each task takes five to ten minutes. Open your own code editor and try before looking at the solution, and for every task predict the result before you check it in the browser.

Problem → Why → Solution

The problem: the screenshot looks right

A learner wrote Tasks 1 and 3 as below, saw three centred cards in the browser and called it done. A designer measured them in DevTools: each card was 284px, not 250px. On a phone, all three cards shrank to about 100px and the text was crammed.

A screenshot only proves that it looks fine; it says nothing about computed values or edge cases. That is why every task in this lesson checks the prediction against the Computed tab in DevTools and also tries a narrow screen.

attempt.css
/* no box-sizing reset */
.card {
  width: 250px;
  padding: 16px;
  border: 1px solid #ddd;
}

.card-list {
  display: flex;   /* no flex-wrap */
  gap: 16px;
  justify-content: center;
}

Why 284px?

The default box-sizing is content-box, so width: 250px sizes only the content, and padding (16 × 2) and border (1 × 2) are added outside it: 250 + 32 + 2 = 284. The shrinking on phones comes from flex items' default flex-shrink: 1: inside a flex row, width is only a suggestion, and items shrink when space runs out.

Core concept: predict each task before you check it

The four original tasks are below. For each one, write down on paper what you expect to happen first, then write the code and compare in DevTools. The places where your prediction is wrong are where you learn the most.

Task 1: a border-box card

With box-sizing: border-box, give .card width: 250px, padding: 16px and border: 1px solid #ddd. Prediction: the outer width is exactly 250px and the content area is 250 - 32 - 2 = 216px. Find 216 in the DevTools box-model diagram.

Task 2: text styles inside the card

Style the title and description with the .card h3 and .card p selectors (colour and font-size). Prediction: .card h3 has specificity (0,1,1), so it beats a plain h3 (0,0,1) wherever that rule sits in the file, and h3 elements outside cards stay untouched.

Task 3: centre them in a flex row

Give .card-list display: flex, gap: 16px and justify-content: center. Prediction: on a 1024px screen three cards plus two gaps total 782px, and the leftover space is split evenly on both sides. gap only appears between cards, never at the edges.

Task 4 (bonus): a hover shadow

Add a box-shadow on .card:hover. Prediction: the shadow changes no layout. Unlike a margin or a border, box-shadow never pushes neighbouring cards. Adding transition: box-shadow makes it fade in smoothly.

box-sizingOuter width of width: 250pxContent area
content-box (default)250 + 32 + 2 = 284px250px
border-box250px250 - 32 - 2 = 216px

Always confirm in DevTools

Cross-check in the DevTools Elements panel that every property really applies, and make it a habit to confirm each change visually in the browser right after writing it.

Mental model: predict, write, measure
  1. Read the task, predict the result
  2. Write the code yourself
  3. Measure in the Computed tab
  4. Try it on a narrow screen

A mismatch between prediction and measurement is not a failure; it is your chance to correct your mental model.

General pattern

css
* { box-sizing: border-box; }

.component { width: SIZE; padding: SPACE; border: WIDTH STYLE COLOR; }
.component child-element { property: value; }   /* scoped selector */

.component-list {
  display: flex;
  flex-wrap: wrap;
  gap: SPACE;
  justify-content: center;
}
.component:hover { box-shadow: X Y BLUR COLOR; }

The reset makes box sizes predictable, the component is built from box-model properties, inner elements are styled through scoped selectors, and the list is a wrapping flex row.

Worked solution for Tasks 1-4

Try all four tasks yourself first, then compare with this solution. It adds one line the original tasks did not ask for, flex-wrap: wrap, so the cards drop to new rows on a phone instead of being squeezed. Run it and narrow the preview to about 375px.

After running it, right-click a card, choose Inspect and open the box-model diagram in the Computed tab. You should find the 216px content width and the 16px padding you predicted in the concept section. If you do not, check the reset line first.

cards.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Card practice</title>
  <style>
    * { box-sizing: border-box; }
    body { font-family: system-ui, sans-serif; }

    /* Task 1: a 250px card, padding and border included */
    .card {
      width: 250px;
      padding: 16px;
      border: 1px solid #ddd;
      border-radius: 8px;
      transition: box-shadow 0.2s ease;
    }

    /* Task 2: type selectors scoped to the card */
    .card h3 { margin: 0 0 8px; color: #1e3a8a; font-size: 1.25rem; }
    .card p { margin: 0; color: #4b5563; font-size: 0.95rem; }

    /* Task 3: centre the cards in a row */
    .card-list {
      display: flex;
      flex-wrap: wrap;
      gap: 16px;
      justify-content: center;
    }

    /* Task 4 (bonus): lift on hover */
    .card:hover { box-shadow: 0 8px 20px rgb(0 0 0 / 0.12); }
  </style>
</head>
<body>
  <div class="card-list">
    <div class="card"><h3>HTML</h3><p>Structure and meaning</p></div>
    <div class="card"><h3>CSS</h3><p>Boxes, colour and layout</p></div>
    <div class="card"><h3>JavaScript</h3><p>Behaviour and interaction</p></div>
  </div>
</body>
</html>

Code breakdown

  1. Predictable box* { box-sizing: border-box; }

    Because of this line, .card's width: 250px already includes padding and border. DevTools shows roughly 250 × 89, with a 216px content area.

  2. Scoped type selector.card h3 { margin: 0 0 8px; color: #1e3a8a; font-size: 1.25rem; }

    Selects only the h3 elements inside a card, removes the browser's default h3 margins and leaves 8px under the title.

  3. Wrapping rowflex-wrap: wrap;

    Without it, the cards shrink to around 100px at 375px. With it, each card keeps its 250px and any card that does not fit moves to the next row.

  4. Centringjustify-content: center;

    Splits the leftover space on the main axis (the row) evenly on both sides, and centres each wrapped row separately as well.

  5. Hover feedback.card:hover { box-shadow: 0 8px 20px rgb(0 0 0 / 0.12); }

    The shadow is offset 8px down with a 20px blur in 12% black. It does not affect layout, so neighbouring cards do not move.

You should see
1024px: three 250px cards centred in one row with 16px gaps.
375px: one 250px card per row, centred.
Hovering a card fades in a soft shadow.

In real projects

In real projects

"Three cards side by side" appears every day in blog post lists, course catalogues and feature sections. Professional teams compare the measurements in the design file (250px, 16px padding) with the computed values in DevTools before merging such a component, a step often called design QA.

Real content is messier than an exercise: titles run to two lines and the number of cards changes from two to twelve. The habit of adding flex-wrap and testing narrow screens, practised here, prevents a lot of bug reports in real projects. Article cards, accessible buttons and form fields are all built from these same selector, box-model and state patterns and then verified against a checklist.

Common mistakes

Common mistakes

Putting the reset only on .card

Writing .card { box-sizing: border-box; } fixes the card itself, but buttons and inputs added later are still content-box. A project where each element follows a different box model is hard to debug. Put the reset on * (or *, *::before, *::after) once.

Using margins on every card instead of gap

With .card { margin-right: 16px; }, the last card also carries 16px on its right, so with justify-content: center the whole group sits 8px off-centre to the left. gap only adds space between items, so the problem never appears.

Styling with a global type selector

Writing Task 2 as h3 { color: navy; } works inside the card, but every h3 in the footer and sidebar turns navy too. Select elements inside a component with a scoped selector such as .card h3.

Best practices

How to get the most out of an exercise

  • Write Tasks 1-3 yourself within ten minutes without the hints, then compare with the solution
  • Do not copy the solution; for every line that differs from yours, ask why
  • Put a long sentence in one card and check that the layout survives
  • Check the preview at three widths: 320px, 768px and a wide screen
  • The next day, rewrite the whole thing without looking; it makes the knowledge stick

Mini challenge

Exercise: a subscribe form

Practise the box model, selectors and states on a form: style a 320px subscribe form with one email field and one button. Predict the input's width before writing any code.

  • .form has width: min(100%, 320px), 16px padding and a 1px border
  • The label is a block and the input has width: 100% and 10px padding (border-box)
  • Both the input and the button get a :focus-visible outline
  • The button is at least 44px tall and darkens on :hover
  • .field--error input gets a red border
/* Markup: form.form > div.field.field--error > (label, input), then button.btn */

/* Prediction: how wide will the input be? Write it here: ____ px */

.form {
  /* TODO: fluid width capped at 320px, padding, border */
}

.field label {
  /* TODO: own line, small gap below */
}

.field input {
  /* TODO: full width, padding, border */
}

/* TODO: .btn with min-height 44px, and a darker :hover */
/* TODO: :focus-visible outline for input and button */
/* TODO: red border for .field--error input */

Expected: On desktop the form is 320px and the input and button are 286px wide each. The error field has a red border, the button is 44px tall, and Tab shows an outline. On a 320px phone the form is 304px and nothing scrolls sideways.

Show hint

With border-box, the form's content width is 320 - 32 - 2 = 286px. The input's width: 100% takes those 286px, and its padding no longer makes it wider.

Show solution
form.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    * { box-sizing: border-box; }
    body { font-family: system-ui, sans-serif; }

    .form {
      width: min(100%, 320px);
      padding: 16px;
      border: 1px solid #cbd5e1;
      border-radius: 8px;
    }

    .field label { display: block; margin-bottom: 4px; font-weight: 600; }

    .field input {
      width: 100%;
      padding: 10px;
      border: 1px solid #94a3b8;
      border-radius: 6px;
    }

    .field--error input { border-color: #dc2626; }

    .btn {
      width: 100%;
      min-height: 44px;
      margin-top: 12px;
      border: 0;
      border-radius: 6px;
      background: #2563eb;
      color: #fff;
      font-weight: 600;
    }

    .btn:hover { background: #1d4ed8; }

    .field input:focus-visible,
    .btn:focus-visible { outline: 3px solid #f59e0b; outline-offset: 2px; }
  </style>
</head>
<body>
  <form class="form">
    <div class="field field--error">
      <label for="email">Email</label>
      <input id="email" type="email" value="not-an-email">
    </div>
    <button class="btn" type="submit">Subscribe</button>
  </form>
</body>
</html>

Quick quiz 1 / 3

Without a box-sizing reset, what is the outer width of .card { width: 250px; padding: 16px; border: 1px solid #ddd; }?

Quick quiz 2 / 3

A stylesheet has .card h3 { color: navy; } and, later, h3 { color: crimson; }. What colour is an h3 inside a card?

Quick quiz 3 / 3

Three width: 250px cards sit in a flex row without flex-wrap. What happens on a 375px screen?

What to remember

  • Predict the result before writing code, then check it in the DevTools Computed tab
  • With border-box, width: 250px means 250px outside; with content-box it becomes 284px
  • A scoped selector like .card h3 beats a global h3 without leaking outside the component
  • gap and justify-content: center space the cards evenly, and flex-wrap: wrap rescues phones

CSS reference — MDN Web Docs

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Exercises: Selector & Box Model Practice | Thuta Learning