🎨 Phase 2 · CSS 🟡 Beginner+ MODULE 06

Box Model & Layout

⏱️ 40 min
📖 Theory + Code
🧩 5 Questions
🏗️ 1 Challenge
Phase 2 progress40%
🎯 What you'll learn: Every HTML element is a box. You'll master the four layers of the CSS box model (content, padding, border, margin), why box-sizing: border-box is essential, all display values (block/inline/inline-block/none), CSS positioning (static/relative/absolute/fixed/sticky), z-index, and overflow.

The CSS Box Model

Every element on a webpage is a rectangular box. The box model has four layers, from inside out: content, padding, border, and margin. Understanding this model is the single most important concept in CSS layout.

MARGIN
BORDER
PADDING
CONTENT
width × height
box_model.css
CSS
.card {
  /* Content */
  width: 300px;
  height: 200px;   /* or auto to fit content */

  /* Padding — space INSIDE the border */
  padding: 20px;                    /* all 4 sides */
  padding: 20px 40px;               /* top-bottom left-right */
  padding: 10px 20px 30px 40px;    /* top right bottom left (clockwise) */
  padding-top: 10px;               /* individual sides */

  /* Border */
  border: 2px solid #4f9eff;      /* width style color */
  border-radius: 12px;             /* rounded corners */
  border-radius: 50%;              /* perfect circle (if w=h) */
  border-top: 4px solid orange;   /* one side only */

  /* Margin — space OUTSIDE the border */
  margin: 20px;                    /* all 4 sides */
  margin: 0 auto;                  /* centre block horizontally */
  margin-bottom: 2rem;             /* space below */
}

/* ✅ ALWAYS add this to your CSS reset */
* {
  box-sizing: border-box;
  /* padding and border are INCLUDED in width/height */
  /* Without this: width=300, padding=20 → total = 340px (surprise!) */
  /* With this:    width=300, padding=20 → total = 300px (expected!)  */
}
⚠️
Always set box-sizing: border-box as your first CSS rule
Without border-box, setting width: 300px and padding: 20px gives you a 340px wide element — surprising! With border-box, padding and border are included in the width, so it stays 300px. Every modern CSS framework and professional stylesheet starts with *, *::before, *::after { box-sizing: border-box; }.

The display Property

The display property determines how an element is laid out. It's one of the most powerful properties in CSS and unlocks every layout technique.

ValueBehaviourExample Elements
blockFull width, starts on new line, respects width/height/margindiv, p, h1-h6, section
inlineFlows in text, ignores width/height/top-bottom marginspan, a, strong, em
inline-blockFlows inline but respects width, height, padding, marginimg (default), button
noneCompletely hidden — removed from layout (takes no space)Hidden elements
flexEnables Flexbox for children — next lesson!Navigation, cards
gridEnables CSS Grid for children — lesson 8!Page layouts
contentsElement disappears, children take its placeWrapper removal
display_examples.css
CSS
/* Make inline elements behave like blocks */
a.btn {
  display: inline-block;
  padding: 12px 24px;     /* works on inline-block! */
  width: 200px;            /* works on inline-block! */
  background: blue;
  color: white;
}

/* Hide an element completely */
.mobile-only {
  display: none;  /* invisible AND takes no space */
}

/* vs visibility: hidden = invisible but KEEPS its space */
.ghost {
  visibility: hidden;
}

/* vs opacity: 0 = invisible, keeps space, still clickable! */
.transparent {
  opacity: 0;
}

CSS Positioning

The position property controls how an element is placed in the document flow. It unlocks top, right, bottom, and left offset properties. Understanding position is essential for navbars, modals, tooltips, and overlays.

positioning.css
CSS
/* STATIC (default) — in normal document flow */
.normal { position: static; } /* top/right/bottom/left ignored */

/* RELATIVE — offset from its normal position */
.nudged {
  position: relative;
  top: 10px;    /* moves DOWN 10px from where it would be */
  left: 20px;   /* moves RIGHT 20px */
  /* original space is still reserved! */
}

/* ABSOLUTE — removed from flow, positioned relative to nearest */
/* positioned ancestor (parent with position != static)     */
.parent { position: relative; }   /* make parent the reference */
.badge {
  position: absolute;
  top: -8px;
  right: -8px;  /* top-right corner of parent */
}

/* FIXED — stays in viewport as page scrolls */
nav {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 999;  /* stays above everything */
}

/* STICKY — relative until scroll threshold, then fixed */
.sticky-header {
  position: sticky;
  top: 0;        /* sticks at top of viewport */
  z-index: 100;
}

/* Z-INDEX — stacking order (higher = in front) */
.modal-overlay { z-index: 1000; }
.modal         { z-index: 1001; }
.tooltip       { z-index: 1002; }
/* z-index only works on positioned elements (not static)! */
💡
The "containing block" rule for absolute positioning
An absolutely positioned element looks for the nearest ancestor with position: relative, absolute, fixed, or sticky — and positions itself relative to that. If none found, it uses the viewport. So always add position: relative to the parent when using absolute positioning on children. This is one of the most common layout bugs in CSS.

Width, Height & Overflow

Controlling how elements handle their size and what happens when content overflows the box are essential CSS skills for building robust, responsive layouts.

sizing_overflow.css
CSS
/* === WIDTH & HEIGHT === */
.container {
  width: 100%;          /* fills parent */
  max-width: 1200px;    /* never wider than 1200px */
  min-width: 320px;    /* never narrower than 320px */
  margin: 0 auto;       /* centres horizontally */
}

.hero {
  height: 100vh;       /* full viewport height */
  min-height: 400px;  /* at least 400px tall */
}

.auto-height {
  height: auto;        /* grows with content (default) */
}

/* === OVERFLOW === */
.box {
  overflow: visible;  /* default: spills out of box */
  overflow: hidden;   /* clips content at box edge */
  overflow: scroll;   /* always shows scrollbars */
  overflow: auto;     /* scrollbars only when needed ← use this */

  overflow-x: auto;   /* horizontal scroll only */
  overflow-y: hidden; /* no vertical overflow */
}

/* Truncate text with ellipsis */
.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;  /* shows "..." */
  max-width: 200px;
}

/* overflow: hidden clips child ::before and rounded borders too */
.image-card {
  border-radius: 12px;
  overflow: hidden;    /* clips the img to the rounded corners */
}
🧩 Knowledge Check
5 questions — Box Model & Layout
1. In the CSS box model, from inside to outside, what is the correct order?
2. What does box-sizing: border-box do?
3. What is the difference between display: none and visibility: hidden?
4. What position value makes an element scroll with the page but stick once it reaches the top of the viewport?
5. An absolutely positioned element needs a positioned ancestor. Which parent property creates that ancestor?
🏗️
Challenge — Profile Card with Badge
Box model + positioning + overflow all in one component
Task: Build a profile card using the box model and positioning:

1. A .card with position: relative, border-radius: 16px, overflow: hidden, fixed width of 280px, and a subtle box shadow
2. A .card-image that fills the card width, fixed height 200px, with object-fit: cover (use a Picsum img)
3. A .card-body with padding of 1.5rem
4. A .badge with position: absolute at top-right showing "New" — styled with orange background, white text, border-radius
5. A .card-title with text-overflow: ellipsis (truncate long names) and .card-desc clamped to 2 lines with overflow: hidden
6. A button link at the bottom using display: block
💡 Show hints
  • object-fit: cover on img makes it fill without distortion
  • Badge: .badge { position: absolute; top: 12px; right: 12px; }
  • Text clamp: overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  • Box shadow: box-shadow: 0 4px 20px rgba(0,0,0,0.3);
card.html + card.css — Sample Solution
HTML+CSS
/* card.css */
* { box-sizing: border-box; margin: 0; padding: 0; }
body { background: #05091a; display: flex; justify-content: center; padding: 4rem 2rem; }

.card {
  position: relative;
  width: 280px;
  background: #0a1230;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 8px 40px rgba(0,0,0,0.5);
  border: 1px solid rgba(255,255,255,.08);
}

.card-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
  display: block;
}

.badge {
  position: absolute;
  top: 12px;
  right: 12px;
  background: #f97316;
  color: white;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 20px;
  letter-spacing: 0.05em;
}

.card-body { padding: 1.5rem; }

.card-title {
  font-size: 1.1rem;
  font-weight: 700;
  color: #eef3ff;
  margin-bottom: 0.5rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-desc {
  font-size: 0.85rem;
  color: #8ca8d8;
  margin-bottom: 1.2rem;
  line-height: 1.6;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.card-btn {
  display: block;
  text-align: center;
  padding: 0.7rem;
  background: linear-gradient(135deg, #4f9eff, #1a78ff);
  color: white;
  border-radius: 8px;
  text-decoration: none;
  font-weight: 700;
  font-size: 0.85rem;
}
🎉
Lesson 6 Complete!
Box model and positioning mastered! Next — Flexbox, the modern way to arrange elements in a line.
← Course Home
Phase 2 · CSSLesson 6 of 5