HW01

Web Layout

Start with the box model, organize pages with Grid and Flexbox, and adapt layouts using container queries.

Web layout answers three questions: How much space does an element occupy? How are multiple elements arranged? How is space redistributed when a container becomes narrower? Instead of using unrelated examples, the following six steps build one visualization course project gallery.

Case brief: a gallery for visualization course projects

We are creating a gallery for a visualization course. Students move between sections in the header, filter by topic and year in the sidebar, browse projects such as Urban Heat, Commuting Flows, and Air Quality in the main region, and find course notes and data sources in the footer. The page can stand alone or be embedded in a narrow course platform, so it must rearrange itself as its container changes.

The six steps develop the same page in sequence: create a predictably sized project card, preserve its content flow while positioning a badge, build the page with Grid, arrange the project toolbar with Flexbox, add container queries, and verify the result. Along the way, whitespace, visual hierarchy, Gestalt grouping, reading order, and information priority explain why the layout works—not only how to write its CSS. The first five steps use independent “Explanation / Key code / Demo” tabs; the sixth connects all the results.

Step 1: define the project card box model

Begin with the case’s smallest repeated unit: a project card. Separating content, padding, border, and card spacing prevents the later three-column layout from overflowing because of incorrect size calculations.

Allocate space to the first project card

A project card has four layers from inside out: content holds the title and summary, padding creates breathing room inside the boundary, border marks the visible edge, and margin separates the card from external elements.

The default content-box adds padding and borders outside the declared width. With border-box, they are included. This case applies box-sizing: border-box so the card width in CSS matches the visible width.

Use padding inside each card and let the Grid’s gap control consistent space between cards. Project titles may be long, so Grid items must also be allowed to shrink instead of forcing the page wider.

From a visualization perspective, whitespace is not “empty.” It is a visual variable that establishes grouping. Under the Gestalt principle of proximity, a card’s closely spaced title, metadata, and summary are perceived as one group; a larger gap between cards signals the start of another project. A border adds a common-region cue by enclosing related information. Too many borders create visual noise, so use whitespace first and add a border only when the card boundary remains unclear.

Decision order for this step

  1. Set the project card’s visible width and use border-box.
  2. Use padding inside cards and gap between cards.
  3. Use min-width: 0 where long titles must be able to shrink.
  4. Check that spacing within a card is smaller than spacing between cards, so grouping is recognizable at a glance.

Write a predictably sized project card

project-card.html
<article class="project-card">
<p class="project-card__meta">Map · 2026</p>
<h2>Urban Heat Island Map</h2>
<p>Compare temperature and green-space coverage.</p>
</article>
project-card.css
*, *::before, *::after {
box-sizing: border-box;
}
.project-card {
width: 320px;
padding: 20px;
border: 2px solid #93c5fd;
}

The card’s visible width is 320px; the content area automatically subtracts horizontal padding and borders. With the default content-box, the visible width becomes 364px (320 + 40 + 4), making the future three-column layout more likely to exceed its intended width. The metadata, title, and summary share the same padding, so they read as one project.

Inspect the project card’s four layers

The diagram separates the card into margin → border → padding → content. It shows containment rather than actual scale. Confirm which layers the declared width includes and that spacing between cards sits outside the border. This step produces a predictably sized base card.

margin · outer spacing
border · boundary
padding · inner spacing
content
Project summary
Project card box-model layers (not to scale): margin → border → padding → content
Visible cardContent, padding, and border make up the project card.
Space between cardsMargin separates neighboring project cards and is excluded from the declared width.

Step 2: keep card content in normal flow

With predictable dimensions in place, add the project title, summary, and link. They should take space in sequence and determine the card’s height; only the “NEW” badge needs to overlay the top-right corner.

Let the main content flow naturally

In normal flow, block boxes follow the block axis and inline content wraps when space runs out. The case card uses position: relative to establish a positioning context without losing its place. Its badge uses position: absolute and leaves normal flow.

The title, summary, and “View project” link remain unpositioned, so they still determine the card’s height. Absolute positioning suits this local overlay; it is not a sound foundation for the entire header, sidebar, or project region.

