HTML DOM Style boxSizing Property


The HTML DOM boxSizing property is used to specify the way an element’s total width and height is calculated. It can have "border-box" or "content-box" as values.

Following is the syntax for −

Setting the boxSizing property −

object.style.boxSizing = "content-box|border-box|initial|inherit"

The property values are explained as follows −

ValueDescription
content-boxThis is the default value and any padding or border width will be added to the content box final width.
border-boxIn border-box the width specified stays intact and the content box shrinks if any padding or border is applied to it.
initialFor setting this property to initial value.
inheritTo inherit the parent property value

Let us look at an example for the boxSizing property −

Example

 Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   #DIV1{
      height:100px;
      width: 350px;
      border: 30px solid lightpink;
      box-sizing: border-box;
   }
</style>
<script>
   function changeBoxSizing(){
      document.getElementById("DIV1").style.boxSizing="content-box";
      document.getElementById("Sample").innerHTML="The box sizing is now changed to content-box ";
   }
</script>
</head>
<body>
   <div id="DIV1">
      THIS IS SAMPLE TEXT INSIDE DIV.HELLO WORLD DIV.
   </div>
   <p>Change the above div box-sizing property by clicking the below button</p>
   <button onclick="changeBoxSizing()">Change Box Sizing</button>
   <p id="Sample"></p>
</body>
</html>

Output

On clicking the “Change Box Sizing” button −

Updated on: 23-Oct-2019

20 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements