Stage 2: Frontend Fundamentals

CSS · Lesson 03

Box Model

margin, padding, border and box-sizing explained with visual examples.

CSS3 min readFree lesson

In CSS, every element is rendered as a box.
Once you understand the four-stage structure of the box model, you can solve layout problems precisely.


1. The four stages of the box model

┌───────────────────────────────────────┐
│         margin (outer space)          │
│  ┌─────────────────────────────────┐  │
│  │         border (frame)          │  │
│  │  ┌───────────────────────────┐  │  │
│  │  │     padding (inner space) │  │  │
│  │  │  ┌─────────────────────┐  │  │  │
│  │  │  │  content (inner)    │  │  │  │
│  │  │  └─────────────────────┘  │  │  │
│  │  └───────────────────────────┘  │  │
│  └─────────────────────────────────┘  │
└───────────────────────────────────────┘
Area Description
content The area that holds the actual content (text, images)
padding The inner space between content and border
border The border that surrounds the element
margin The space outside the element, between it and other elements
div {
  /* Content */
  width: 200px;
  height: 100px;

  /* Inner spacing (top right bottom left order) */
  padding: 10px 20px 10px 20px;
  padding: 10px 20px;      /* 10px top/bottom, 20px left/right */
  padding: 10px;           /* 10px on all four sides */
  padding-top: 10px;       /* Individual setting for each side */

  /* Border */
  border: 2px solid #333;  /* thickness style color */
  border-radius: 8px;      /* round the corners */

  /* Outer spacing */
  margin: 20px auto;       /* 20px top/bottom, auto left/right (centered) */
}

2. box-sizing: border-box

By default, width is the size of the content area only.
If you add padding and border, the actual element becomes larger.

/* Default: width = content only */
/* Actual size = 200 + 40(padding) + 4(border) = 244px */
.default {
  width: 200px;
  padding: 20px;
  border: 2px solid black;
  box-sizing: content-box; /* default value */
}

/* border-box: width = content + padding + border */
/* Actual size = always 200px */
.better {
  width: 200px;
  padding: 20px;
  border: 2px solid black;
  box-sizing: border-box;  /* recommended */
}

In practice, we apply it everywhere with * { box-sizing: border-box; }.


3. width, height, max-width

.container {
  width: 100%;             /* 100% of the parent's width */
  max-width: 1200px;       /* cap the maximum width (essential for responsive design) */
  height: auto;            /* automatic, based on the content */
  min-height: 100vh;       /* minimum height: viewport height */
}

4. overflow

Specifies how to handle content that is larger than the element.

.box {
  overflow: visible; /* default: spills outside */
  overflow: hidden;  /* clipped (useful for cropping images) */
  overflow: scroll;  /* always show scrollbars */
  overflow: auto;    /* show scrollbars only when needed (recommended) */
}

5. The display property

/* Block element: takes up an entire line, width/height can be set */
display: block;         /* default for div, p, h1~h6, section */

/* Inline element: flows like text, width/height cannot be set */
display: inline;        /* default for span, a, strong */

/* Inline block: flows like text but width/height can be set */
display: inline-block;  /* default for img, button */

/* Hidden */
display: none;          /* removed from the screen entirely */

6. Margin collapse

The vertical margins of two adjacent elements are not added together; only the larger one applies.

.first  { margin-bottom: 30px; }
.second { margin-top: 20px; }
/* Actual gap: 30px (not 30 + 20 = 50px!) */

How to fix it:

  • Replace them with padding
  • Apply overflow: hidden or padding: 1px to the parent
  • Use Flexbox/Grid (no collapsing)

Example file

Open box-model.html in the examples/ folder to see a visualization demo where each area is distinguished by color.

Assignment

Read assignment.md and build the card component.

Example files

Complete, runnable files used in this lesson. Copy them into your project and experiment.