As summaries grow, cards should grow naturally. Give only genuine scrolling regions explicit size constraints and overflow: auto. Do not hide errors with overflow: hidden, which can clip text and keyboard focus.

Normal flow also carries the card’s reading order: category and year first, then title, summary, and the path into the project. Keep visual order close to DOM order instead of using positioning to make content merely look correct. Visual scanning, keyboard navigation, and screen readers then receive the same information narrative. The badge is secondary status: it may make a new project easier to recognize, but it should not become more prominent than the project title.

Positioning rule for this step

Keep structural content in normal flow and position only status information that needs no reserved space. Then check that the badge does not cover the heading and that keyboard focus remains visible.

Take only the project badge out of normal flow

positioned-project-card.html
<article class="project-card">
<span class="project-card__badge">NEW</span>
<p class="project-card__meta">Map · 2026</p>
<h2>Urban Heat Island Map</h2>
<p>Compare temperature and green-space coverage.</p>
<a href="#urban-heat">View project</a>
</article>
positioned-project-card.css
.project-card {
position: relative;
padding: 24px;
}
.project-card__badge {
position: absolute;
inset-block-start: 12px;
inset-inline-end: 12px;
}

The parent stays in normal flow; only the badge leaves it. The title, summary, and link take space in sequence and naturally make the project card taller when its summary grows.

Compare two treatments of the project card

The left card keeps every item in normal flow. The right card positions only the project badge in its top-right corner. This step confirms that a local overlay does not change the title, summary, and link order or remove the card’s natural height.

Card content stays in normal flowTitle, summary, and link take space in sequence
Urban Heat Island Map
Compare temperature and green-space coverage
View project
Position only the project badgeThe badge leaves normal flow; card content keeps its place
NEW
Urban Heat Island Map
Compare temperature and green-space coverage
View project

The project badge may overlay the card locally, while the title, summary, and link still determine its height.

Step 3: build the project gallery with Grid

Now place the stable card inside the complete page. The case needs a header, filter sidebar, project region, and footer, while the project region itself contains three cards. This two-dimensional relationship is a good fit for Grid.

Describe the case regions as tracks

The outer Grid uses 160px minmax(0, 1fr): the filter sidebar has a fixed 160px track and the project region fills the rest. The 0 in minmax(0, 1fr) lets the main track shrink so long project titles do not force the whole page wider.

Because the case regions have clear roles, grid-template-areas names header, sidebar, main, and footer. A nested Grid in the project region uses repeat(3, minmax(0, 1fr)) for the three cards.

Grid distributes space; it does not replace HTML semantics. Keep header, aside, main, and footer in the document and use CSS to control their visual arrangement.

These regions also establish a visual hierarchy. Course navigation provides global context, the filter sidebar affects which projects are shown, the project region carries the primary browsing task, and footer material comes last. Regular grid lines create alignment, equal card widths support rapid comparison, and the gap between the sidebar and projects separates “controls” from “results” through proximity. The flow also follows a common visualization pattern: provide an overview, let people filter it, then offer details on demand through each card.

Combine the header, filters, and project region

project-gallery.html
<div class="project-gallery">
<header class="gallery-header">Visualization Project Gallery · Course navigation</header>
<aside class="gallery-sidebar">Filter projects by topic and year</aside>
<main class="gallery-main">
<div class="project-grid">
<article>Urban Heat</article>
<article>Commuting Flows</article>
<article>Air Quality</article>
</div>
</main>
<footer class="gallery-footer">Course notes and data sources</footer>
</div>
project-gallery.css
.project-gallery {
display: grid;
grid-template-columns: 160px minmax(0, 1fr);
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 16px;
}
.gallery-header { grid-area: header; }
.gallery-sidebar { grid-area: sidebar; }
.gallery-main { grid-area: main; min-width: 0; }
.gallery-footer { grid-area: footer; }
.project-grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 16px;
}

Inspect the project page’s region mapping

The demo maps the case header, filter sidebar, project region, and footer to named areas, then arranges three concrete project cards inside the main region. This step produces a recognizable project-gallery structure.

Do not inspect only whether the regions form two columns. Also ask whether the eye reaches the project region first, whether the three projects are easy to compare, and whether filters are clear without overpowering the primary content.

