Beginner Topic 7 of 51 Web Development
Box Model
Content, padding, border and margin around every element.
How it works
The CSS box model says that every element is a rectangular box made of content, padding, border and margin, from the inside out. With the default content-box sizing, width and height apply to the content only, so padding and borders add to the total size; with border-box sizing, they are included. Vertical margins between adjacent block elements can collapse into one.
Step-by-step walkthrough
- Every element is a box.
- Content holds text or images.
- Padding is space around content inside the border.
- The border wraps the padding.
- Margin is the space outside, and box-sizing: border-box includes padding and border in the width.
Where it is used
Spacing, alignment, sizing and layout of every element depend on the box model. Most modern stylesheets set box-sizing to border-box for predictable widths.
Common pitfall
Forgetting that padding and borders add to width makes elements overflow their containers. Margin collapse surprises people, especially between parent and first child. Percent padding is relative to the width, not the height. Inline elements ignore vertical width and height. Inspect boxes in DevTools to see computed values and use consistent spacing scales.
Where it fits in the course
Box Model is topic 7 of 51 in Web Development, rated Beginner. It follows CSS Selectors. The next topic is Flexbox.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Web Development quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Web Development quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Box Model?
Where do people use Box Model?
What is the most common mistake with Box Model?
What should I learn before this topic?
How can I practice it?
Is Box Model hard to learn?
How long does it take to learn Box Model?
Which topics are related to Box Model?
What should I study after Box Model?
Related topics
- CSS Selectors (Web Development)
- Flexbox (Web Development)
- CSS Basics (Web Development)
- CSS Grid (Web Development)
- Client-Server Model (Distributed Systems & Cloud)
- Relational Model (Databases)
- Waterfall Model (Software Engineering)