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.

Box‑Sizing Property | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 6 of 50

·css

Box‑Sizing Property

5 mins

Course progress12%

What you'll learn

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

2 points
  • `border-box` prevents layout‑shifts when adding padding or borders.
  • Apply it globally via the universal selector for predictable sizing.

The box-sizing property tells the browser how to calculate an element’s total width and height.

ValueCalculation
content-box (default)width = content only; padding & border are added outside.
border-boxwidth = content + padding + border; the declared size is the final visible size.

Why border-box is popular

  • Designers think in visible dimensions. A 300 px button should stay 300 px no matter how much padding you add.
  • It eliminates the notorious “box‑model bug” where adding padding suddenly widens containers.

One‑Line Reset (most projects use this)

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

Real‑World Scenario – Card Component

.card {
  width: 100%;               /* Fill the column */
  padding: 1.5rem;            /* Safe inner space */
  border: 2px solid #e0e0e0;
  background: #fff;
  /* No need to subtract padding from width – border-box handles it */
}

When you later change padding to 2rem, the card’s outer width stays 100 % of its container.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonThe Box ModelContinue to lesson 7Colors & Color FunctionsBack to CSS Basics to Advanced curriculum