-
Hajipur, Bihar, 844101
box-sizing
in CSS?By default, when you set the width
and height
of an element in CSS, it only includes the content area — not padding or border.
The box-sizing
property allows you to control how the total width and height of an element is calculated.
element {
box-sizing: content-box | border-box;
}
box-sizing
content-box
(Default)Only the content is included in the width and height.
Padding and border are added outside the defined size.
.box1 {
width: 200px;
padding: 20px;
border: 5px solid black;
box-sizing: content-box;
}
🟢 Total width = 200 + 202 + 52 = 250px
border-box
Width and height include padding and border.
Actual content area shrinks to accommodate them.
.box2 {
width: 200px;
padding: 20px;
border: 5px solid black;
box-sizing: border-box;
}
🟢 Total width = exactly 200px
border-box
?Easier to maintain layout sizes
More predictable behavior
Commonly used in modern web design
<div class="box1">Content Box</div>
<div class="box2">Border Box</div>
.box1, .box2 {
width: 200px;
padding: 20px;
border: 5px solid;
margin-bottom: 20px;
}
.box1 {
box-sizing: content-box;
border-color: red;
}
.box2 {
box-sizing: border-box;
border-color: green;
}
🟢 You'll see that .box1
is larger in total width than .box2
.
border-box
to All Elements (Best Practice)*,
*::before,
*::after {
box-sizing: border-box;
}
✅ This ensures consistent sizing across all elements.
Q1. Set a <div>
to use box-sizing: border-box
.
Q2. Create two boxes side by side: one with content-box
, one with border-box
.
Q3. Apply box-sizing: border-box
to all elements globally.
Q4. Prevent box from overflowing its parent due to padding.
Q5. Style an input field that remains same width regardless of padding.
Q6. Create a button with fixed width and consistent padding.
Q7. Add 10px padding and 2px border inside a box with box-sizing: border-box
.
Q8. Create a responsive card layout using border-box
.
Q9. Apply box-sizing
using a class .fixed-box
.
Q10. Compare two elements’ total widths with different box-sizing
values.
Q1: What does box-sizing control?
Q2: What is the default value of box-sizing?
Q3: In content-box, the width excludes:
Q4: In border-box, the width includes:
Q5: Which box-sizing value is easier for layout control?
Q6: How to apply border-box to all elements globally?
Q7: What happens if you increase padding in content-box?
Q8: Which of the following is true for border-box?
Q9: Which value causes element size to increase with padding?
Q10: Which box-sizing value helps avoid layout overflow?