🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to CSS Notes
Topic #168

CSS Box Sizing


CSS Box Sizing

The CSS box-sizing property defines how to calculate the width and height of an element: should the calculation include padding and borders, or not.

By default, the width and height of an element is calculated like this:

  • width + padding + border = actual width of an element
  • height + padding + border = actual height of an element

This means: When you set the width/height of an element, the element often appears bigger than you have set (because the element's border and padding are added to the element's specified width/height).

The following illustrates two

elements with the same specified width and height. The two
elements end up with different sizes (because div2 has a large padding specified):

Example

.div1 {
  width: 300px;
  height:
100px;
  border: 1px solid blue;
}

.div2 {
  width: 300px;
  height: 100px;
  padding: 50px;
  border: 1px solid red;
}

The box-sizing property solves this problem.


The CSS box-sizing Solution

The box-sizing property allows us to include the padding and border in an element's total width and height.

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

Here is the same example as above, with box-sizing: border-box; added to both

elements:

Example

.div1 {
  width: 300px;
  height:
100px;
  border: 1px solid blue;
  box-sizing: border-box;
}

.div2 {
  width: 300px;

height: 100px;
  padding: 50px;
  border: 1px solid red;
  box-sizing: border-box;
}

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.

The code below ensures that all elements are sized in this more intuitive way. Many browsers already use box-sizing: border-box; for many form elements (but not all - which is why inputs and text areas look different at width: 100%;).

Applying this to all elements is safe and wise:

Example

* {
  box-sizing: border-box;
}

CSS Box Sizing Property

Property Description
box-sizing Defines how the width and height of an element are calculated: should they include padding and borders, or not

Want to go beyond the notes?

Join CodingNow 2.0's CSS course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

CSS Box Sizing – FAQs

Quick answers about learning CSS Box Sizing in CSS.

This free note from CodingNow 2.0 explains CSS Box Sizing in CSS — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every CSS topic on CodingNow 2.0, including CSS Box Sizing, is 100% free with no signup required.
With focused practice, most students grasp CSS Box Sizing in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now