0
0
CSSmarkup~8 mins

Box sizing in CSS - Performance & Optimization

Choose your learning style9 modes available
Performance: Box sizing
MEDIUM IMPACT
Box sizing affects how the browser calculates element sizes, impacting layout stability and rendering speed.
Setting element width and padding without layout shifts
CSS
div {
  width: 200px;
  padding: 20px;
  box-sizing: border-box;
}
Padding and border are included inside the width, preventing layout shifts and reducing reflows.
📈 Performance GainSingle reflow on size change, improved visual stability (lower CLS).
Setting element width and padding without layout shifts
CSS
div {
  width: 200px;
  padding: 20px;
  box-sizing: content-box;
}
Padding adds to the width, causing unexpected element size and layout shifts when padding changes.
📉 Performance CostTriggers multiple reflows when padding or border changes, increasing CLS.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
content-box with paddingNormal DOM nodesMultiple reflows if padding changesHigher paint cost due to layout shifts[X] Bad
border-box with paddingNormal DOM nodesSingle reflow on size changeLower paint cost, stable layout[OK] Good
Rendering Pipeline
Box sizing affects the Layout stage by determining how element sizes are calculated before painting.
Layout
Paint
Composite
⚠️ BottleneckLayout stage is most expensive due to recalculations when box sizing causes size changes.
Core Web Vital Affected
CLS
Box sizing affects how the browser calculates element sizes, impacting layout stability and rendering speed.
Optimization Tips
1Always use box-sizing: border-box for predictable element sizing.
2Avoid content-box with padding to prevent layout shifts and reflows.
3Stable box sizing reduces Cumulative Layout Shift (CLS) and improves user experience.
Performance Quiz - 3 Questions
Test your performance knowledge
Which box-sizing value includes padding and border inside the element's width?
Aborder-box
Bcontent-box
Cpadding-box
Dmargin-box
DevTools: Performance
How to check: Record a performance profile while resizing or changing padding. Look for layout shifts and reflows in the flame chart.
What to look for: Frequent Layout events and Layout Shift score indicate poor box sizing usage.