CSS Margin vs Padding vs Border: The Box Model Explained with Examples

CSS Margin vs Padding vs Border: The Box Model Explained with Examples

Margin, padding and border are the first CSS properties almost everyone learns, and the ones people mix up the most. Should the gap be padding or margin? Why is my box wider than the width I set? Why did two margins turn into one?

This lesson answers all of that with pictures. Every image below was drawn with the same CSS it shows, and you can try everything yourself in the live playground.

Open the box model playground Move the sliders and watch a real box change.

What you'll learn

1. The box model: every element is a box

To the browser, every element on a page is a rectangle: a paragraph, a button, an image, even a single link. Each rectangle is made of four layers, from the inside out:

Diagram of the CSS box model: a blue content area inside green padding, inside an orange border, inside a light orange margin, with pixel values on each side
The box model. These are the same colours the browser DevTools use.
  1. Content: the text, image or other elements inside. Its size comes from width and height, or from what's inside it.
  2. Padding: empty space inside the box, between the content and the border.
  3. Border: a line around the padding.
  4. Margin: empty space outside the box, between it and its neighbours.

Here's CSS that builds a box like the one in the picture:

CSS: the box from the diagram
.box {
  width: 300px;
  padding: 20px;                /* inside space */
  border: 8px solid #e8a33d;    /* the line */
  margin: 24px;                 /* outside space */
}

Tip: you can see these layers on any website. Right-click an element, choose Inspect, and look at the box diagram in the Styles or Computed panel. Hovering an element paints its padding green and its margin orange on the page.

2. Padding: space inside the box

Padding pushes the content away from the box's edges. Most importantly, the box's background fills the padding. Add padding to a coloured button and the coloured area gets bigger.

CSS: padding on a button
.button {
  background: #6d5dfc;
  color: #fff;
  padding: 12px 24px;   /* 12px top and bottom, 24px left and right */
}

Padding is why buttons are comfortable to click: the clickable area is the content plus the padding.

One side at a time

Each side has its own property:

CSS: padding on one side
.box {
  padding-top: 10px;
  padding-right: 20px;
  padding-bottom: 10px;
  padding-left: 20px;
}

The shorthand: 1, 2, 3 or 4 values

Writing four lines gets tiring, so padding accepts up to four values in one line. The same rules work for margin.

Four boxes showing padding with one, two, three and four values, with the size of each side written on the box, and a clock diagram showing top, right, bottom, left
One to four values, and the clock trick for remembering the order.
You writeTopRightBottomLeft
padding: 20px20202020
padding: 10px 30px10301030
padding: 8px 30px 26px8302630
padding: 8px 20px 26px 36px8202636

With four values, the order is always top, right, bottom, left: clockwise from 12 o'clock. Many developers remember it as TRouBLe. When a value is missing, the browser copies it from the opposite side.

Good to know: padding can't be negative. Percentages like padding: 5% are worked out from the parent's width, even for top and bottom padding.

3. Margin: space outside the box

Margin pushes the whole box away from the things around it. Unlike padding, margin is always transparent. The box's background never fills it; you see the parent's background through it.

Two cards compared: on the left, 32px of padding makes the purple card bigger around white content; on the right, 32px of margin leaves the purple card the same size with empty orange space around it
Same 32px: padding grows the card, margin moves it away from others.
CSS: space between cards
.card {
  margin-bottom: 24px;   /* a gap under every card */
}

Centering a box with margin: 0 auto

A block element with a width (or max-width) can be centred horizontally by setting its left and right margins to auto. The browser splits the leftover space equally between them.

A purple box 360px wide centred inside a dashed container, with arrows showing equal margin-left auto and margin-right auto on each side
margin: 0 auto shares the spare space equally on both sides.
CSS: centering a page column
.container {
  max-width: 960px;
  margin: 0 auto;      /* 0 top and bottom, auto left and right */
  padding: 0 16px;     /* keeps text off the screen edge on phones */
}

This only works on block elements with a width smaller than their parent. It doesn't centre things vertically; for that, use Flexbox or Grid.

Negative margins

Margins, unlike padding, can be negative. margin-top: -20px pulls the element 20px up, overlapping whatever is above it. It's handy for small overlaps, like an avatar that sits half over a banner, but use it sparingly; it makes layouts harder to follow.

Margin collapsing

This surprises everyone at least once. When two block elements are stacked, their vertical margins don't add up. They overlap, and only the larger one is used.

Two boxes stacked: on the left the expected 50px gap from 30px plus 20px margins; on the right the actual 30px gap, because the margins collapse
margin-bottom: 30px meets margin-top: 20px. The gap is 30px, not 50px.
CSS: two margins that collapse
.a { margin-bottom: 30px; }
.b { margin-top: 20px; }
/* the gap between .a and .b is 30px, not 50px */

A few things to remember:

The simplest way to avoid surprises: put spacing on one side only (for example, always margin-bottom), or use gap in Flexbox and Grid layouts.

4. Border: the line around the box

The border sits between the padding and the margin. The border shorthand takes three things: width, style and colour.

CSS: border shorthand
.box {
  border: 2px solid #6d5dfc;
  /*      width style colour */
}

The style is required. Without it there's no border at all, because the default style is none. There are eight visible styles:

Eight boxes showing border styles solid, dashed, dotted, double, groove, ridge, inset and outset, and five purple shapes showing border-radius 0, 10px, 28px, 999px as a pill and 50% as a circle
The eight border styles, and border-radius from square corners to a circle.

In real projects you'll mostly use solid, sometimes dashed (for drop zones and placeholders) and dotted.

One side only

Like padding and margin, each side can be set on its own. A thick left border is a classic way to style a quote or a tip:

CSS: a left accent border
.tip {
  border-left: 4px solid #6d5dfc;
  padding: 12px 16px;
  background: #eef2ff;
}

Rounded corners with border-radius

border-radius rounds the corners. It works even if the box has no border at all; it also rounds the background.

CSS: border-radius examples
.card   { border-radius: 12px; }   /* gently rounded */
.pill   { border-radius: 999px; }  /* round ends, any width */
.avatar {
  width: 96px;
  height: 96px;
  border-radius: 50%;              /* a circle (when width = height) */
}

Border vs outline

outline looks like a border, but there's one big difference: a border takes up space, while an outline is drawn on top without moving anything.

Two rows of chips: in the first, a 4px border makes the middle chip bigger and pushes its neighbours; in the second, a 4px outline draws a ring without moving anything
Adding a border moves things. An outline does not.

That's why outlines are used for keyboard focus rings: they appear and disappear without making the page jump.

CSS: a focus ring with outline
button:focus-visible {
  outline: 3px solid #6d5dfc;
  outline-offset: 3px;   /* a small gap between the button and the ring */
}

5. box-sizing: why is my box too wide?

Here's the most common box model surprise. You set width: 300px, add some padding and a border, and the box comes out 360px wide.

Two boxes with the same width 300px, padding 20px and border 10px: with content-box the box measures 360px wide, with border-box it measures exactly 300px
content-box adds padding and border to the width. border-box keeps the box at the width you set.

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

300 + 20 + 20 + 10 + 10 = 360px

With box-sizing: border-box, the width includes padding and border. The box is exactly 300px, and the content shrinks to fit (240px). This is much easier to work with, so almost every modern website starts with this rule:

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

Margin is never part of the width in either mode. It's always outside.

The box model playground with border-box turned on, showing a dashed rounded border and the sum 24 + 8 + 20 + 164 + 20 + 8 + 24 = 268px
In the playground, the sum under the box shows how the width is shared out.

6. Margin or padding? A quick guide

PaddingMargin
Where the space goesInside the boxOutside the box
Background colourFills itNever fills it
ClickableYes, part of the elementNo
Can be negativeNoYes
Can be autoNoYes (for centering)
CollapsesNeverVertical margins can

A simple rule of thumb:

7. Practice: build a profile card

Let's use everything in one small component.

A profile card with a round avatar, the name Aisha Khan, a short bio and a Follow button; next to it the same card with its margin painted orange and its padding painted green
The finished card, and the same card with its margin and padding painted in.
HTML: profile card
<article class="profile">
  <img src="avatar.jpg" alt="Aisha Khan">
  <h3>Aisha Khan</h3>
  <p>Front-end developer who loves clean CSS.</p>
  <a class="btn" href="#">Follow</a>
</article>
CSS: profile card
.profile {
  max-width: 320px;
  margin: 40px auto;            /* space outside, and centred */
  padding: 24px;                /* space inside */
  border: 1px solid #e4e7ec;    /* a thin line around */
  border-radius: 16px;
  text-align: center;
  box-sizing: border-box;
}
.profile img {
  width: 96px;
  height: 96px;
  border-radius: 50%;           /* a circle */
  border: 4px solid #fff;       /* a white ring... */
  outline: 3px solid #6d5dfc;   /* ...inside a purple ring */
}
.profile h3 { margin: 16px 0 4px; }
.profile p  { margin: 0 0 20px; color: #667085; }
.btn {
  display: inline-block;        /* lets padding push the button bigger */
  padding: 10px 24px;           /* a comfortable click area */
  border: 2px solid #6d5dfc;
  border-radius: 999px;         /* pill shape */
  color: #6d5dfc;
  font-weight: 700;
  text-decoration: none;
}

Notice how each property does one job:

The button needs display: inline-block. Links are inline by default, and vertical padding on inline elements doesn't push the lines around them away.

8. Common mistakes

Try it in the playground

The CSS Box Model Playground: sliders for margin, border width, padding and width on the left, a live coloured box in the middle with the total width added up underneath, and the generated CSS
The playground: every slider changes a real box, and the CSS updates as you go.

The box model playground lets you try everything from this lesson:

The sum under the box adds up every layer, and the line below it shows what the browser actually measured, so you can check your maths.

Share your work

Built your first card? Share a screenshot as a link with sshot.online, or share your code with the free code sharing tool. No account needed.

Summary

Frequently asked questions

What is the difference between margin and padding in CSS?

Padding is space inside an element, between its content and its border, and it is filled by the element's background. Margin is transparent space outside the border that separates the element from its neighbours.

What order do the four values in padding and margin go in?

Top, right, bottom, left: clockwise starting at 12 o'clock. With two values the first is top and bottom and the second is left and right. With three values they are top, left and right, then bottom.

Why is my element wider than the width I set?

By default, width only sets the content area, and padding and border are added on top. Set box-sizing: border-box so the width includes padding and border.

Why does the gap between two elements not equal both margins added together?

Vertical margins between block elements collapse: they overlap and only the larger margin is used. This does not happen with left and right margins or inside Flexbox and Grid containers.

Does border-radius need a border?

No. border-radius rounds the element's corners, including its background and any image inside, even when it has no border.

What is the difference between border and outline?

A border is part of the box and takes up space, so adding one can move nearby elements. An outline is drawn on top of the box without taking space, which makes it ideal for focus rings.

Related articles

← All guides