← Back to CSS
08

CSS Box Model: content, padding, border and margin

Understand how the actual size of an element is calculated and how to control its spacing.

📘 Theory

The 4 layers of the box model

Understand what takes up space and what does not.

Each element has content, padding, border and margin. The browser adds up these layers to determine its actual size.

The margin separates boxes from each other; the padding creates internal air between content and border.

  • Content: the actual content.
  • Padding: internal space.
  • Border: line around.
  • Margin: external space.

box-sizing: size control

Prevent padding from breaking your layouts.

1

With box-sizing: border-box, padding and border are included within the defined width and height.

2

This makes layouts more predictable, especially in columns and cards.

Clear and consistent spacing

Use simple scales to avoid visual chaos.

An 8px or 4px scale helps maintain consistency in padding and margin.

Think of content blocks with enough air to facilitate reading.

  • 8px scale: 8, 16, 24, 32.
  • Padding for reading: 16px or more.
  • Separation between sections: 24px or more.

🧭 Key visuals

Box model layers

Visualize how the total size changes according to box-sizing.

Layers of the box model with margin, border, padding and content.

Box model explained with layers

Works as a quick support to remember which part takes up space inside and outside the box.

CSS box model infographic with content, padding, border and margin

🧪 Learn by doing

Example Card with padding and margin Control internal air and external separation.
Example Predictable box
Example Compare box-sizing Observe how the actual width changes.

🏁 Challenges

Challenge Challenge: exact separation Define internal padding and external margin.

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com