HEADERVisualization Project Gallery · Course navigation
ASIDEFilter projectsTopic · Year
MAINCourse projects
Urban HeatCommuting FlowsAir Quality

grid-template-areas establishes shared boundaries and alignment between filters and results; a nested Grid makes the three project cards easy to compare side by side.

Step 4: arrange the project toolbar with Flexbox

With the page structure in place, add an overview toolbar above the cards. Its left side shows “Course projects / 12 projects,” and its right side summarizes map, chart, and story projects. These items follow one primary axis, making Flexbox appropriate.

Distribute the toolbar’s remaining space

display: flex arranges items horizontally by default. justify-content: space-between places the title and type summary at opposite ends, while align-items: center aligns them vertically.

The title group uses flex: 1 1 220px and min-width: 0, so it can fill remaining space and still shrink. The type summary keeps its intrinsic width. With flex-wrap: wrap, the summary moves below the title in a narrow embed instead of being clipped.

This step also demonstrates nesting: Grid manages the page regions, while Flexbox manages one row inside the project region.

The toolbar also demonstrates visual hierarchy and consistent encoding. Type size and weight make “Course projects” the local heading. The count is supporting information: weaker than the heading, but stronger than ordinary notes. Maps, charts, and stories use the same shape and style because they are peer categories. Assigning each an arbitrary color would add unnecessary color semantics and make the interface less robust for people with color-vision differences.

Put the project heading and type summary on one row

project-toolbar.html
<div class="project-toolbar">
<div class="project-toolbar__title">
<strong>Course projects</strong>
<span>12 projects</span>
</div>
<div class="project-toolbar__summary" aria-label="Project type summary">
<span>Maps · 4</span>
<span>Charts · 5</span>
<span>Stories · 3</span>
</div>
</div>
project-toolbar.css
.project-toolbar {
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
gap: 12px 20px;
}
.project-toolbar__title {
display: flex;
align-items: baseline;
flex: 1 1 220px;
min-width: 0;
gap: 12px;
}
.project-toolbar__summary {
display: flex;
flex: 0 1 auto;
flex-wrap: wrap;
gap: 8px;
}

Change the project toolbar’s real width

Select “Narrow container” to constrain the toolbar demo to 320px; the type summary moves below the heading. Select “Fluid width” to restore the available width. This step leaves the toolbar complete and readable in either container.

Keep evaluating hierarchy as you switch widths: after wrapping, the heading should still be read before the category summary, and all three categories should retain the same encoding rather than changing meaning with space.

Change the project toolbar container width
Course projects12 projects
Maps · 4Charts · 5Stories · 3

Wide container: the project heading establishes visual hierarchy, while all three summaries use consistent encoding on the same row.

Main axis: justify-contentCross axis: align-items

Step 5: adapt the gallery to its container

The wide layout is complete, but the gallery must also work inside the course platform. In a narrow container, filters should move above the project list and three card columns should become one. Simply shrinking everything does not solve those spatial relationships.

Rearrange the case based on its own width

Declare container-type: inline-size on the gallery embed, then query its inline size with @container. The same gallery can now respond to the width it actually receives in either a full page or a narrow course-platform region.

Container queries suit this reusable region. @media still suits viewport-level changes such as site navigation and page margins. A breakpoint should answer a concrete problem: Is the sidebar squeezing the project region? Can three cards still sit comfortably side by side?

The case uses 560px as its rearrangement point. Below it, the outer layout and project-card Grid both become a single column. No project content is removed, and the sidebar width setting temporarily has no effect.

Responsive visualization should preserve the user’s task and information priority, not the desktop layout’s geometry. A wide container supports side-by-side comparison; a narrow one supports sequential reading. Reflow may change position, but it should retain key semantics such as project title, type, year, and entry point. Choose a breakpoint when comparison becomes difficult or labels become crowded, not because it happens to match a named device.

responsive-project-gallery.css
.gallery-embed {
container-type: inline-size;
}
.gallery-layout {
display: grid;
grid-template-columns: 160px minmax(0, 1fr);
gap: 16px;
}
.project-grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 16px;
}
@container (max-width: 560px) {
.gallery-layout,
.project-grid {
grid-template-columns: 1fr;
}
}

