CSS Layout — Flexbox & Grid
Flexbox and Grid properties for modern CSS layouts — alignment, sizing, responsive patterns.
CSS Flexbox and Grid are the two modern layout systems that replace floats and table-based layouts. Flexbox is ideal for one-dimensional layouts (rows or columns), while Grid excels at two-dimensional layouts with rows and columns simultaneously.
This cheatsheet covers the essential properties for both systems, including alignment, sizing, and responsive patterns.
Flexbox — Container
display: flexflex-direction: row | columnflex-wrap: wrapjustify-content: center | space-betweenalign-items: center | stretch | flex-startgap: 1remFlexbox — Items
flex: 1align-self: center | stretchorder: 1Grid — Container
display: gridgrid-template-columns: 1fr 1fr 1frgrid-template-rows: auto 1fr autogrid-template-areas:
"header header"
"sidebar main"Grid — Items
grid-column: span 2grid-column: 1 / 3grid-area: headerAlignment & Spacing
justify-items: center | stretchalign-items: center | stretchplace-items: centerplace-content: centerCSS Layout — Flexbox & Grid
Flexbox and Grid properties for modern CSS layouts — alignment, sizing, responsive patterns.
CSS Flexbox and Grid are the two modern layout systems that replace floats and table-based layouts. Flexbox is ideal for one-dimensional layouts (rows or columns), while Grid excels at two-dimensional layouts with rows and columns simultaneously.
This cheatsheet covers the essential properties for both systems, including alignment, sizing, and responsive patterns.
Flexbox — Container
display: flex — Enable Flexbox on a container. Children become flex items.flex-direction: row | column — Set the main axis direction. Row (default) or column.flex-wrap: wrap — Allow items to wrap onto multiple lines instead of shrinking.justify-content: center | space-between — Align items along the main axis.align-items: center | stretch | flex-start — Align items along the cross axis.gap: 1rem — Set spacing between flex items (row-gap and column-gap shorthand).Flexbox — Items
flex: 1 — Shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0. Item grows to fill space.align-self: center | stretch — Override align-items for a specific flex item.order: 1 — Reorder a flex item. Lower numbers come first.Grid — Container
display: grid — Enable CSS Grid on a container.grid-template-columns: 1fr 1fr 1fr — Define the column track sizes. fr units distribute available space.grid-template-rows: auto 1fr auto — Define row track sizes. Classic sticky footer pattern.grid-template-areas:
"header header"
"sidebar main" — Name grid areas for visual layout mapping. Use named grid-area on children.Grid — Items
grid-column: span 2 — Make an item span multiple column tracks.grid-column: 1 / 3 — Place an item from grid line 1 to line 3.grid-area: header — Assign an item to a named area defined in grid-template-areas.Alignment & Spacing
justify-items: center | stretch — Align grid items along the inline (row) axis within their cells.align-items: center | stretch — Align grid items along the block (column) axis within their cells.place-items: center — Shorthand for align-items + justify-items. Centers items in their cells.place-content: center — Shorthand for align-content + justify-content. Centers the entire grid.