Skip to main content

The CSS Box Model, explained.

Published 2022/03/07 14:00

This is a website:

Screenshot of a simple website with a title, two paragraphs and a link

This simple website has these elements:

  • A title
  • Two paragraphs
  • A link in the first paragraph.

It's fair to say that each of these elements lives in its own little space, with its own little borders. It's also safe to say that these boundaries are shaped like squares.

These boxes aren't there just to put stuff on a page. The boxes the elements are in, are also used to style each element with CSS.

Let's focus for a minute, on a single paragraph.

Styling happens in various parts of the element's borders:

The content of the element…

The space within the border…

The border itself that wraps the content and the space inside it…

And the space outside the border

All these parts together make up an element's box.

This is why the way the layout and styling of an element within a box is called The Box Model.

The Box Model

Everything I've mentioned above is part of an element's box.

  1. Content: the element's content. Usually text or other elements within it

  2. Padding: the area around the content but within the borders of the element.

  3. Border: The border mark itself.

  4. Margin: The area outside the border mark.

All these parts can be styled individually, so let's see use some CSS to do that for each one.

Let's start with some basic HTML of a basic element:

<p id="my-element">
This is the content
</p>

Styling the content

The content in an element can be anything, but in our example our element only has some text in it, for simplicity. Let's style our text to give it a red color and make it slightly bigger:

#my-element{
    color: red;
    font-size: 30px;
}

Styling the padding

Next, let's style the area around the content. Let's increase the padding to 30 pixels and change its background colour to a light grey:

#my-element{
    padding: 30px;
    background-color: lightgrey;
}

Styling the border

Now, let's style the border of the element. Let's give it a size of 21 pixels and make its color black:

#my-element{
    border: 21px solid black;
}

Styling the margin

Finally, let's style the area outside the border. Let's give our element a top margin so that it has a space from the element on top of it of 60 pixels:

#my-element{
    margin-top: 60px;
}

With everything together, this is how our CSS would look like for our element:

#my-element{
/*     Content */
    color: red;
    font-size: 30px;

/*     Padding */
    padding: 30px;
    background-color: #eee;

/*   Border */
    border: 21px solid black;

/*   Margin */
    margin-top: 60px;
}

And this is how the box model makes layouts and styling possible.

 More information

To learn more about the box model, check this amazing documentation by the MDN

Dev, Explained (43 part series)

  1. Javascript Scopes, explained.
  2. Javascript Promises, explained.
  3. Accessibility, explained.
  4. React, explained
  5. Should I use forEach() or map()?
  6. Should I use Flexbox or CSS Grid?
  7. Docker, explained.
  8. Unit testing, explained
  9. Git, explained.
  10. Typescript, explained.
  11. async/await, explained.
  12. The DOM, explained.
  13. Regular expressions, explained
  14. GraphQL, explained.
  15. Vue, explained.
  16. Svelte, explained.
  17. API, explained.
  18. Javascript Hoisting, explained.
  19. Immediately Invoked Function Expressions (IIFE), explained.
  20. ARIA roles, explained.
  21. Test-driven Development, explained.
  22. ARIA live regions, explained.
  23. aria-label in accessibility, explained.
  24. Type coercion in Javascript, explained.
  25. Variables, explained.
  26. if statements, explained.
  27. Arrays, explained.
  28. Currying in Javascript, explained.
  29. Memoization, explained.
  30. For loops, explained.
  31. Javascript Prototypes, explained.
  32. React Hooks, explained.
  33. Graph databases, explained.
  34. MongoDB, explained.
  35. Serverless, explained.
  36. Javascript Callback functions, explained.
  37. HTML, explained.
  38. CSS, explained.
  39. Responsive design, explained.
  40. Javascript, explained.
  41. The CSS Box Model, explained.
  42. CSS Flexbox, explained.
  43. CSS Grid, explained.
2022 Savvas Stephanides
Buy me a coffee
Some icons from Freepik