The Box Model: Content, Padding, Border, Margin
You'll learn how every HTML element is structured as a box with four distinct layers that stack from the inside out.
What is the Box Model?
Every element on a webpage is treated as a rectangular box, even if it doesn't look like one. This box has four layers that surround each other like nesting dolls:
- Content — The innermost area where your text or images appear
- Padding — Clear space between the content and the border (inside the element)
- Border — A line that wraps around the padding and content
- Margin — Clear space outside the border (between elements)
How They Stack
Think of the box model like a framed picture on a wall:
- Content is the actual photograph
- Padding is the mat board around the photo
- Border is the frame itself
- Margin is the wall space separating this frame from others
Each layer adds to the total space an element occupies. If your content is 200px wide, and you add 20px of padding on each side, 2px borders, and 10px margins, the element takes up significantly more space than just the content width.
Why This Matters
Understanding the box model is essential because it controls:
- How much space elements actually take up
- How elements are positioned relative to each other
- Why elements might not align as you expect
- How to create proper spacing in your layouts
The box model affects every single element you style, from paragraphs to forms to entire page sections. Mastering it is the foundation for all CSS layout work.
Key Takeaway: Every HTML element is a box with four layers—content, padding, border, and margin—stacking from inside out, and understanding this structure is fundamental to controlling layout and spacing.