Lecture 3 of 1419 minInteractiveFree lecture

CSS: the box model and layout

Every element is a box. Learn how CSS sizes boxes with content, padding, border and margin, why box-sizing matters, and how Flexbox and Grid arrange boxes into real layouts — with live playgrounds.

In this lecture you will

  • Explain how CSS rules, selectors and the cascade decide an element’s styles
  • Calculate an element’s size from the box model, with and without border-box
  • Build one-dimensional layouts with Flexbox
  • Know when to reach for CSS Grid

HTML gives a page its structure; CSS decides how it looks and where everything goes. Layout used to be the most frustrating part of web development. Modern CSS has made it far more predictable — once you understand the model underneath.

Rules, selectors and the cascade

A CSS rule pairs a selector with declarations:

.card {
  padding: 24px;
  border-radius: 12px;
  background: white;
}

When several rules target the same element, the cascade decides which wins. In short: more specific selectors beat less specific ones (an ID beats a class, which beats an element name); if specificity is equal, the rule that comes later wins. Some properties, such as color and font-family, are also inherited by child elements.

Specificity is counted as three numbers — IDs, classes, element names — compared left to right. Switch rules on and off below; your browser applies them to a real button and reports which colour won:

InteractiveWho wins the cascade?Click rules to switch them on and off
Loading visual…

One ID outweighs any number of classes. Inline styles beat every selector, and !important beats everything else — which is why it causes arms races. Keep specificity low and even, for example one class per rule, so the order of your stylesheet stays in charge.

Everything is a box

The browser draws every element as a rectangle made of four layers, from the inside out:

Content
The text or image itself. width and height apply here by default.
Padding
Space inside the border, around the content. It shows the element’s background.
Border
A line around the padding, with its own width, style and colour.
Margin
Transparent space outside the border that pushes neighbouring boxes away.

Adjust each layer below, and switch box-sizing to see why so many developers get widths wrong the first time.

InteractiveThe box model, liveDrag the sliders
Loading visual…

With the default content-box, adding padding makes an element wider than the width you set. Nearly every modern project starts with this reset so that width means the visible width:

*, *::before, *::after {
  box-sizing: border-box;
}

Flexbox: layout in one direction

Flexbox arranges children in a row or a column and shares space between them. It is the everyday tool for navigation bars, button groups, toolbars and centering things. The CSS in the panel is exactly what the playground applies.

InteractiveFlexbox playgroundChange the properties
Loading visual…

The key idea is two axes. The main axis follows flex-direction; justify-content distributes items along it. The cross axis is perpendicular; align-items positions items across it. Switch the direction to column and notice that the two properties swap visual roles.

Grid: layout in two directions

When you need rows and columns — page layouts, image galleries, dashboards — use CSS Grid:

.dashboard {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 20px;
}

This single rule creates as many 260-pixel-minimum columns as fit the screen and stretches them to fill the row, so the layout adapts from phones to wide monitors automatically. Drag the container width below and watch the browser add and remove columns:

InteractiveResponsive columns without media queriesDrag the container width
Loading visual…

The column sizes are read back from your browser. With fewer cards than columns, auto-fill keeps the empty tracks; auto-fit collapses them, so the cards stretch to fill the row.

For a classic page, named areas read almost like a sketch:

.page {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
}

Flexbox or Grid?

A simple rule of thumb: Flexbox for one direction, Grid for two. Flexbox is content-driven — items size themselves and the container distributes leftover space. Grid is layout-driven — you define the tracks and place items into them. Real pages use both: a Grid for the page skeleton and Flexbox inside components.

With structure and layout covered, the next lecture brings the page to life with JavaScript and events.

Key takeaways

  • Each element is a box of content, padding, border and margin; width applies to the content unless box-sizing is border-box.
  • Most projects set box-sizing to border-box everywhere so widths behave predictably.
  • Flexbox arranges items along one direction and distributes space between them.
  • Grid arranges items in rows and columns at the same time — ideal for page and card layouts.

Check your understanding

3 questions · answers explained

Q1An element has width 200px, padding 20px and a 5px border with the default box-sizing. How wide is its visible box?

With content-box (the default), width sets only the content. Padding and border are added on both sides — 200 + 2 × 20 + 2 × 5 = 250px.

Q2Which Flexbox property spreads navigation links so the first is at the start, the last at the end and the space is shared between them?

justify-content controls distribution along the main axis. space-between pushes the first and last items to the edges and shares the remaining space equally between items.

Q3You need a dashboard of cards that wraps into as many columns as fit the screen. What is the most natural tool?

Grid’s auto-fill with minmax creates as many equal columns as fit, and reflows automatically on different screen sizes — without a single media query.