CSSBasicsbeginner

CSS Box Model Explained Simply

Every element is a box. Learn content, padding, border and margin with a diagram, how width is calculated, margin vs padding and box-sizing.

Published
TL;DR – Quick Answer

The CSS box model says every element on a page is a rectangular box made of four layers. From the inside out they are: content (the text or image), padding (space inside the border), border (the line around the box) and margin (space outside the border that pushes other elements away). By default, width sets only the content width, so padding and border are added on top. Setting box-sizing: border-box makes width include padding and border, which is easier to work with.

On This Page

In CSS, every element on a web page is a rectangular box. A paragraph is a box. A button is a box. An image is a box. The CSS box model describes the four layers that make up each box and how they decide its size and spacing.

Once you understand the box model, most spacing problems stop being mysterious: why a box is wider than you set it, why two boxes overlap or why there is a gap you never asked for. This page explains each layer with a diagram, compares margin and padding, and ends with a small before-and-after example. If you are new to CSS, start with our CSS tutorial for beginners.

CSS box model diagram

From the inside out, every box has four layers:

+--------------------------------------------------+
|                     MARGIN                       |
|   +------------------------------------------+   |
|   |                 BORDER                   |   |
|   |   +----------------------------------+   |   |
|   |   |             PADDING              |   |   |
|   |   |   +--------------------------+   |   |   |
|   |   |   |         CONTENT          |   |   |   |
|   |   |   |   text, image, etc.      |   |   |   |
|   |   |   +--------------------------+   |   |   |
|   |   |                                  |   |   |
|   |   +----------------------------------+   |   |
|   |                                          |   |
|   +------------------------------------------+   |
|                                                  |
+--------------------------------------------------+
Layer What it is Shows the background colour?
Content The actual text, image or child elements Yes
Padding Space between the content and the border Yes
Border A line around the padding Has its own colour
Margin Space outside the border, between this box and others No, always transparent

The gift box analogy

Think of a gift you are packing:

  • The gift itself is the content.
  • The bubble wrap around it is the padding. It keeps the gift away from the walls of the box.
  • The cardboard box is the border.
  • The space you leave between this box and the next one on the shelf is the margin.

You can see all four layers for any element. Right-click it in Chrome or Edge, choose Inspect, and look at the box diagram in the Computed tab of the developer tools.

Content box: width and height

width and height set the size of the content area.

.card {
  width: 200px;
  height: 100px;
}

If the content is larger than the box, it overflows. You can control that with overflow: hidden, overflow: auto (adds scrollbars when needed) or overflow: scroll.

Padding: space inside the border

.card {
  padding: 20px;              /* all four sides */
  padding: 10px 20px;         /* top and bottom 10px, left and right 20px */
  padding: 10px 20px 30px;    /* top 10px, left and right 20px, bottom 30px */
  padding: 5px 10px 15px 20px;/* top, right, bottom, left (clockwise) */
  padding-left: 30px;         /* one side only */
}

(You would write only one of these in real code. They are shown together to compare.)

Tip: For four values, remember TRouBLe: Top, Right, Bottom, Left. It goes clockwise from the top, like a clock.

Border: the line around the box

.card {
  border: 2px solid #333;   /* width, style, colour */
  border-radius: 8px;       /* rounded corners */
}

Common border styles are solid, dashed, dotted and double. You can set one side only with border-bottom: 1px solid #ccc;. The border takes up space, so a thicker border makes the box bigger.

Margin: space outside the border

.card {
  margin: 16px;           /* all sides */
  margin: 0 auto;         /* no top or bottom margin, centre horizontally */
  margin-top: 24px;       /* one side only */
}

Margin uses the same 1, 2, 3 or 4 value shortcuts as padding. Two things are special about margin:

  • margin: 0 auto centres a block element that has a set width. The browser splits the leftover space equally on both sides.
  • Margins can be negative, which pulls the element towards its neighbour. Padding cannot be negative.

Margin vs padding

Point Padding Margin
Where Inside the border Outside the border
Background colour Shows the element's background Always transparent
Clickable area Part of the element, so it is clickable Not part of the element
Can be negative No Yes
Collapses with neighbours Never Vertical margins can collapse
Use it to Give content breathing room inside a box Push boxes away from each other

A simple rule: if you want the coloured area to get bigger, use padding. If you want more gap between two boxes, use margin.

Common mistake: Using margin to make a button bigger. The button's colour does not grow, and the extra space is not clickable. Use padding instead, for example padding: 12px 24px;.

How the total width is calculated

By default, width sets only the content width. Padding and border are added on top.

.box {
  width: 200px;
  padding: 20px;
  border: 5px solid black;
  margin: 10px;
}
Visible width = width + padding-left + padding-right + border-left + border-right
              = 200  +     20       +      20       +      5      +      5
              = 250px

Space it takes on the page, including margin = 250 + 10 + 10 = 270px

You set 200px but the box is 250px wide. This surprises almost every beginner.

