CSS Reference ยท Practical Pages
Choosing a layout method
Compare CSS layout methods side by side: flexbox, grid, floats, positioning and multi-column. Learn which one fits which job, with a live comparison.
CSS offers several layout systems, and each was designed for a different job. Picking the right one makes your code shorter and sturdier. The short version: flexbox arranges items along one line, grid places items in rows and columns together, floats wrap text around a picture, positioning takes an element out of the flow, and multi-column flows text through columns.
.toolbar { display: flex; gap: 8px; } /* one dimension */ .gallery { display: grid; grid-template-columns: repeat(3, 1fr); } /* two dimensions */
Try it
The same three items, laid out three ways. Each approach produces a row of three equal cards, but they work very differently.
The float version needs manual width math and a container that contains its floats (display: flow-root), and its cards do not stretch to equal heights. Flexbox and grid make equal heights the default.
How they compare
| Method | Best for | Not for |
|---|---|---|
| Flexbox | Navigation bars, toolbars, centering, rows or columns of items that share space | Strict two-dimensional alignment of rows and columns |
| Grid | Page layouts, galleries, forms, dashboards, anything with aligned rows and columns | Simple one-line groups (flex is lighter) |
| Float | Text wrapping around an image or pull quote | Page or component layout |
| Positioning | Badges, tooltips, overlays, sticky headers, anchored popups | Normal page flow; absolute elements take no space |
| Multi-column | Long text or lists flowing into newspaper-style columns | Interactive card layouts |
| Inline-block / table display | Legacy code only | New work |
Choosing quickly
- Is it a single row or column of items? Use flexbox.
- Do items need to line up in both directions, or do you want named areas? Use grid.
- Does text need to wrap around something? Use
float(orshape-outside). - Should an element sit on top of or attached to something? Use positioning, usually with
insetor anchor positioning. - Is it flowing text? Use multi-column.
Usage notes
- Mix them. A page is often a grid of regions, each region a flex container for its content, with a positioned badge on top.
- Content-out versus layout-in. Flexbox sizes starting from the items' content; grid starts from the track definition you write. Choose by which side should drive.
- Grid with
auto-fitandminmax()gives responsive columns without media queries; flexbox withflex-wrapgives a looser version where the last row's items stretch. - Avoid layout hacks. Percentage widths with float, negative margins and table layouts are replaced by
gap,frunits and alignment properties. - Accessibility. Reordering items visually with
order, grid placement or positioning does not change the reading and tab order, so keep the source order logical.
Browser support
Flexbox and grid are supported by all current browsers, while newer pieces such as subgrid and anchor positioning have more recent support; check a compatibility table for those.