CSS
🎯 CSS box-sizing Property
The box-sizing CSS property determines how the total width and height of an HTML element are calculated.
It controls whether an element's padding and border are included within the specified width and height values or added on top of them.
🔹 Why Use box-sizing?
The box-sizing property helps create predictable layouts and is widely used in responsive web design. It allows developers to manage element dimensions more accurately.
- Makes layout calculations easier
- Improves responsive design consistency
- Prevents unexpected element sizing
- Simplifies width and height management
🧩 box-sizing Values
1️⃣ content-box (Default)
The specified width and height apply only to the content area. Padding and borders are added to the final size of the element.
div {
box-sizing: content-box;
width: 200px;
padding: 20px;
border: 10px solid black;
}
Total width = 200px + 40px padding + 20px border = 260px.
2️⃣ border-box
The specified width and height include the content, padding, and border. The overall size remains exactly as defined.
div {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 10px solid black;
}
Total width remains 200px because padding and border are included within the specified width.
3️⃣ initial
Resets the property to its default value, which is content-box.
div {
box-sizing: initial;
}
4️⃣ inherit
The element inherits the box-sizing value from its parent element.
div {
box-sizing: inherit;
}
📌 Practical Example
* {
box-sizing: border-box;
}
Many developers apply border-box globally because it makes element sizing more predictable and simplifies responsive layouts.
-
🧠 Summary
- box-sizing controls how element dimensions are calculated.
- content-box calculates only the content size, while padding and borders are added separately.
- border-box includes padding and borders within the specified dimensions.
- Border-box is commonly used in responsive and modern web design.
