Box sizing controls how the size of an element is calculated. It helps you decide if padding and borders are included in the width and height or added outside.
When you want to make sure an element fits exactly in a space without growing bigger because of padding or borders.
When you want consistent sizing for buttons or boxes, no matter their padding or border thickness.
When you want to avoid layout problems caused by padding or borders increasing the element size unexpectedly.
Syntax
CSS
box-sizing: content-box | border-box;
content-box is the default. Width and height apply only to the content area. Padding and border add extra size outside.
border-box includes padding and border inside the width and height, so the element size stays as you set it.
Examples
The total size will be 200px (content) + 40px (padding) + 10px (border) = 250px wide.
This page shows two boxes. The first uses content-box, so padding and border add to the width. The second uses border-box, so the total width stays 200px. You can see how the boxes differ in size.
CSS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Box Sizing Example</title>
<style>
.box {
width: 200px;
padding: 20px;
border: 5px solid #333;
margin-bottom: 20px;
background-color: #cce5ff;
font-family: Arial, sans-serif;
}
.content-box {
box-sizing: content-box;
}
.border-box {
box-sizing: border-box;
}
</style>
</head>
<body>
<h1>Box Sizing Demo</h1>
<div class="box content-box">
<strong>content-box</strong><br />
Width set to 200px.<br />
Padding and border add extra size.<br />
Total width is 250px.
</div>
<div class="box border-box">
<strong>border-box</strong><br />
Width set to 200px.<br />
Padding and border inside width.<br />
Total width stays 200px.
</div>
</body>
</html>
OutputSuccess
Important Notes
Using border-box often makes layout easier because sizes stay consistent.
You can set box-sizing: border-box; globally with * { box-sizing: border-box; } to simplify styling.
Summary
Box sizing controls if padding and border are inside or outside the width and height.
content-box adds padding and border outside the size.
border-box keeps padding and border inside the size, making layout easier.
Practice
(1/5)
1. What does the CSS property box-sizing: border-box; do?
easy
A. Removes padding and border from the element.
B. Adds padding and border outside the element's width and height.
C. Sets the element's width and height to auto.
D. Includes padding and border inside the element's width and height.
Solution
Step 1: Understand box-sizing options
The content-box model adds padding and border outside the width and height, while border-box includes them inside.
Step 2: Apply to border-box
Using box-sizing: border-box; means the total size includes padding and border, making layout easier.
Final Answer:
Includes padding and border inside the element's width and height. -> Option D
Quick Check:
box-sizing: border-box = padding and border inside [OK]
Hint: Remember border-box keeps size fixed including padding and border [OK]
Common Mistakes:
Confusing border-box with content-box behavior
Thinking padding is always outside size
Ignoring border width in calculations
2. Which of the following is the correct CSS syntax to set box sizing to include padding and border inside the element's size?
easy
A. box-sizing: border-box;
B. box-sizing: size-box;
C. box-sizing: padding-box;
D. box-sizing: content-box;
Solution
Step 1: Recall valid box-sizing values
The valid values are content-box and border-box. Others like padding-box or size-box are invalid.
Step 2: Identify correct value for including padding and border
border-box includes padding and border inside the element's size.
Final Answer:
box-sizing: border-box; -> Option A
Quick Check:
Correct syntax for including padding and border = border-box [OK]
Hint: Only content-box and border-box are valid values [OK]