FrontendBeginnerCSS
Box Model
PronunciationBOKS MOD-el
Definition
The Box Model is a fundamental CSS concept where every HTML element is treated as a rectangular box consisting of content, padding, borders, and margins. It defines how these layers interact to determine the total size and layout of an element.
Where you hear it
Commonly discussed when styling layouts, debugging spacing issues, or learning the basics of CSS.
Examples
The browser calculates the total width of an element by adding its content, padding, and border.
You can change the default behavior of the box model using the
box-sizingproperty.With the default box-sizing, 20px of padding on each side makes the box 40px wider.
Common mistake
Many beginners forget that the margin is outside the border and does not contribute to the element's background color or size calculation, often leading to unexpected spacing issues.
Don't confuse with
Margin vs padding are often confused, but margin creates space outside the element's border, whereas padding adds space inside the border around the content.
Say it at work
- In a meeting or chat
Let's check the box model in the browser dev tools to see why this card is overflowing.
- In an email or ticket
Please ensure the box model calculations are correct before adjusting the outer layout.