box-model.htmlHTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Box Model Visualization</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>

  <header class="page-header">
    <h1>Box Model Visualization Demo</h1>
    <p>Each area (content / padding / border / margin) is distinguished by color so you can see it clearly.</p>
  </header>

  <main class="container">

    <!-- Box model diagram -->
    <section class="card">
      <h2>1. Box model structure</h2>
      <div class="diagram-wrapper">
        <div class="box-margin">
          <span class="label label-margin">margin</span>
          <div class="box-border">
            <span class="label label-border">border</span>
            <div class="box-padding">
              <span class="label label-padding">padding</span>
              <div class="box-content">
                <span class="label label-content">content</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>

    <!-- box-sizing comparison -->
    <section class="card">
      <h2>2. box-sizing comparison</h2>
      <p class="desc">Both boxes have <code>width: 300px</code>, but their actual sizes differ.</p>
      <div class="sizing-demo">
        <div class="sizing-content-box">
          <strong>content-box (default)</strong>
          <p>width: 300px<br>padding: 20px<br>border: 4px</p>
          <p class="result">Actual width: 348px</p>
        </div>
        <div class="sizing-border-box">
          <strong>border-box (recommended)</strong>
          <p>width: 300px<br>padding: 20px<br>border: 4px</p>
          <p class="result">Actual width: 300px</p>
        </div>
      </div>
    </section>

    <!-- display property comparison -->
    <section class="card">
      <h2>3. display property comparison</h2>

      <h3>display: block</h3>
      <p>Block elements take up an entire line.</p>
      <div class="display-block">Block A</div>
      <div class="display-block">Block B</div>

      <h3>display: inline</h3>
      <p>Inline elements are arranged side by side like text. (width and height are ignored)</p>
      <span class="display-inline">Inline A</span>
      <span class="display-inline">Inline B</span>
      <span class="display-inline">Inline C</span>

      <h3>display: inline-block</h3>
      <p>They are arranged side by side like inline elements, but width and height can be set.</p>
      <span class="display-inline-block">Inline block A</span>
      <span class="display-inline-block">Inline block B</span>
      <span class="display-inline-block">Inline block C</span>
    </section>

    <!-- overflow -->
    <section class="card">
      <h2>4. overflow property</h2>
      <div class="overflow-demo">
        <div class="overflow-visible">
          <strong>overflow: visible</strong>
          <p>When the content overflows, it spills outside the box. This is the default. This text goes beyond the box!</p>
        </div>
        <div class="overflow-hidden">
          <strong>overflow: hidden</strong>
          <p>When the content overflows, it is clipped and hidden. This text is hidden even though it goes beyond the box!</p>
        </div>
        <div class="overflow-auto">
          <strong>overflow: auto</strong>
          <p>When the content overflows, a scrollbar appears. This text is long and needs scrolling. Try scrolling down. There is more content!</p>
        </div>
      </div>
    </section>

    <!-- margin collapse -->
    <section class="card">
      <h2>5. Margin collapse</h2>
      <p class="desc">
        The margin-bottom/top of the two boxes below is 30px each,<br>
        but the actual gap is <strong>30px</strong> (not 60px combined).
      </p>
      <div class="collapse-demo">
        <div class="collapse-box">
          margin-bottom: 30px
        </div>
        <div class="collapse-box">
          margin-top: 30px
        </div>
      </div>
    </section>

    <!-- centering with margin -->
    <section class="card">
      <h2>6. Horizontal centering with margin auto</h2>
      <div class="centered-box">
        margin: 0 auto<br>
        width: 200px
      </div>
    </section>

  </main>

  <footer class="page-footer">
    <p>Box Model Visualization Example &copy; 2025</p>
  </footer>

</body>
</html>
style.cssCSS
/* =============================================
   Box model visualization example
   Lesson 03: content / padding / border / margin
   ============================================= */

/* Global box-sizing reset (recommended approach) */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Segoe UI', Arial, sans-serif;
  background-color: #f0f2f5;
  color: #333;
  line-height: 1.6;
}

.page-header {
  background-color: #2c3e50;
  color: white;
  text-align: center;
  padding: 40px 20px;
}

.page-header h1 {
  font-size: 1.9rem;
  margin-bottom: 8px;
}

.container {
  max-width: 900px;
  margin: 0 auto;       /* auto left/right: horizontal centering */
  padding: 30px 20px;
}