“Narrow container” constrains the case preview to 360px, below the 560px breakpoint. You can therefore see the rearrangement without resizing the browser window.

First adjust the filter sidebar, region gap, and region padding to see which space each parameter controls. Then select “Narrow container.” The goal is to move filters above the project list, stack all three cards, and preserve every piece of content.

Treat this as an information-design experiment. An oversized sidebar weakens the overview, too little gap merges controls with results, and too much padding lowers information density. There is no universally correct number; the useful value keeps grouping, comparison, and reading tasks working together.

Project gallery layout lab

Adjust the filter sidebar, region gap, and padding to complete the case’s responsive layout.

HEADERVisualization Project Gallery · Course navigation
ASIDEFilter projectsTopic · Year
MAIN

Course projects

Urban Heat
Commuting Flows
Air Quality

Two-column project page: the filter sidebar has a fixed width and the project region fills the remaining space.

Information-design objective

As you adjust spacing, check that filters and results form clear groups and that the three cards are easy to compare. Then select “Narrow container” and confirm that the layout becomes a reading sequence while preserving the “filter → browse projects” task path.

Step 6: verify the complete case

The first five steps have progressed from one project card to a complete responsive gallery. Replace “looks about right” with checks that prove each step supports the same case.

Follow the case through verification

  1. Inspect grouping: temporarily ignore the borders and confirm that whitespace alone distinguishes “inside one card” from “between neighboring cards.”
  2. Inspect reading order: scan category and year, title, summary, and link in sequence, then use the Tab key and document structure to confirm that the DOM tells the same story.
  3. Inspect overview and comparison: verify that the project region is easy to find, cards align for comparison, and the filter sidebar is clear without overpowering the results.
  4. Inspect visual hierarchy: verify a stable strength relationship among the page title, project-region title, project titles, and supporting metadata. Badges and category summaries should not take the primary attention.
  5. Inspect changing space: switch between wide and narrow containers. The layout should move from side-by-side comparison to sequential reading while preserving project type, year, title, and entry point.
  6. Inspect extreme states: add long titles, an empty result, and more categories, then check overflow, grouping, color dependence, and keyboard focus.

Keep the case’s adjustable parameters together

project-gallery-tokens.css
.project-gallery {
--content-max: 72rem;
--region-gap: 16px;
--region-padding: 16px;
--sidebar: 160px;
width: min(100%, var(--content-max));
margin-inline: auto;
padding-inline: var(--region-padding);
container-type: inline-size;
}
.project-gallery__body {
display: grid;
grid-template-columns: var(--sidebar) minmax(0, 1fr);
gap: var(--region-gap);
}
@container (max-width: 560px) {
.project-gallery__body {
grid-template-columns: 1fr;
}
}

These properties correspond to the controls in Step 5. When a problem appears, you can identify whether the filter sidebar, region gap, region padding, or container breakpoint caused it.

Case completion criteria

  • In a wide container, the filter sidebar has a fixed width, the project region fills the remaining space, and three cards sit side by side.
  • Grid manages the two-dimensional page regions, Flexbox arranges the one-dimensional toolbar, and gap expresses space between siblings.
  • Long project titles and summaries do not break out of their cards or create unnecessary horizontal scrolling.
  • In a narrow container, filters and projects stack, cards use one column, and all content remains visible, readable, and operable.
  • Whitespace and common regions express project grouping, alignment supports comparison, and headings and supporting metadata form a clear visual hierarchy.
  • Layout changes preserve the “overview → filter → view a project” task path rather than merely keeping the CSS from breaking at different widths.

Further reading

This chapter draws on MDN’s official learning materials: CSS layout, The box model, Block and inline layout in normal flow, Positioning, CSS grid layout, Flexbox, CSS container queries, and Overflowing content. Follow them in the same six-step order and reproduce the case with browser developer tools.

For the visualization theory behind these decisions, continue with Tamara Munzner’s Visualization Analysis and Design for task-driven design, Colin Ware’s Information Visualization: Perception for Design for perception, grouping, and visual attention, and Ben Shneiderman’s The Eyes Have It for the interaction path from overview through filtering to details on demand. Together, they move the evaluation beyond “does the CSS work?” toward “is the information easy to find, group, compare, and understand?”