Box Model: Content-Box vs. Border-Box
Master CSS Box Model for Better Layout Control
CSS Box Model Fundamentals
Content-Box (Default)
Width applies to content only. Padding and borders are added to the total width, making elements larger than expected.
Border-Box (Modern)
Width includes padding and borders. The visual size matches your CSS width value exactly.
Content-Box vs Border-Box Behavior
| Feature | Content-Box | Border-Box |
|---|---|---|
| Width Calculation | Content + Padding + Border | Total Visual Width |
| Predictability | Less Predictable | More Predictable |
| Layout Control | Requires Math | Direct Control |
This exercise builds on previous box model concepts with enhanced text styling. You'll work with the Tahoe Box Model folder containing HTML and CSS files.
Implementing Percentage Padding
Add Flexible Padding
Replace fixed 20px padding with 7% padding in the header rule for responsive spacing
Observe Responsive Behavior
Notice how padding adjusts based on viewport width - less on narrow screens, more on wide screens
Identify Width Issue
Header becomes wider than sections below due to content-box model adding padding to max-width
Content-Box Width Calculation Example
Testing Border-Box in DevTools
Inspect Header Element
Right-click header and choose Inspect to open Chrome DevTools
Add Box-Sizing Property
In Styles panel, add box-sizing: border-box to see immediate width adjustment
Observe Uniform Widths
Header and sections now match in width as padding and borders fit within 800px
Apply border-box to all elements using the universal selector pattern: *, *::before, *::after. This ensures consistent behavior across your entire website.
We recommending including this code in every site you create.
Key Takeaways


