The CSS Box Model Fundamentals
Every single element rendered on a web page is essentially a rectangular box. Mastering the box model is the single most important concept in CSS.
What is it?
The CSS Box Model describes the rectangular box generated for elements in the document tree. It consists of four concentric layers: Content, Padding, Border, and Margin.
Why is it used?
Understanding how padding, borders, and margins calculate element width and height prevents unexpected layout breaks and horizontal scrollbars.
Detailed Explanation
Syntax Structure
*, *::before, *::after {
box-sizing: border-box;
}
.box {
width: 300px;
padding: 20px;
border: 2px solid #1c1c1c;
margin: 15px;
}
Code Example
/* Universal border-box reset */
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
/* Visual Box Demonstration */
.demo-box {
width: 320px;
padding: 24px;
border: 2px solid #1c1c1c;
margin: 20px auto;
background-color: #ffffff;
border-radius: 8px;
text-align: center;
}
Explanation of the Code
Expected Output
Real-World Application
Every modern CSS framework (Bootstrap, Tailwind, Bulma) includes the universal `border-box` reset in its base stylesheet.
Common Mistakes to Avoid
Quick Practice Challenge
Inspect any element on this page using Chrome DevTools. Scroll down in the Styles pane to see the interactive Box Model diagram.