P2.3: The CSS Box Model

Every element on a web page is treated by the browser as a rectangular box. The CSS Box Model governs how sizing and spacing are calculated.


1. Anatomy of the Box Model

The box model consists of four nested layers:

  • Content: The text or image itself.
  • Padding: Transparent spacing immediately surrounding the content (inside the border).
  • Border: A border surrounding the padding.
  • Margin: Transparent spacing outside the border to separate elements.

Box Sizing Calculation Impact

Comparing element size calculations under different properties (Width: 200px, Padding: 20px, Border: 5px).

Josh W. Comeau

Josh W. ComeauView Profile 🔗

Real-World Developer Case Study
8 Years Exp
RoleSenior CSS Educator
Salary₹65 Lakhs/yr equivalent
CompanyCSS for JS Devs
Core Tech Stack
CSS Box ModelLayout SystemsInteractive MDXReact

Always apply box-sizing: border-box globally in your reset stylesheets. If you don't, padding and border additions will stretch your layout columns and break your layouts.


2. Global Box-Sizing Reset

Use this universal reset at the top of your stylesheets:

*, *::before, *::after {
  box-sizing: border-box;
}

🎓 Practice Assignment Tasks

  • Create a box container with a width of 300px, 30px padding, and a 10px border.
  • Compare its width with and without `box-sizing: border-box` using Chrome DevTools.
Stuck? Hints available
/* Write your CSS styling rules here to complete the assignment */
🔒 Code requirements not met yet. Complete the tasks above.
🔒 Complete the Practice Assignment above to unlock completion.
Advertisement
NS
Ask Nextsem
AI Mode
NS
Home
Course
Playground