LearningCSS TutorialBox Model
CHAPTER 05 · BEGINNER

CSS Box Model

Browser almost har visible HTML element ko ek rectangular box ki tarah treat karta hai. Us box ke four main layers hote hain: content, padding, border aur margin.

English + Hinglish20 min readPractice included

What is the box model?

The CSS box model explains how an element's visible size and surrounding space are calculated.

box.cssCSS
.card {
  width: 300px;
  padding: 20px;
  border: 2px solid #cbd5e1;
  margin: 24px;
}
Hinglish Explanation

Element ko ek gift box samjho: andar content hai, content ke around padding, uske around border, aur doosre elements se distance margin deta hai.

Four layers of a box

  • Content — text, image ya actual element content.
  • Padding — content aur border ke beech ka inner space.
  • Border — padding/content ko surround karne wali boundary.
  • Margin — element ke bahar doosre elements se spacing.
visual orderMODEL
Margin
  Border
    Padding
      Content

Content width and height

By default, width and height apply to the content box when box-sizing: content-box is used.

content-box.cssCSS
.box {
  width: 300px;
  height: 120px;
  background-color: #eaf3ff;
}
Important: Fixed height ko unnecessary use mat karo. Content grow hone par overflow ho sakta hai. Often min-height ya natural content height safer hoti hai.

Padding

Padding creates space inside the border. The element's background extends through the padding area.

padding.cssCSS
.card {
  padding-top: 12px;
  padding-right: 20px;
  padding-bottom: 12px;
  padding-left: 20px;
}

.card-short {
  padding: 12px 20px;
}
Shorthand

padding: 12px 20px; ka matlab top/bottom = 12px aur left/right = 20px.

Border

A border surrounds the padding and content. Border width contributes to the element's total outer size in the default box model.

border.cssCSS
.card {
  border: 1px solid #dbe4ee;
  border-radius: 12px;
}

The shorthand commonly combines border width, style and color.

Margin

Margin creates space outside the border and separates an element from nearby elements.

margin.cssCSS
.section {
  margin-top: 40px;
  margin-bottom: 40px;
}

.card {
  margin: 0 auto;
}
Centering note

Block element ki defined/max width ho to margin: 0 auto; horizontal centering ke liye commonly use hota hai.

How total size is calculated

With the default content-box, declared width does not include padding and border.

calculation.cssCSS
.box {
  width: 300px;
  padding: 20px;
  border: 2px solid;
}

Total horizontal rendered width becomes 300 + 40 + 4 = 344px, excluding margins.

Formula: total width = content width + left/right padding + left/right border. Margin adds outside spacing but is not part of the element's border box.

box-sizing: border-box

border-box makes the declared width include content, padding and border. This usually makes layout sizing easier to reason about.

recommended-reset.cssCSS
*,
*::before,
*::after {
  box-sizing: border-box;
}

.card {
  width: 300px;
  padding: 20px;
  border: 2px solid #cbd5e1;
}
Why useful?

Ab card ki final border-box width 300px hi rahegi. Padding aur border us 300px ke andar calculate honge.

Margin and padding shorthand

shorthand.cssCSS
/* all sides */
padding: 16px;

/* vertical | horizontal */
padding: 12px 20px;

/* top | horizontal | bottom */
margin: 10px 20px 30px;

/* top | right | bottom | left */
margin: 10px 20px 30px 40px;
Memory trick: Four-value shorthand clockwise chalta hai: top → right → bottom → left.

Margin collapsing

Vertical margins between normal block elements can sometimes collapse into a single margin instead of adding together.

margin-collapse.cssCSS
.first {
  margin-bottom: 30px;
}

.second {
  margin-top: 20px;
}

In a common normal-flow case, the vertical gap may be 30px rather than 50px.

Beginner note

Agar vertical margin expected tarah add nahi ho raha, margin collapsing reason ho sakta hai. Flexbox aur Grid contexts me ye behavior different hota hai.

Overflow and oversized content

If content is larger than the available box, overflow can happen.

overflow.cssCSS
.panel {
  max-width: 420px;
  overflow-wrap: anywhere;
}

img {
  max-width: 100%;
  height: auto;
}

Responsive sizing and content-aware constraints help prevent boxes from breaking the layout.

Inspect the box model in DevTools

Browser DevTools can show content size, padding, border and margin visually. Inspect an element and check the computed/layout panel.

Practical habit

Box model ko sirf theory se nahi, browser DevTools me inspect karke samjho. Ye layout debugging ka daily tool hai.

Best practices

  • Project start me predictable sizing ke liye global border-box reset consider karo.
  • Spacing system consistent rakho instead of random values everywhere.
  • Fixed heights ko carefully use karo.
  • Responsive elements ke liye max-width useful hota hai.
  • Margin outer spacing ke liye aur padding inner spacing ke liye use karo.
  • DevTools me actual computed box inspect karo.

Common beginner mistakes

  • width: 300px ko final total width samajhna while using default content-box.
  • Margin aur padding ka purpose mix karna.
  • Every element ko fixed height dena.
  • Box-sizing difference ignore karna.
  • Four-value shorthand ka order confuse karna.
  • Unexpected vertical gap me margin collapse check na karna.

Practice Task

Ek “Profile Card” banao aur box model ko DevTools me inspect karo.

  1. Card ko width: 320px do.
  2. 20px padding aur 2px border add karo.
  3. Default content-box me total rendered width calculate karo.
  4. Phir box-sizing: border-box add karke difference observe karo.
  5. Card ko margin: 24px auto se horizontally center karo.
  6. Image ke liye max-width: 100% use karo.
  7. Two stacked cards ke vertical margins test karke margin collapse observe karo.
  8. DevTools me content, padding, border aur margin values verify karo.