liveBook · Manning

Fluid layout

As explained in CSS in Depth, Second Edition

A fluid layout is a web layout whose dimensions adapt continuously to the available viewport size rather than relying on fixed dimensions.

A fluid layout uses containers that grow and shrink with the viewport instead of fixed dimensions. This keeps the page within the available screen and avoids horizontal scrolling on smaller devices. section 7.3

Build fluid layouts by applying these principles:

  • Leave the main container without an explicit width, or define its width as a percentage; use padding or auto margins for breathing room. section 7.3
  • Define columns with percentages, such as a 70% main column and a 30% sidebar, so they continue to fit at different viewport widths. section 7.3
  • Use flexbox when its items can grow and, especially, shrink enough to fit the available width. section 7.3
  • Preserve the browser’s responsive starting point: block elements fit the viewport and inline content wraps before added styles introduce overflow. section 7.3

A fluid page with responsive images

A page can divide its content into a 70% main column and a 30% sidebar, then prevent images from exceeding their container with `img { max-inline-size: 100%; }`. A responsive `srcset` can provide small, medium, and large image files for different display conditions. section 7.3 section 7.4.2

Listing 7.15 Responsive srcset image
<img alt="A white coffee mug on a bed of coffee beans"
     src="coffee-beans-small.jpg"          #1
     srcset="coffee-beans-small.jpg 560w,      #2
             coffee-beans-medium.jpg 800w,     #2
             coffee-beans.jpg 1280w"           #2
/>

Compare image candidates in a `srcset` and let the browser select an appropriate resolution while the image remains constrained by its container. section 7.4.2

How CSS in Depth, Second Edition covers it

How deep it goes
Explains definitions, fixed-layout contrasts, percentage sizing, default browser behavior, flexbox constraints, and image overflow prevention.
Examples
Uses one-, two-, or three-column pages, percentage-based main and sidebar columns, and responsive srcset images.
What it assumes
Starts from the browser’s default responsive behavior before adding CSS constraints that developers must keep from causing overflow.
What you can do after
Build it from scratch by sizing containers fluidly, allowing columns to shrink, and preventing images from overflowing.
How it connects
Connects fluid layout with responsive design, mobile-first design, media queries, flexbox, responsive images, and container queries.

Also explained in

Compare how 3 books explain fluid layout →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.