box-sizing: border-box

The box-sizing property changes what width means.

Value width includes width: 200px with 20px padding and 5px border
content-box (default) Content only Visible width 250px, content 200px
border-box Content + padding + border Visible width 200px, content 150px

With border-box, the box stays exactly as wide as you set it. The content area shrinks to make room for padding and border. Most developers add this rule at the top of every stylesheet:

*,
*::before,
*::after {
  box-sizing: border-box;
}

Popular CSS frameworks such as Bootstrap do the same thing.

Margin collapsing

When two block elements sit one above the other, their vertical margins do not add up. The browser keeps only the larger one.

.first  { margin-bottom: 30px; }
.second { margin-top: 20px; }
Expected gap: 30px + 20px = 50px
Actual gap:   30px   (the larger of the two)

This only happens with top and bottom margins of block elements in normal flow. Left and right margins never collapse, and margins of items inside a flex or grid container do not collapse either. If you need a fixed gap between items, gap on a flex container is often simpler.

Before and after: a card that needs the box model

Before: no spacing

<div class="card">
  <h3>Java Full Stack</h3>
  <p>Learn Java, Spring Boot, SQL and React by building projects.</p>
</div>
<div class="card">
  <h3>Data Analytics</h3>
  <p>Learn Excel, SQL, Python and Power BI with real datasets.</p>
</div>
.card {
  background-color: #eef4ff;
}

What you see:

+-------------------------------------------------------------+
|Java Full Stack                                              |
|Learn Java, Spring Boot, SQL and React by building projects. |
|Data Analytics                                               |
|Learn Excel, SQL, Python and Power BI with real datasets.    |
+-------------------------------------------------------------+

The text touches the edges of the blue background, there is no border, and the two cards run into each other. They look like one block.

After: padding, border and margin

* {
  box-sizing: border-box;
}

.card {
  background-color: #eef4ff;
  width: 400px;
  padding: 16px 20px;          /* breathing room inside */
  border: 1px solid #b9cdf5;   /* a visible edge */
  border-radius: 8px;
  margin: 0 auto 16px;         /* centred, with a 16px gap below */
}

.card h3 {
  margin: 0 0 8px;             /* remove the default top margin */
}

.card p {
  margin: 0;
}

What you see:

        +--------------------------------------+
        |                                      |
        |  Java Full Stack                     |
        |  Learn Java, Spring Boot, SQL and    |
        |  React by building projects.         |
        |                                      |
        +--------------------------------------+

        +--------------------------------------+
        |                                      |
        |  Data Analytics                      |
        |  Learn Excel, SQL, Python and        |
        |  Power BI with real datasets.        |
        |                                      |
        +--------------------------------------+

What each line did:

  • padding: 16px 20px moved the text away from the edges, and the blue background grew with it.
  • border gave each card a visible edge.
  • margin: 0 auto 16px centred each 400px card and put a 16px gap between them.
  • box-sizing: border-box kept each card exactly 400px wide, even with padding and a border.
  • h3 and p have default margins from the browser. Setting them ourselves makes the spacing predictable.

Paste both versions into our online HTML, CSS and JS editor and change the numbers to see the effect live.

Inline elements are different

The box model applies fully to block elements such as div, p and h1. Inline elements such as span, a and strong behave differently:

  • width and height are ignored.
  • Left and right padding and margin work.
  • Top and bottom padding is drawn but does not push other lines away, and top and bottom margin has no effect.

If you need an inline element to respect width, height and vertical spacing, set display: inline-block (or display: block). This is why links styled as buttons usually have display: inline-block.

Quick revision

  • Every element is a box: content, padding, border, margin.
  • Padding is inside the border and shows the background. Margin is outside and transparent.
  • By default, total width = width + padding + border.
  • box-sizing: border-box makes width include padding and border.
  • Vertical margins between blocks collapse to the larger value.

Next, use these boxes to build layouts with the CSS Flexbox guide, or practise with our CSS interview questions.

Frequently Asked Questions

What is the CSS box model in simple words?
It is the idea that every HTML element is a box with four layers: the content, padding around the content, a border around the padding and margin outside the border.
What is the difference between margin and padding?
Padding is space inside the border, between the border and the content, and it shows the element's background colour. Margin is space outside the border, between this element and its neighbours, and it is always transparent.
What does box-sizing: border-box do?
It makes width and height include the padding and border. A box with width 300px stays 300px wide even after you add padding and a border; the content area shrinks to make room.
How is the total width of an element calculated?
With the default content-box, total width = width + left padding + right padding + left border + right border. Margin adds space around the box but is not part of its visible width.
What is margin collapsing?
When the bottom margin of one block element meets the top margin of the next, the browser uses only the larger of the two instead of adding them. This happens with vertical margins, not horizontal ones.

Want to Build Your Career in Java Full Stack with AI?

Join CodeBegun and train with working industry engineers — See the Java Full Stack course in Hyderabad

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 5 December 2026 LinkedIn
Chat with us