Sitemap
Chirag Prajapati

Chirag Prajapati

Angular Enthusiast 🅰️

Press enter or click to view image in full size
Photo by Praveen Thirumurugan on Unsplash

CSS Box Model

5 min readJul 8, 2023

--

The CSS box model is a container that contains multiple properties including border, margin, padding, and the content itself. It is used to create the design and layout of web pages. According to the CSS box model, the web browser supplies each element as a square prism.

The following diagram illustrates how the CSS properties of width, height, padding, border, and margin dictate how much space an attribute will occupy on a web page.

Press enter or click to view image in full size

These are the following properties in the CSS box model:

Content

The content area consists of content like image, text, or other forms of media content. The height and width properties help to modify the box dimensions.

Padding

The padding area is the space around the content area and within the border box. It can be applied to all sides of the box or to the specific, selected side(s) — top, right, bottom, and left.

Border

The border area surrounds the padding and the content and can be applied to all the sides of the box or to selected side(s) — top, right, bottom, and left.

Margin

The margin area consists of the space between the border and the margin. The margin does not possess its own background color and is completely transparent. It shows the background color of the element, like the body element.

Press enter or click to view image in full size

example:

<style>
.container {
width:320px;
height:70px;
margin: 0;
padding:10px;
border:5px solid black;
background:wheat;
}
</style>
<div class="container">
Hello world....
</div>

So what is the total width and height of the div?

so here is a calculation of the box model element’s properties:

Total width = width + left padding + right padding + left border 
+ right border + left margin + right margin
Total height = height + top padding + bottom padding + top border 
+ bottom border + top margin + bottom margin

https://web.dev/learn/css/box-model/

Box-sizing property

The CSS box-sizing property is used to specify how to calculate the total height and width of an element. It controls the size of an element with a specified height and width. It allows you to include the padding and border within the total height and width of the element.

Get Chirag Prajapati’s stories in your inbox

Join Medium for free to get updates from this writer.

Before starting with CSS box-sizing, let’s first understand what issue occurs if we don’t use this property and why it’s needed nowadays.

Without box-sizing:

<style> 
.div1 {
width: 350px;
height: 200px;
border: 1px solid blue;
}

.div2 {
width: 350px;
height: 200px;
padding: 20px;
border: 1px solid red;
}
</style>
<div class="div1">This div is smaller (width is 350px and height is 200px).</div>
<br>
<div class="div2">This div is bigger (width is also 350px and height is 200px).</div>

So here you can see the width and height calculation of element:

actual width of an element's box : width + padding + border

actual height of an element's box : height + padding + border

It means if we create a box of some specific height and width and then add padding and border to it, it will look wider than the actual width.

This issue can be resolved with the help of box-sizing property.

If you set box-sizing: border-box; on an element, padding and border are included in the width and height:

<style> 
.div1 {
width: 350px;
height: 200px;
border: 1px solid blue;
box-sizing: border-box;
}

.div2 {
width: 350px;
height: 200px;
padding: 20px;
border: 1px solid red;
box-sizing: border-box;
}
</style>
<div class="div1">This div is smaller (width is 350px and height is 200px).</div>
<br>
<div class="div2">This div is bigger (width is also 350px and height is 200px).</div>

As we can see in the above output, we got both div with the same width.

Since the result of using the box-sizing: border-box is so much better, many developers want all elements on their pages to work this way.

Applying this to all elements is safe and wise:

* {
box-sizing: border-box;
}

The box-sizing property can be used to adjust this behavior:

  • content-box gives you the default CSS box-sizing behavior. If you set an element's width to 100 pixels, then the element's content box will be 100 pixels wide and the width of any border or padding will be added to the final rendered width making the element wider than 100px.
  • border-box tells the browser to account for any border and padding in the values you specify for an element's width and height. If you set an element's width to 100 pixels, that 100 pixels will include any border or padding you added, and the content box will shrink to absorb that extra width. This typically makes it much easier to size elements.
  • inherit: inherits the box sizing of the parent element.

Margin Collapse

Collapsing margins happen when two vertical margins come in contact with one another. If one margin is greater than the other, then that margin overrides the other, leaving one margin.

In ohter words, adjacent margins can sometimes overlap. This is known as margin collapse, and it has a reputation for being quite dastardly.

Here’s a typical example, involving two sibling paragraphs:

<style>
p {
margin-top: 24px;
margin-bottom: 24px;
}
</style>
<p>Paragraph One</p>
<p>Paragraph Two</p>

Instead of sitting 48px apart, their 24px margins merge together, occupying the same space!

Only vertical margins collapse. The left and right margins of the two horizontal elements will not collapse.

<style>
p {
display: inline-block;
margin-left: 24px;
margin-right: 24px;
}
</style>
<p>Paragraph One</p>
<p>Paragraph Two</p>

Refference: https://www.joshwcomeau.com/css/rules-of-margin-collapse/

--