CSS3 - Box Sizing


Box sizing property is using to change the height and width of element.

Since css2, the box property has worked like as shown below −

width + padding + border = actual visible/rendered width of an element's box

height + padding + border = actual visible/rendered height of an element's box

Means when you set the height and width, it appears little bit bigger then given size cause element boarder and padding added to the element height and width.

CSS2 sizing property

<html>
   <head>
      <style>
         .div1 {
            width: 200px;
            height: 100px;
            border: 1px solid green;
         }
         .div2 {
            width: 200px;
            height: 100px;    
            padding: 50px;
            border: 1px solid pink;
         }
      </style>
   </head>

   <body>
      <div class = "div1">TutorialsPoint.com</div><br />
      <div class = "div2">TutorialsPoint.com</div>
   </body>
</html>

It will produce the following result −

Above image is having same width and height of two element but giving result is different, cause second one is included padding property.

CSS3 box sizing property

<html>
   <head>
      <style>
         .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;
         }
      </style>
   </head>

   <body>
      <div class = "div1">TutorialsPoint.com</div><br />
      <div class = "div2">TutorialsPoint.com</div>
   </body>
</html>

Above sample is having same height and width with box-sizing:border-box. here result is shown below.

It will produce the following result −

Above elements are having same height and width with box-sizing:border-box so result is always same for both elements as shown above.

Advertisements