๐Ÿ“ Html Css

The CSS Box Model

P
Author
Pyland
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
2 min
๐Ÿ‘๏ธ
Views
85
๐ŸŒฑ
Level
Beginner

Every HTML element is a rectangle. CSS controls the size of that rectangle through four layers: content, padding, border, and margin. Together they form the box model.

The Four Layers

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚            margin                โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚          border            โ”‚  โ”‚
โ”‚  โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚  โ”‚
โ”‚  โ”‚  โ”‚       padding        โ”‚  โ”‚  โ”‚
โ”‚  โ”‚  โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚  โ”‚  โ”‚
โ”‚  โ”‚  โ”‚  โ”‚    content     โ”‚  โ”‚  โ”‚  โ”‚
โ”‚  โ”‚  โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚  โ”‚  โ”‚
โ”‚  โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  • content โ€” the actual text or image. Size is set with width and height.
  • padding โ€” inner space between the content and the border. The background color extends into padding.
  • border โ€” the frame around the padding. Visible and takes up space.
  • margin โ€” outer space. Transparent, pushes the element away from its neighbors.

Example

.card {
  width: 300px;
  padding: 16px;
  border: 2px solid #e5e7eb;
  margin: 24px;
}
<div class="card">
  <h2>Python Article</h2>
  <p>A short description...</p>
</div>

The Problem: width does not include padding and border by default

By default, the browser treats width as the width of the content only. Padding and border are added on top.

.card {
  width: 300px;
  padding: 16px;
  border: 2px solid #e5e7eb;
}
/* Actual width: 300 + 16*2 + 2*2 = 336px */

This is counterintuitive and causes unexpected layout shifts.

The Fix: box-sizing: border-box

* {
  box-sizing: border-box;
}

With this rule, width includes padding and border โ€” the element is exactly as wide as you specified.

.card {
  width: 300px;
  padding: 16px;
  border: 2px solid #e5e7eb;
}
/* Actual width: exactly 300px */

Always add box-sizing: border-box at the top of your CSS. This is the first rule in every serious project.

Shorthand notation

padding and margin can be written on one line:

/* All four sides equal */
padding: 16px;

/* Vertical | horizontal */
padding: 8px 16px;

/* Top | horizontal | bottom */
padding: 8px 16px 12px;

/* Top | right | bottom | left (clockwise) */
padding: 8px 16px 12px 0;

The same applies to margin.

Individual sides

.card {
  margin-top: 24px;
  margin-bottom: 24px;
  padding-left: 16px;
  padding-right: 16px;
  border-bottom: 1px solid #e5e7eb;  /* only the bottom border */
}

Margin collapse

Vertical margins between adjacent block elements collapse โ€” the browser takes the larger value instead of adding them together.

.article-title {
  margin-bottom: 24px;
}

.article-meta {
  margin-top: 16px;
}
/* Gap between them: 24px, not 40px */

This surprises many beginners. Horizontal margins do not collapse.

DevBlog: base styles for an article card

/* Reset box-sizing for all elements */
*, *::before, *::after {
  box-sizing: border-box;
}

/* Article card */
.post-card {
  width: 100%;
  max-width: 680px;
  padding: 24px;
  margin-bottom: 32px;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
}

.post-card h2 {
  margin-top: 0;      /* remove extra space above the first heading */
  margin-bottom: 8px;
}

.post-card p {
  margin: 0;
}

Summary

Property What it does Affected by background
padding Inner space Yes
border Frame โ€”
margin Outer space No
box-sizing: border-box width includes padding+border โ€”

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

Django: Template Tags

Template tags are logic inside HTML. Unlike {{ variable }} which only outputs a value,...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 81
๐Ÿ“

Colors and Fonts in CSS

The first thing a DevBlog reader sees is text. Well-chosen colors and fonts make a...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 73
๐Ÿ“

CSS Selectors

A selector is the first part of a CSS rule. It tells the browser: "apply...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 78

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!