Lesson 09 / Layout

CSS Box Model and Layout Techniques

Time
55 min
Type
Reading + Interactive
Level
Beginner

Understand how elements are visually rendered, how their dimensions are calculated, and how display, float, flexbox, and grid affect layout.

Teacher Notes / In-Class Use

Demo Live

  • Walk through the interactive demo before students start changing their own project files.
  • Connect the demo back to the first goal: Explain content, padding, border, margin, and box-sizing

Try In Class

  • Build a content card using padding, border, margin, and box-sizing.
  • Have students make one visible change, save, refresh, and explain what changed.

Submit Or Check

  • Ask students to show the work in the browser, not only in the editor.
  • Have students commit their progress with a clear message when the checkpoint is stable.

Watch For

  • Students copying code without checking file paths, spelling, or capitalization.
  • Visual changes that work locally but break when the project is published.

Learning Goals

  • Explain content, padding, border, margin, and box-sizing
  • Compare block-level and inline element behavior
  • Identify common CSS layout techniques

Interactive Demo

How to use this demo.

Use the demo as a small lab. Change one thing, observe the result, then connect it back to your own project.

What To Try

  • Adjust content width, padding, border, and margin to see each layer change.
  • Switch box-sizing and compare how the total rendered size responds.

What Changes

  • The visual box updates while the code sample shows the related CSS.
  • Spacing changes become easier to diagnose when each layer is separated.

What To Notice

  • Padding is inside the border, margin is outside the border.
  • border-box makes element sizing easier to reason about in layouts.

Apply It

  • Inspect one project card or content block and identify its content, padding, border, and margin.

Interactive Demo

Box Model Visualizer

Adjust the controls to see how width, padding, border, margin, and box-sizing change the space an element occupies.

margin
border
padding
content
.box {
  box-sizing: content-box;
  width: 220px;
  padding: 24px;
  border: 6px solid;
  margin: 24px;
}

What to notice

  • Padding creates space inside the border.
  • Margin creates space outside the border.
  • box-sizing changes how width is calculated.

Try this

  • Increase padding and watch the content move inward.
  • Switch between content-box and border-box.
  • Recreate the generated CSS in your own card component.

This demo uses extra JavaScript for teaching. The code sample shows the pattern to practice. View full demo source (opens in a new tab).

What This Teaches

This lesson teaches how every visible element on a webpage is drawn as a box.

You will learn how content, padding, border, margin, and box-sizing affect the space an element takes up.

You will also learn how basic layout techniques like display, float, flexbox, and grid change how elements arrange themselves.

Why It Matters

Most layout problems are box model problems first.

When spacing looks strange, elements overflow, columns do not line up, or a card is wider than expected, the answer is often padding, margin, border, or box-sizing.

Understanding the box model gives you a reliable way to debug layout instead of guessing at random CSS properties.

Core Concept: Block vs. Inline Behavior

HTML elements are broadly categorized as either block-level or inline elements, each with distinct behaviors.

Block-level elements

  • Occupy the full width available to them by default.
  • Create line breaks before and after the element, stacking vertically.
  • Respect width and height properties.
  • Padding, margins, and borders apply and affect the layout of surrounding elements.
  • Examples: h1, p, div, ul, form, table

Inline elements

  • Only occupy the space needed for their content.
  • Flow within a line of text, not creating line breaks.
  • Ignore width and height properties.
  • Vertical padding, margins, and borders apply but don't affect the positioning of other inline elements.
  • Horizontal padding, margins, and borders apply and affect the positioning of other inline elements.
  • Examples: span, a, img, strong, em

Core Concept: The CSS Box Model

Every element on a web page is treated as a rectangular box, composed of content, padding, border, and margin.

Box Layer: Content

The actual content of the element, such as text, images, or other media. The height and width of the content area determine the element's natural size.

Box Layer: Padding

The space between the content and the border. Padding is transparent and expands the background color of the element.

p {
  background-color: lightblue;
  padding: 20px;
}

Box Layer: Border

A visual boundary surrounding the padding and content. Borders can have different styles, colors, and thicknesses.

div {
  border: 2px solid black;
}

Box Layer: Margin

The space outside the border, creating separation between elements. Margins are transparent and collapse with adjacent margins.

h1 {
  margin-bottom: 1em;
}

Core Concept: box-sizing

The box-sizing property determines how the width and height of an element are calculated.

Many developers prefer border-box because it makes element sizing easier to predict.

  • content-box: The default. Width and height include only the content area.
  • border-box: Width and height include content, padding, and border, but not margin.
div {
  width: 200px;
  padding: 20px;
  border: 5px solid black;
  box-sizing: border-box; /* Total width will still be 200px */
}

Core Concept: Layout Techniques

CSS includes several layout techniques for controlling how elements are displayed and arranged.

Different layout tools solve different layout problems. The trick is choosing the right one.

Code Example: display Property

  • display: none; hides the element completely.
  • display: block; makes the element a block-level element.
  • display: inline; makes the element inline.
  • display: inline-block; combines block and inline behaviors.
.hidden {
  display: none;
}

.button-like-link {
  display: inline-block;
  padding: 0.5rem 1rem;
}

Float

float: left; or float: right; allows content to wrap around the floated element. Use sparingly due to complexity.

Flexbox

display: flex; enables the Flexbox layout model for one-dimensional layouts.

.card-row {
  display: flex;
  gap: 1rem;
}

Grid

display: grid; allows for two-dimensional layouts with rows and columns.

.card-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

Common Mistakes

  • Forgetting that padding and border can increase the final size of an element when using content-box.
  • Using margin when padding would better create internal space.
  • Using padding when margin would better separate two elements.
  • Trying to set width or height on inline elements and expecting it to work like a block.
  • Using float for modern page layout when flexbox or grid would be simpler.
  • Not accounting for margin collapse between vertical block elements.

Debugging Checklist

  • Inspect the element in browser developer tools and look at the box model diagram.
  • Check whether the element is block, inline, inline-block, flex, or grid.
  • Temporarily add a border or background color to see the real size of the element.
  • Check whether spacing is coming from padding, margin, gap, or line-height.
  • Set box-sizing: border-box when sizing feels unpredictable.
  • Remove layout rules one at a time to find which property is creating the issue.

Resources

MDN: The box model | MDN: display | CSS-Tricks: box-sizing

Checkpoint

Before moving on, make sure these feel true.

  • I can explain the main concept in my own words.
  • I can apply this lesson to my current project.
  • I can verify the result in the browser.
  • I can commit the change with a clear message.

Project Connection

This lesson supports current class projects.

Practice

  • Build a content card using padding, border, margin, and box-sizing.
  • Create a simple two-column layout.