.card {
  background: white;
  border-radius: 10px;
  padding: 28px;
  margin-bottom: 24px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.07);
}

.card h2 {
  font-size: 1.25rem;
  color: #2c3e50;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 2px solid #3498db;
}

.card h3 {
  font-size: 1rem;
  color: #555;
  margin-top: 20px;
  margin-bottom: 8px;
}

.desc {
  color: #666;
  font-size: 0.9rem;
  margin-bottom: 16px;
}

code {
  background: #f4f4f4;
  color: #c0392b;
  padding: 2px 6px;
  border-radius: 3px;
  font-family: 'Courier New', monospace;
  font-size: 0.88em;
}

/* =============================================
   1. Box model diagram (each area distinguished by color)
   ============================================= */
.diagram-wrapper {
  display: flex;
  justify-content: center;
  padding: 10px 0;
}

/* margin area: orange */
.box-margin {
  background-color: #fde8c8;  /* light orange (margin area) */
  padding: 30px;               /* this padding is the visual size of the margin area */
  position: relative;
  border: 2px dashed #e67e22; /* dashed line marks the area */
  display: inline-block;
}

/* border area: red tones */
.box-border {
  background-color: #fad7d7;  /* light red (border area) */
  border: 16px solid #e74c3c; /* the border itself is the border area */
  position: relative;
  display: inline-block;
}

/* padding area: green tones */
.box-padding {
  background-color: #d5f5e3;  /* light green (padding area) */
  padding: 30px;               /* this padding is the size of the padding area */
  position: relative;
  display: inline-block;
}

/* content area: blue tones */
.box-content {
  background-color: #d6eaf8;  /* light blue (content area) */
  width: 120px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #2980b9;
}

/* Labels for each area */
.label {
  position: absolute;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 3px;
  top: 4px;
  left: 6px;
}

