CSS box-sizing – impact on padding and border

CSS box-sizing decides if specified width, height, etc. will apply to content area of an element or to whole element including padding and border.

CSS property box-sizing

CSS version: CSS 3
Value: content-box | border-box
Initial: conent-box
Applies to: all elements that accept width or height
Inherited: no

box-sizing values

valueDescription
content-boxspecified width and height apply to of the content box of the element. Two element with same width and height but different padding or border will have different overall size.
border-boxspecified width and height apply to of the whole element including padding and border. Two element with same width and height but different padding or border will have same overall size

Example – box-sizing content-box and border-box

In the following code, CSS property box-sizing can be changed to the following values
  • content-box
  • border-box
<style type="text/css">
.box {
  width: 100px; height: 100px;
  background-color: lightgreen;
  float:left; margin:10px;
  /*default box-sizing is content-box*/
  box-sizing: content-box;
}
.box1 {
  padding: 2px;
  border: 2px solid lightblue;
}
.box2 {
  padding: 10px;
  border: 10px solid lightblue;
}
</style>

<div class="box box1">Hello world</div>
<div class="box box2">Hello world</div>
box-sizing refresh

Related

Specification and Browser compatibility

SpecificationStatusCategories
CSS3 Box-sizingW3C Candidate RecommendationCSS3
Desktop
ChromeFirefoxIEEdgeSafariOpera
Yes 10+ Yes 29+ Yes 8+ Yes 12+ Yes 5.1+ Yes 10.0-10.1+
Mobile
Android ChromeAndroid FirefoxiOS SafariIE MobileOpera Mobile
Yes 47+ Yes 44+ Yes 5.0-5.1+ Yes 10+ Yes 10+
source: caniuse.com
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments