CSS

The Box Model: Margin, Padding, and Border Explained

Diagram of the CSS box model showing margin, border, padding, and content
The Box Model: Margin, Padding, and Border Explained
Every HTML element on your screen is a box. The CSS Box Model is the concept that dictates how much space that box takes up. It can be frustrating when you set a div to width: 100px, add padding: 20px, and it suddenly takes up 140px of space. That's the default box model (content-box). The total width = width + padding + border. To change this behavior and make your life infinitely easier, use the universal selector * { box-sizing: border-box; }. This changes the model so that the 'width' property includes the padding and border. Now, if you set width: 100px and padding: 20px, the content area shrinks to fit, but the total visible width stays 100px. I apply this reset at the start of every single project. It eliminates countless layout bugs, especially when dealing with percentages and responsive design.
3,392
Views
136
Words
1 min read
Read Time
May 2025
Published
← All Articles 📂 CSS