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.
Box sizing in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
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
CSS
div {
box-sizing: content-box;
width: 200px;
padding: 20px;
border: 5px solid black;
}CSS
div {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 5px solid black;
}Sample Program
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>
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. What does the CSS property
box-sizing: border-box; do?easy
Solution
Step 1: Understand box-sizing options
Thecontent-boxmodel adds padding and border outside the width and height, whileborder-boxincludes them inside.Step 2: Apply to
Usingborder-boxbox-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 DQuick 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
Solution
Step 1: Recall valid box-sizing values
The valid values arecontent-boxandborder-box. Others likepadding-boxorsize-boxare invalid.Step 2: Identify correct value for including padding and border
border-boxincludes padding and border inside the element's size.Final Answer:
box-sizing: border-box; -> Option AQuick Check:
Correct syntax for including padding and border = border-box [OK]
Hint: Only content-box and border-box are valid values [OK]
Common Mistakes:
- Using invalid box-sizing values
- Mixing up content-box and border-box
- Forgetting the colon or semicolon in CSS
3. Given this CSS:
div {
width: 200px;
padding: 20px;
border: 10px solid black;
box-sizing: content-box;
}
What is the total width of the div element as rendered in the browser?medium
Solution
Step 1: Calculate width with content-box
Withcontent-box, width is content only. Padding and border add outside width.
Width = content width + 2 * padding + 2 * border = 200 + 40 + 20 = 260px.Step 2: Confirm total width
Total width rendered = 260px (margin not included here).Final Answer:
260px -> Option CQuick Check:
content-box width = content + padding + border = 260px [OK]
Hint: Add padding and border twice to content width for content-box [OK]
Common Mistakes:
- Forgetting to double padding and border for both sides
- Confusing content-box with border-box
- Including margin in width calculation
4. You have this CSS:
p {
width: 150px;
padding: 15px;
border: 5px solid blue;
box-sizing: border-box;
}
But the paragraph is wider than 150px on the page. What is the likely cause?medium
Solution
Step 1: Understand border-box behavior
Withborder-box, padding and border are inside the width, so total width should be 150px.Step 2: Identify other layout factors
If the element is wider, likely margin, or display (like inline-block with whitespace) or parent constraints affect size.Final Answer:
There is extra margin or display property affecting width. -> Option AQuick Check:
border-box includes padding/border; extra width = margin or display [OK]
Hint: Check margin and display if border-box width seems off [OK]
Common Mistakes:
- Assuming border-box adds padding outside width
- Ignoring margin or inline-block spacing
- Thinking width is ignored with border-box
5. You want a fixed 300px wide button including padding and border. Which CSS will achieve this correctly?
hard
Solution
Step 1: Understand fixed width with padding and border
To keep total width 300px including padding and border, usebox-sizing: border-box;.Step 2: Check options
button { width: 300px; padding: 20px; border: 5px solid black; box-sizing: content-box; }uses content-box, so total width > 300px.button { width: 300px; padding: 20px; border: 5px solid black; box-sizing: border-box; }uses border-box with 300px width, so total size is 300px.button { width: 270px; padding: 20px; border: 5px solid black; box-sizing: border-box; }sets width 270px with border-box, making total smaller than 300px.button { width: 300px; padding: 0; border: 0; box-sizing: border-box; }removes padding and border, which is not desired.Final Answer:
Option B CSS code -> Option BQuick Check:
border-box + width = total fixed size including padding/border [OK]
Hint: Use border-box to keep total width fixed including padding and border [OK]
Common Mistakes:
- Using content-box and expecting fixed total width
- Adjusting width incorrectly with border-box
- Removing padding or border instead of adjusting box-sizing
