CSS Academy · Lesson

Box-Sizing: Content-Box vs Border-Box

Understand how box-sizing changes how width and height calculations include padding and border.

Lesson 3 of 413 steps

Box-Sizing: Content-Box vs Border-Box is a free CSS Academy lesson on CoddyKit. This is lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CSS Academy learning path, and your progress syncs across the web and the CoddyKit app. The CSS Academy course includes 4 lessons in total.

The box-sizing Problem

By default, CSS sets width on the content area only. Adding padding or border increases the element's actual rendered size, which is confusing and error-prone.

<div class="box">200px + padding + border = 244px rendered</div>

content-box (Default)

box-sizing: content-box is the default. width and height apply to the content only. Padding and border are added on top, growing the element beyond the set size.

border-box

With box-sizing: border-box, width and height include content + padding + border. The element stays exactly the declared size regardless of padding or border.

<div class="box">Exactly 200px rendered width</div>

The Universal border-box Reset

Apply border-box globally using the universally recommended reset:

<div class="demo-box">Width stays exactly 200px</div>

Why inherit Instead of *

An alternative pattern allows components to override box-sizing if needed:

<div class="demo-box">Inherited border-box sizing</div>

Percentage Width + Padding

The main practical benefit of border-box is with percentage widths. A width: 50% element with 20px padding stays at exactly 50% of its parent without overflow.

<div class="col">50% wide column with padding, no overflow</div>

border-box Does Not Include margin

Even with border-box, margin is not included in the declared width. Margins always add space outside the element on top of its set dimensions.

History: Why Is content-box the Default?

Historically browsers disagreed on the box model. The W3C standardized content-box but IE used border-box. Modern CSS defaults to content-box for W3C compliance, but virtually all projects override it to border-box.

Inspecting Box Model in DevTools

In Chrome DevTools, the Elements panel shows a box model diagram with exact content, padding, border, and margin dimensions. This is the fastest way to debug sizing issues.

Grid and Flexbox Use border-box Internally

CSS Grid and Flexbox always size tracks and items using border-box semantics, regardless of the element's own box-sizing setting. This keeps their sizing math predictable.

min/max with border-box

min-width, max-width, min-height, and max-height work the same way as width — they include padding and border when border-box is active.

Quick Check

With box-sizing: border-box, an element declared as width: 300px; padding: 30px; has a content area width of:

Recap

box-sizing: border-box includes padding and border in the declared width, preventing unexpected size growth. Apply it globally with *, *::before, *::after { box-sizing: border-box }. Margins are never included in either box model.

Free to start

Learn CSS with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “Box-Sizing: Content-Box vs Border-Box” lesson free?

Yes — the full text of “Box-Sizing: Content-Box vs Border-Box” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CSS Academy course, upgrade to CoddyKit PRO. The CSS Academy course includes 4 lessons in total.

What will I learn in “Box-Sizing: Content-Box vs Border-Box”?

Understand how box-sizing changes how width and height calculations include padding and border. You practise CSS Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start CSS Academy?

No prior experience is required. CSS Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Box-Sizing: Content-Box vs Border-Box” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this CSS Academy lesson?

Yes. Every CSS Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Understanding Width Height Margin and Padding
  2. Border Styles Width and Color
  3. Box-Sizing: Content-Box vs Border-Box
  4. Outline and Box-Shadow
← Back to CSS Academy