Performance: Common box model issues
This affects page layout stability and rendering speed by causing unexpected size calculations and layout shifts.
Jump into concepts and practice - no test required
div {
width: 300px;
padding: 20px;
border: 5px solid black;
box-sizing: border-box;
}div {
width: 300px;
padding: 20px;
border: 5px solid black;
box-sizing: content-box;
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| content-box with padding and border | Minimal | Multiple reflows if size changes | Higher due to layout shifts | [X] Bad |
| border-box with padding and border | Minimal | Single reflow | Lower paint cost, stable layout | [OK] Good |
box-sizing changes whether width/height include padding and border or not.box-sizing -> Option Bcontent-box excludes padding and border; border-box includes them.box-sizing and the value to include padding and border is border-box.div {
width: 200px;
padding: 20px;
border: 5px solid black;
box-sizing: content-box;
}
What will be the total width of the div element as seen in the browser?content-box, width is only content width; padding and border add outside it.p {
width: 300px;
padding: 10px;
border: 2px solid blue;
box-sizing: border-box;
}
But the paragraph still overflows its container. What is the most likely cause?border-box includes padding and border inside the width, so width 300px is total size.border-box, width includes padding and border, so width: 400px means total size is 400px.