CSS box-sizing Property

You are Here:

CSS box-sizing Property

CSS box-sizing property specifies how the total width and height of an element is calculated.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> div{ width: 300px; height: 100px; padding: 25px; text-align: center; border: 1px solid #8c8c8c; } #point{ box-sizing: content-box; } #point1{ box-sizing: border-box; } </style> </head> <body> <h1>CSS box-sizing property</h1> <h2>box-sizing: content-box;</h2> <div id="point"> My Container </div> <h2>box-sizing: border-box;</h2> <div id="point1"> My Container </div> </body> </html>

Syntax

Using CSS

element{ box-sizing: border-box; }

Using Javascript

object.style.boxSizing="border-box";

Animatable

No, box-sizing property is not animatable. CSS Animatable Properties Reference.

Default Value

Default value for CSS box-sizing property is content-box.

Property Value

The following table provides a list of values for CSS box-sizing property.

ValueExplanation
content-boxSpecifies that the width and height properties includes only the content. Border and padding are not included.
border-boxSpecifies that the width and height properties includes content, padding and border.

Using JavaScript

In the following example, we will demonstrate how to change the CSS box-sizing property of an element using JavaScript.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> div{ width: 300px; height: 100px; padding: 25px; text-align: center; border: 1px solid #8c8c8c; box-sizing: content-box; } </style> </head> <body> <h1>CSS box-sizing property</h1> <div> My Container </div> <button onclick="myFunction()">Click Me</button> <script> var x = document.getElementsByTagName("div")[0]; function myFunction(){ x.style.boxSizing = "border-box"; } </script> </body> </html>

Reminder

Hi Developers, we almost covered 98.7% of CSS Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in CSS.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author