KodingKaro
CSS · Beginner · The CSS Box Model

Box sizing and predictable dimensions

The box-sizing property determines whether padding and border are counted inside a declared width. Choosing a consistent model avoids many layout surprises.

✓ What you will learn in this topic

  • Explain the key idea: Border-box sizing makes component dimensions easier to reason about.
  • Apply the concept: The demo’s outside width stays at 18rem, with padding and border contained within that measurement.
  • Avoid this common mistake:

Concept explanation

box-sizing: border-box makes the declared width include content, padding, and border. A small universal reset applies it to elements and their pseudo-elements, so components are easier to size. The content area shrinks to make room for padding and border rather than expanding the outer box.

This does not make every layout responsive by itself. A fixed width can still overflow a small screen; combine predictable sizing with flexible widths such as max-width: 100%.

Syntax

A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.

*,
*::before,
*::after {
  box-sizing: border-box;
}
.demo {
  width: 18rem;
  padding: 1rem;
  border: 4px solid #2563eb;
}

Example 1: a small page element

Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.

<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>
*,
*::before,
*::after {
  box-sizing: border-box;
}
.demo {
  width: 18rem;
  padding: 1rem;
  border: 4px solid #2563eb;
}

Expected result: The demo’s outside width stays at 18rem, with padding and border contained within that measurement.

Example 2: a practical interface card

The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.

<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>
.card {
  box-sizing: border-box;
  width: min(100%, 30rem);
  padding: clamp(1rem, 4vw, 2rem);
  border: 1px solid #cbd5e1;
}

Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.

Common mistake

With the default content-box model, declared width excludes padding and borders, so the rendered box can become wider than expected.

Best practice

Use border-box sizing consistently and remember that margin is outside the border while padding is inside it.

Practice exercise

Build two cards with equal outer widths, different padding, and visible borders. Explain why border-box keeps their measurements predictable. Apply it to the example above and inspect the result at a narrow and a wide viewport.

Section summary

  • Border-box sizing makes component dimensions easier to reason about.
  • Use border-box sizing consistently and remember that margin is outside the border while padding is inside it.

Knowledge check

Under border-box, what does width include?

Reveal the explanation

Border-box keeps the total outer width equal to the declared width.

Finish the module lessons, then take the server-scored module quiz to check your understanding.

Practical example

css
*,
*::before,
*::after {
  box-sizing: border-box;
}
.demo {
  width: 18rem;
  padding: 1rem;
  border: 4px solid #2563eb;
}

Expected result:

The demo’s outside width stays at 18rem, with padding and border contained within that measurement.

Hands-on practice task

Build two cards with equal outer widths, different padding, and visible borders. Explain why border-box keeps their measurements predictable.

In this section

Other lessons in The CSS Box Model

Broaden your knowledge

Related learning topics