CSS · CSS Advanced · Lesson 57
CSS Box Sizing
content-box vs border-box.
By default, width and height apply only to the content box, so padding and border make the element bigger. box-sizing: border-box includes padding and border in the width and height.
Example
<style>
.d1 { width: 300px; height: 80px; padding: 30px; border: 5px solid blue; }
.d2 { width: 300px; height: 80px; padding: 30px; border: 5px solid red; box-sizing: border-box; margin-top: 10px; }
</style>
<div class="d1">content-box: really 370px wide</div>
<div class="d2">border-box: exactly 300px wide</div>
Example
* {
box-sizing: border-box;
}
Keep reading “CSS Box Sizing”
Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.