.label-margin    { color: #e67e22; background: rgba(255,255,255,0.7); }
.label-border    { color: #c0392b; background: rgba(255,255,255,0.7); }
.label-padding   { color: #27ae60; background: rgba(255,255,255,0.7); }
.label-content   {
  position: static;    /* the content box is shown centered as static */
  color: #1a5276;
  font-size: 0.75rem;
  font-weight: 700;
}

/* =============================================
   2. box-sizing comparison
   ============================================= */
.sizing-demo {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
}

/* content-box: default, actual size = width + padding + border */
.sizing-content-box {
  width: 300px;
  padding: 20px;
  border: 4px solid #e74c3c;
  box-sizing: content-box;    /* default: only the content is 300px */
  background-color: #fde8e8;
  border-radius: 6px;
}

/* border-box: actual size = width (including padding and border) */
.sizing-border-box {
  width: 300px;
  padding: 20px;
  border: 4px solid #27ae60;
  box-sizing: border-box;     /* recommended: the total is 300px */
  background-color: #eafaf1;
  border-radius: 6px;
}

.sizing-content-box strong { color: #e74c3c; }
.sizing-border-box strong  { color: #27ae60; }

.result {
  margin-top: 8px;
  font-weight: 700;
  font-size: 0.9rem;
  color: #333;
}

/* =============================================
   3. display property comparison
   ============================================= */

/* Block: takes up an entire line */
.display-block {
  display: block;
  background-color: #d6eaf8;
  border: 2px solid #2980b9;
  padding: 10px 16px;
  margin-bottom: 8px;
  border-radius: 4px;
  font-weight: bold;
  color: #1a5276;
}

/* Inline: arranged side by side (width/height ignored) */
.display-inline {
  display: inline;
  background-color: #fde8c8;
  border: 2px solid #e67e22;
  padding: 4px 10px;          /* top/bottom padding only works visually */
  border-radius: 4px;
  font-weight: bold;
  color: #784212;
  /* width: 100px; ← ignored on inline elements */
}

/* Inline block: arranged side by side + width/height can be set */
.display-inline-block {
  display: inline-block;
  background-color: #d5f5e3;
  border: 2px solid #27ae60;
  padding: 10px 16px;
  border-radius: 4px;
  font-weight: bold;
  color: #1e8449;
  width: 130px;               /* inline-block allows setting the width */
  text-align: center;
  margin: 4px 2px;
}

/* =============================================
   4. overflow property
   ============================================= */
.overflow-demo {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

/* Common: all overflow boxes */
.overflow-demo > div {
  width: 200px;
  height: 100px;             /* a fixed height makes the content overflow */
  border: 2px solid #666;
  padding: 10px;
  border-radius: 6px;
  font-size: 0.85rem;
  background-color: #fafafa;
}

.overflow-demo strong {
  display: block;
  margin-bottom: 6px;
  font-size: 0.8rem;
  color: #333;
}

/* Default: overflows outside */
.overflow-visible { overflow: visible; border-color: #e74c3c; }

/* Clipped */
.overflow-hidden  { overflow: hidden;  border-color: #27ae60; }

/* Scroll when needed */
.overflow-auto    { overflow: auto;    border-color: #3498db; }

/* =============================================
   5. Margin collapse
   ============================================= */
.collapse-demo {
  background-color: #fafafa;
  border: 1px dashed #aaa;
  padding: 0 20px;
  border-radius: 6px;
}

.collapse-box {
  background-color: #d6eaf8;
  border: 2px solid #2980b9;
  padding: 16px;
  text-align: center;
  border-radius: 6px;
  font-size: 0.9rem;
  color: #1a5276;
  font-weight: bold;
  margin-bottom: 30px;  /* 30px bottom margin */
  margin-top: 30px;     /* 30px top margin: the two margins collapse → only 30px applies */
}

/* =============================================
   6. Centering with margin auto
   ============================================= */
.centered-box {
  width: 200px;
  margin: 0 auto;         /* auto left/right → horizontal centering */
  background-color: #d5f5e3;
  border: 2px solid #27ae60;
  padding: 20px;
  text-align: center;
  border-radius: 8px;
  font-size: 0.9rem;
  color: #1e8449;
  font-weight: bold;
}

/* =============================================
   Footer
   ============================================= */
.page-footer {
  background-color: #2c3e50;
  color: #aab7b8;
  text-align: center;
  padding: 20px;
  font-size: 0.85rem;
}

Assignment

Assignment — Build a Card Component

Goal

Understand the box model (padding, border, margin) precisely,
and build the profile card component that is used often in practice.


Requirements

Card structure (HTML)

<div class="card">
  <div class="card-image">
    <!-- Replace with an image or a background color -->
  </div>
  <div class="card-body">
    <h2 class="card-name">John Doe</h2>
    <p class="card-role">Frontend Developer</p>
    <p class="card-desc">Hello! I am studying CSS.</p>
    <div class="card-tags">
      <span class="tag">HTML</span>
      <span class="tag">CSS</span>
      <span class="tag">JavaScript</span>
    </div>
  </div>
</div>

CSS requirements

Property Requirement
box-sizing Apply border-box globally
width Card width 300px (or use max-width)
padding At least 24px on card-body
border A 1px border around the whole card, or a box-shadow
border-radius Rounded card corners (12px or more)
margin Place the card in the center of the screen (margin: auto)
card-image Height 180px, use a background color in place of an image

Extra challenges (optional)

  • Set .tag to display: inline-block and apply padding/border-radius
  • Add a :hover effect to the card (the shadow becomes stronger)
  • Try arranging 3 cards side by side (slightly beyond the scope of this lesson, but give it a try!)

Checklist

  • Applied * { box-sizing: border-box; }
  • The card is placed in the center of the screen
  • Used padding and margin as distinct concepts
  • The corners are rounded with border-radius
  • The .tag elements are arranged side by side
  • Did not use inline CSS

Hints

/* Card image area: a background color instead of an image */
.card-image {
  height: 180px;
  background-color: #3498db; /* any color you like */
  border-radius: 12px 12px 0 0; /* round only the top corners */
}

/* Tags: arrange them side by side with inline-block */
.tag {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 20px;
  background-color: #eee;
  font-size: 0.8rem;
}

If you are curious about the answer, refer to the solution/ folder.

Learn this lesson with a mentor

Work through the Build Your Homepage series on your own, or join a guided cohort at our Pitampura campus and online.

Call NowEnroll Now