Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

The Box Model | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 5 of 50

·css

The Box Model

10 mins

Course progress10%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • The total size = `margin + border + padding + content`.
  • `box-sizing:border-box` makes the width/height include padding and border, simplifying layout.
  • Use the inspector to visualize each box‑model layer.

Every element in the layout world is a rectangular box composed of four concentric layers:

+---------------------------+
| margin (transparent)      |
|  +---------------------+  |
|  | border              |  |
|  |  +---------------+  |  |
|  |  | padding       |  |  |
|  |  |  +----------+ |  |  |
|  |  |  | content  | |  |  |
|  |  |  +----------+ |  |  |
|  |  +---------------+  |
|  +---------------------+  |
+---------------------------+
  • Content – actual text, image, or media.
  • Padding – inner “breathing room” between content and border.
  • Border – visual line; thickness contributes to total size.
  • Margin – outer whitespace that separates the element from its neighbours.

A common source of layout bugs is forgetting the margin that collapses with its parent’s margin, or adding padding that pushes the element beyond the width you designed.


Practical Example – Visualizing the Box Model

.box {
  width:200px;            /* content width */
  padding:20px;           /* inner space */
  border:5px solid #333;  /* border thickness */
  margin:30px;            /* outer space */
  background:#f4f4f4;
}

Use the browser inspector (Chrome DevTools → Computed → Box Model) to see the numbers update live.


Pro Tip – box-sizing

Set border‑box globally so the width you declare already includes padding and border:

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

Now the .box above will stay exactly 200 px wide, no matter how much padding you add.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonThe Cascade & SpecificityContinue to lesson 6Box‑Sizing PropertyBack to CSS Basics to Advanced curriculum