Skip to content
Justice Tech School

Box Sizing

By default (content-box), width applies to the content only, so padding and border make the element bigger than its stated width. With box-sizing: border-box, width includes padding and border.

Most developers apply border-box to everything with *, *::before, *::after { box-sizing: border-box; } at the top of the stylesheet. Sizes then add up the way you expect.

Example
<style>
  div { width: 200px; padding: 20px; border: 5px solid #be123c; margin-bottom: 10px; background: #ffe4e6; }
  .content { box-sizing: content-box; }
  .border { box-sizing: border-box; }
</style>
<div class="content">content-box: 250px wide in total</div>
<div class="border">border-box: exactly 200px wide</div>

Exercise

Make width include padding and border.

* {
  box-sizing: ;
}

Quiz Test

With border-box, a 200px wide element with 20px padding on each side has a content width of:

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.