Size Boxes From Their Container

In the last lesson, you gave the events column a max-width and centered it with auto margins. The banner above it still shows only a strip of the garden photo, as tall as the heading, the welcome line, and their padding. Rosa would like the events page to open on the garden itself, with the banner filling the screen until a visitor scrolls down to the events.

In this lesson, you’ll learn where a box’s width and height come from when you don’t set them, so you can predict both before you save. You’ll also see why the first thing most people write for a full-screen banner silently does nothing and what to write instead. Start with the size you’ve relied on all along, the width.

See Where Sizes Come From

When you haven’t set a size, both width and height are auto, but auto means something different in each direction.

A block’s auto width comes from its container. You saw this in Follow Normal Flow: a block takes the full width available inside its parent. In a window 1,280 pixels wide, the banner is 1,280 pixels wide because <main> is. Each event card is 652 pixels wide, the section’s 700 pixels minus its 24 pixels of padding on each side.

A block’s auto height comes from its content. The browser lays out the work day card’s heading and paragraphs at the card’s width, adds its padding and border, and that’s its height. Make the window narrower, and the lines wrap, so the card gets taller.

If you think of layout as function calls, the parent passes each child its width as an argument, and the child returns its height once its content is laid out. The picture holds for blocks in normal flow, but you never write the calls, and the browser makes them again whenever the window changes size. At the end of this lesson, you’ll also meet widths that come from the content instead.

Percentages follow the same direction. A percentage width is a share of the container’s content width, not of the window. To confirm this, add a width to the .event-card rule in events.css:

Language: CSS Filename: events/events.css
.event-card {
  background-color: honeydew;
  padding: 16px 24px;
  border: 2px solid var(--green);
  margin-bottom: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 8px oklch(0 0 0 / 0.2);
  width: 50%;
}

Save and scroll down to the Coming Up section:

In the centered green column, the work day card fills only the left half, and the seed swap card is as narrow, with its photo sticking far out to the right.
Half of the Column, Not Half of the Window

Each card is now 326 pixels wide, half of the section’s 652 pixels of content width, and its padding and border fit inside those 326 pixels, as border-box makes them. The seed swap photo is still 800 pixels wide, so it sticks out even further. Delete width: 50%; again and save, so the cards fill the column.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.