Understanding the Box Model

Learn the CSS box model, its sizing modes, and how box-sizing and display affect page layout.

🎯 Introduction

The box model is fundamental to understanding web layouts. Each HTML element can be thought of as a rectangular box consisting of content, padding, a border, and margins. Understanding these parts lets you control element dimensions, positioning, and spacing more precisely and build more flexible layouts.

This article introduces the box model, compares its sizing modes, illustrates the effects of box-sizing, and explains how display affects boxes. It is useful both for beginners and for frontend developers reviewing the fundamentals.


📦 What is the Box Model?

Each element on a web page can be thought of as a box with four parts:

PartDescription
ContentThe area containing the element's actual content, such as text or images.
PaddingThe space between the content and the border.
BorderThe boundary around the content and padding, with a configurable color, width, and style.
MarginThe space outside the element that separates it from other elements.

Box model calculations determine how much space an element occupies on the page.

For example, a box with a width of 200px, 10px of padding, a 2px border, and 15px margins can occupy considerably more than 200px in total width.


🧱 Which Box Sizing Modes Are Available?

There are two commonly used box sizing modes:

Box sizing modeDescriptionTotal width calculation
Standard box model (content-box)The default: width and height include only the content, excluding padding and borders.Total width = content width + horizontal padding + horizontal borders + horizontal margins
Border-box model (historically called the IE box model)Width and height include content, padding, and borders, making overall sizing easier.Total width = specified width + horizontal margins

Use box-sizing to switch between these modes:

/* Default standard box model */
.example1 {
    box-sizing: content-box;
}

/* Border-box model */
.example2 {
    box-sizing: border-box;
}

With border-box, the specified width includes padding and borders. These reduce the available content width instead of making the box wider.

Many modern projects apply box-sizing: border-box; consistently to simplify sizing and make layouts more predictable.

🎨 Visual Differences Between box-sizing Values

Here is a simple example comparing the two sizing modes.

<style>
    .box1,
    .box2 {
        width: 200px;
        height: 100px;
        padding: 20px;
        border: 5px solid #333;
        margin: 20px;
        background-color: lightblue;
    }
    .box1 {
        box-sizing: content-box;
    }
    .box2 {
        box-sizing: border-box;
    }
</style>

<div class="box1">content-box</div>
<div class="box2">border-box</div>
  • .box1 uses content-box and occupies:
    200px content width + 40px padding (20px on each side) + 10px borders (5px on each side) = 250px total width
    Height is calculated in the same way.
  • .box2 uses border-box: the content area shrinks so that padding and borders fit within 200px.

In the browser, .box1 is visibly larger than .box2. Understanding this difference helps prevent broken layouts.


🧱 Understanding the display Property

The display property determines how a box participates in document flow. Common values include:

ValueDescriptionBox model behavior
blockA block-level box starts on a new line and fills the available width by defaultSupports width, margins, padding, and borders
inlineAn inline box shares a line with other content and is sized by its contentFor non-replaced inline elements, width and height do not apply; horizontal margins and padding affect spacing
inline-blockShares a line with other elements while supporting explicit width and heightUses block-like sizing while participating in inline flow
noneThe element is not displayed and occupies no spaceNo box is generated

Different display values significantly affect box behavior. In particular, width and height do not apply to non-replaced inline elements, although padding and margins can adjust spacing.

Choose a display value that suits your layout. For example, navigation items often use inline-block, while main content areas commonly use block.

❓ Knowledge Check

Q1: Why does adding padding make an element wider than its specified width?

The default sizing mode is content-box, where width includes only the content. Padding and borders add to the total width.

Q2: How is width calculated with border-box?

With border-box, the specified width includes content, padding, and borders, so it matches the box's border-to-border width.

Q3: Why can't you set width and height on inline elements?

Non-replaced inline elements are sized by their content. CSS width and height do not apply to them; use padding and margins to adjust spacing.


🧪 Exercises

Exercise 1: Compare Box Sizing Modes

Create two boxes with the same width, padding, and border. Use content-box on one and border-box on the other, then compare their appearance.

<style>
    .box1,
    .box2 {
        width: 150px;
        height: 80px;
        padding: 15px;
        border: 3px solid #666;
        margin: 10px;
        background-color: #a0d8f0;
        color: #333;
        font-size: 14px;
        text-align: center;
        line-height: 50px;
    }
    .box1 {
        box-sizing: content-box;
    }
    .box2 {
        box-sizing: border-box;
    }
</style>
<div class="box1">content-box</div>
<div class="box2">border-box</div>

Copy the code into your editor and preview the result!


Exercise 2: Explore the Effect of display

Create a simple navigation menu. Use inline-block to place its items on one line, and give each item a width, padding, and border.

<style>
    .nav {
        background-color: #eee;
        padding: 10px;
    }
    .nav-item {
        display: inline-block;
        width: 100px;
        padding: 10px;
        margin-right: 10px;
        border: 2px solid #333;
        text-align: center;
        cursor: pointer;
    }
</style>
<div class="nav">
    <div class="nav-item">Home</div>
    <div class="nav-item">Products</div>
    <div class="nav-item">About</div>
    <div class="nav-item">Contact</div>
</div>

Copy the code into your editor and preview the result!


🎉 You have learned the box model, its sizing modes, and the essentials of box-sizing and display! Keep practicing to make page layout easier.