Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Essential CSS
Lesson Plan
  1. Introduction
    1. Welcome to Essential CSS
    2. Getting started with the course
    3. What are file formats?
    4. Web standards
    5. What is HTML?
    6. What is CSS?
    7. Devtools, your indispensable best friend
  2. CSS Key Concepts
    1. Some background about CSS
    2. CSS Syntax and Terminology
    3. Selectors
    4. Cascade and Specificity
    5. Origin and Importance
    6. Inheritance
    7. How browsers handle CSS
  3. Basic Mental Model
    1. Everything on the web is a box
    2. The box model
    3. How big is your box?
    4. The relationship between px and em
    5. The `box-sizing` property
    6. The `display` property
    7. Formatting contexts
    8. Block boxes
    9. Inline boxes
    10. What is inline-block?
  4. Flexbox and Grid
    1. The parent-child relationship
    2. Sizing in Flexbox
    3. Flex directions
    4. Basic grid concepts
    5. Setting up a grid
    6. Placing grid items
    7. Box alignment
  5. Building web layouts
    1. Arranging your boxes on the web
    2. Positioning schemes
    3. Floating boxes
  6. Typography
    1. Making your text look good
    2. What is a font?
    3. Typesetting with CSS

The `box-sizing` property

2m:16s

The box model has many different layers, and each of them could take up space that adds to the size of your box. When it comes to how the browser calculates the size of a box, the box-sizing property can affect the final sizing values.

The default value of box-sizing is content-box, which implies that the sizes applied on properties like width and height exclude any margins, borders and paddings.

Say we have a div with the following CSS properties set on it:

div {
width: 100px;
height: 100px;
padding: 10px;
border: 2px dashed;
margin: 10px;
}
Devtools showing 100px square box with padding, borders and margin applied

Devtools will show you how the final width of the box is calculated, in the example above, you will notice that the box is 124 x 124. This is from the content itself which is 100px, plus padding (10px + 10px) and borders (2px + 2px).

If the box-sizing was set to border-box, this implies that sizing values applied to the element include padding and borders. So the width value of 100px will be made up of the content plus any padding and borders.

Devtools showing 100px square box with padding, borders and margin applied

Once you set the box-sizing property to border-box, you can see from Devtools that the content box is now sized to 76px because the 10px padding and 2px border are included in the 100px width calculation.

It is common to set box-sizing to border-box because often folks treat the padding and border as part of the content. Especially now that it is more common to size layouts with relative values, border-box makes things a lot more intuitive.

You can this manifest in the following example of a box with some child elements in it, all sized using percentages:

<div class="parent">
<div class="pair"></div>
<div class="pair"></div>
<div class="child"></div>
</div>
.parent {
width: 40%;
border: 3px solid green;
float: left;
}
.pair {
width: 50%;
padding: 10px;
border: 2px solid tomato;
float: right;
}
.child {
width: 75%;
padding: 20%;
border: 2px solid orange;
float: left;
}
Screenshot of above code example rendered in the browser

The child element of class name child ends up being longer than its parent container even though its content width is 75% of its parent. This is because the padding and borders are not factored into this 75%, and so the final width of the child ends up being 464px.

Once we set the box-sizing for all these boxes to border-box, their behaviour becomes more inline with what most people expect.

Screenshot of above code example with box-sizing set to border-box