Gigson Expert

/

November 26, 2025

The CSS Box Model

Discover the complete guide to the CSS Box Model in CSS. Understand how content, padding, borders, and margins shape your layout and learn how to fix common styling issues in modern web development.

Blog Image

Bukunmi Odugbesan

A frontend engineer with a desire to be a JavaScript expert

Article by Gigson Expert

Styling an application can be a difficult task, especially when it comes to controlling the size and position of elements on the application’s screen. Have you ever set an element width to 200px, only to find it overflowing the container? Layouts and styling challenges often stem from a lack of clarity on how browsers calculate an element’s final dimensions. The key to fixing these issues is mastering the CSS Box Model. 

For every HTML element used to add objects (words, buttons, images, etc.) to a web page, there is automatically a box around the object. For example, if you add <p> I am a boy </p> to a webpage, the words and the space around them make up a box. The properties of this box help in determining the space that the HTML element would take on the webpage. There are four properties of the box model: 

The Four Properties of the Box Model

Content 

The content is the actual object contained in a tag, whether it is a text, image, or video. In the example above, the words in the P tag are the content. In the case of a button, the word(s) inside the button are the content. When you set width and height properties, they apply only to the content area in the default box model.

Padding

The padding is the space immediately around the content of an element. The value of the padding ultimately affects the width and height specified for that element. For example:

<p> I am a boy </p>

p{
width; 40px;
padding: 10px;
}

As a result of the added padding, the total width becomes 60px (40px + 10px to the left + 10px to the right). Padding is usually added to an element so that the contents do not appear squeezed. 

Border 

The border is a line around the element, which, when added, takes up the space after the padding. The thickness of the border is specified to determine how visible you want the border to be. Setting border: none;  removes the border from a tag that comes with an already existing border. 


This is a button with border:

A button with border


This is a button after the border has been set to none.

A button after the border has been set to none

There are different border types: solid, dotted, dashed, etc. You can look them up here

Margin

The margin is the space immediately after the border. The values given to the margin are used to determine the spaces between two boxes on a webpage. For example, if you give the above <p> tag a margin of 10px, it moves 10px from the box above it, 10px from the box to its left, 10px from the box to its right, and 10px from the box beneath it.

The Box Model in Practice

In adding values to the properties of the box, you could specify where you want the change to happen. Margin-top, padding-left, and border-right are all valid properties. There are also shortcuts to adding these properties. For example, to add different values to each side, you add the value for each side one after the other on the same line:

<p> I am a boy </p>

p {
padding: 10px 5px 15px 7px
}.

The key is to remember that each value is allocated to a default position; you cannot change which position it goes to. The first value goes to the top, the second goes to the right, the third goes to the bottom, and the fourth goes to the left. This is useful so you don’t have to specify different sides of the same property using multiple lines. If you want the top and bottom to have the same value, and left and right to have the same value, you could write: margin: 10px 5px; such that the top and bottom have a margin of 10px, and the left and right have a margin of 5px. If you want all sides to have the same value, giving the property just one value applies it to all sides.

Borders also have a radius property, used to make the edges of the box rounder where necessary. 

This is a button without a radius

A button without a radius

This is a button with a radius of 4px

A button with a radius of 4px

As the object on the web page comes in a box shape by default, adding a border-radius can help us achieve our rounded needs. As long as you add the appropriate value, you will get your desired results.

Access a Global pool of Talented and Experienced Developers

Hire skilled professionals to build innovative products, implement agile practices, and use open-source solutions

Start Hiring

Margin Collapse

Margin collapse is a CSS behaviour where the vertical margins of two or more adjacent elements combine into a single margin. The resulting collapsed margin is equal to the largest of the individual margins, not their sum. Horizontal margins never collapse.

Collapsed margins can be counterintuitive, particularly in complex nested layouts. It can make debugging tricky if you are unaware of the collapsing rules. Margin collapsing will not occur if the elements are in flexbox or grid layout, or in inline-block.

Content Box vs Border Box

The default state of the box model is the content box, which refers to the innermost part of an element. When width and height are added to an element, it only affects the content area. This can be counterintuitive because if the width is set to 200px and a padding of 20px is added, the total width becomes 240px, which often causes layout issues. For example, if 3 columns are expected to fit into a row, and their widths are set at width: 33.3%, adding a padding distorts this as the width is increased on both sides by the value of the padding.

To avoid layout issues caused by the content box, the box model is set to border box, using the property box-sizing - box-sizing: border-box. In using the border box model, the layout becomes more intuitive, predictable, and easier to manage. So, when 3 columns are expected to fit in a row, and their widths are set, adding a padding doesn’t distort this, as the padding value is taken from the already existing width, i.e, the width of the content area reduced to accommodate the padding value. 

Example:

Content-box example

.box {
  width: 200px;
  padding: 20px;
}

Total width = 240px

Border-box example

.box {
  width: 200px;
  padding: 20px;
  box-sizing: border-box;
}

Total width = 200px

Conclusion

The box model can be tricky for a newbie, but as is the case with everything, practice makes perfect. If you are just learning, I encourage you to practice this out as you will need them every time you style! Good luck!

Frequently Asked Questions

What is the CSS Box Model?

It is a core concept that describes how every HTML element is rendered as a rectangular box by the browser. It determines the size, padding, margin, and border of all elements on the page.

What is the difference between content-box and border-box?

The difference between the two of them lies in how they affect the width and height of elements. Content-box applies the width and height to the content area only, while border-box applies the width and height to the border area.

Why is border-box generally preferred?

It is preferred because it is more intuitive. When you set the width of an element to 200px, it takes up 200px of the screen, regardless of how much padding or border you add.

Subscribe to our newsletter

The latest in talent hiring. In Your Inbox.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Hiring Insights. Delivered.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Request a call back

Lets connect you to qualified tech talents that deliver on your business objectives.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.