ProgramGuru

Understanding the CSS Box Model

Understanding the CSS Box Model

Every CSS element participates in a box model made of content, padding, border and margin.

What you’ll learn

  • Name content, padding, border and margin.
  • Identify which part creates inner or outer space.
  • Predict how changing padding affects a box.

Understand the concept

The content box holds text or child elements. Padding creates space inside the border; the border surrounds the padding and content; margin separates the entire bordered box from neighbors.

With the default box-sizing: content-box, a declared width applies to content only. With border-box, the declared width includes padding and border. Understanding this is essential before learning Flexbox and Grid.

Visualize the relationship

margin (outside the border)borderpadding (inside the border)content
From the inside out: content → padding → border → margin.

See all four layers

Visible colors reveal the padding, border and outside margin.

HTML

<div class="canvas"><div class="box">Content</div></div>

CSS

.canvas { background: #ffedd5; padding: 1px; }
.box { background: #bfdbfe; padding: 24px; border: 6px solid #7c3aed; margin: 18px; }

Rendered output

Content

Observe: The content is blue, padding stays inside the purple border, and margin separates the box from the outer surface.

Change the inner space

Increase padding while preserving the border styling.

HTML

<div class="canvas"><div class="box">More breathing room</div></div>

CSS

.canvas { background: #ffedd5; padding: 1px; }
.box { background: #bfdbfe; padding: 36px; border: 6px solid #7c3aed; margin: 8px; }

Rendered output

More breathing room

Observe: Increasing padding moves content away from the border; reducing margin moves the border toward neighboring content.

Explain the result

The outer footprint of a content-box element consists of content width plus left/right padding and borders; margins add outside spacing.

Compare the two examples: identify the selector, each changed property, and which visible part of the output proves that the CSS took effect.

Common mistakes to avoid

  • Padding is inside the border; margin is outside.
  • Declared width does not always represent the final outer footprint.

Try it yourself

Change padding to 8px and margin to 30px. Predict which space appears inside versus outside the purple border.

Before changing the code, predict what will change. Then verify by checking the rendered result.

Check your understanding

Which part of the box model creates space between content and border?
Choose one answer. You can try again.

Answer and explanation

padding

Padding creates inner space between content and the border.

What you should remember

The four regions serve different roles; visualize them before adjusting dimensions.

Revisit the rendered examples if the rule still feels abstract. Change one declaration at a time so you can connect cause and effect.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Width, Height, and Min/Max Dimensions →