CSS Layout: Flexbox & Grid
Every essential Flexbox and CSS Grid property: containers, items, alignment, and spacing, with syntax and real use cases.61 commands · 5 sections
Flexbox and Grid are the two layout systems of modern CSS. This cheatsheet covers the properties you use daily: flex containers and items, grid containers and items, alignment and gap spacing, and the tricks that solve real layout problems.
Every entry shows the property with example values followed by the use case: when and why you reach for it.
Flexbox: Container17
display: flexflex-direction: rowflex-direction: columnflex-direction: row-reverseflex-wrap: wrapflex-wrap: nowrapjustify-content: space-betweenjustify-content: centerjustify-content: flex-endjustify-content: space-aroundjustify-content: space-evenlyalign-items: centeralign-items: flex-startalign-items: stretchgap: 8pxrow-gap: 12px; column-gap: 16pxflex-flow: row wrapFlexbox: Items11
flex: 1flex: 0 0 200pxflex: 2flex-grow: 0flex-shrink: 0flex-basis: 33.33%order: -1align-self: flex-endmargin-left: automargin: automin-width: 0Grid: Container12
display: gridgrid-template-columns: 1fr 1fr 1frgrid-template-columns: repeat(12, 1fr)grid-template-columns: 200px 1frgrid-template-columns: minmax(200px, 1fr) 2frgrid-template-columns: repeat(auto-fit, minmax(150px, 1fr))grid-template-rows: auto 1fr autogrid-template-areas:
"header header"
"sidebar main"
"footer footer"grid-auto-rows: 100pxgrid-auto-flow: densegrid-template-columns: auto 1fr autocolumn-gap: 16px; row-gap: 24pxGrid: Items9
grid-column: span 2grid-column: 1 / 3grid-column: 1 / -1grid-row: span 2grid-row: 1 / 3grid-area: 1 / 1 / 3 / 4justify-self: centeralign-self: endgrid-column: span 2 / -1Alignment & Spacing12
place-items: centerjustify-content: centeralign-content: centerplace-content: centerjustify-items: stretchalign-items: startgap: 16pxmargin: 0 autopadding: 0 16pxtext-align: centeraspect-ratio: 16 / 9object-fit: coverCSS Layout: Flexbox & Grid
Every essential Flexbox and CSS Grid property: containers, items, alignment, and spacing, with syntax and real use cases.
Flexbox and Grid are the two layout systems of modern CSS. This cheatsheet covers the properties you use daily: flex containers and items, grid containers and items, alignment and gap spacing, and the tricks that solve real layout problems.
Every entry shows the property with example values followed by the use case: when and why you reach for it.
Flexbox: Container
display: flex: Turn an element into a flex container: children become flex items on one axis.flex-direction: row: Main axis horizontal: items left to right (default).flex-direction: column: Main axis vertical: stack items top to bottom.flex-direction: row-reverse: Reverse horizontal order: items right to left.flex-wrap: wrap: Allow items to wrap to new lines instead of shrinking.flex-wrap: nowrap: Force all items onto one line (default).justify-content: space-between: Main axis: push items apart with space between: navbar layout.justify-content: center: Main axis: center items: hero buttons.justify-content: flex-end: Main axis: push items to the end.justify-content: space-around: Equal space around each item: half-gaps at the edges.justify-content: space-evenly: Fully equal gaps everywhere: even at edges.align-items: center: Cross axis: center items: the classic vertical centering.align-items: flex-start: Cross axis: align items to the start: uneven heights stay top-aligned.align-items: stretch: Items fill the cross axis (default): equal-height rows.gap: 8px: Space between items: modern, no margin hacks.row-gap: 12px; column-gap: 16px: Separate horizontal and vertical gaps for wrapped flex.flex-flow: row wrap: Shorthand for direction + wrap in one line.Flexbox: Items
flex: 1: Item grows to fill available space, sharing equally with siblings.flex: 0 0 200px: Fixed size: never grow, never shrink, exactly 200px.flex: 2: Grow twice as fast as flex: 1 siblings: the 2:1 split layout.flex-grow: 0: Prevent growth: button stays its content size.flex-shrink: 0: Prevent shrinking: logos and icons never squish.flex-basis: 33.33%: Base size before growing/shrinking: three equal columns.order: -1: Reorder items: move an item to the front without touching HTML.align-self: flex-end: Override the container's align-items for ONE item.margin-left: auto: Push an item to the far end: the "auto margin" layout trick.margin: auto: Center an item BOTH ways: the ultimate centering hack.min-width: 0: Allow flex items to shrink below content size: fixes overflow in text.Grid: Container
display: grid: Turn an element into a grid container.grid-template-columns: 1fr 1fr 1fr: Three equal columns: 1fr = one fraction of free space.grid-template-columns: repeat(12, 1fr): Twelve equal columns: the 12-col bootstrap pattern.grid-template-columns: 200px 1fr: Fixed sidebar + fluid content.grid-template-columns: minmax(200px, 1fr) 2fr: Column that never shrinks below 200px.grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)): Auto-responsive columns: items wrap as space allows. The modern card grid.grid-template-rows: auto 1fr auto: Header/content/footer rows: the app shell.grid-template-areas:
"header header"
"sidebar main"
"footer footer": Named areas: readable, visual layout definition.grid-auto-rows: 100px: Default row height for implicitly created rows.grid-auto-flow: dense: Back-fill gaps: dense packing for mixed-size tiles.grid-template-columns: auto 1fr auto: Auto-sized edges + fluid middle: nav/status layouts.column-gap: 16px; row-gap: 24px: Gaps between grid tracks.Grid: Items
grid-column: span 2: Item spans 2 columns: feature cards.grid-column: 1 / 3: Item from column line 1 to 3: explicit placement.grid-column: 1 / -1: Span the FULL width: full-width rows in a grid.grid-row: span 2: Item spans 2 rows: tall tiles.grid-row: 1 / 3: Explicit row placement.grid-area: 1 / 1 / 3 / 4: Full placement shorthand: row-start / col-start / row-end / col-end.justify-self: center: Align one item horizontally inside its cell.align-self: end: Align one item vertically: sticky footer buttons.grid-column: span 2 / -1: Span to the end: items that stretch to the right edge.Alignment & Spacing
place-items: center: Center ALL items in both axes: flex-center equivalent for grid.justify-content: center: Center the whole grid inside the container.align-content: center: Center grid rows vertically when there is spare space.place-content: center: Center the entire grid both ways: the page-level centering.justify-items: stretch: Items fill their cell horizontally (default): equal-width grid items.align-items: start: Grid items align to the top of their cells: uneven content heights.gap: 16px: Universal gap shorthand: works in flex AND grid.margin: 0 auto: Center a block element horizontally: classic, still works.padding: 0 16px: Inset spacing: breathing room inside containers.text-align: center: Center inline content: text, inline images.aspect-ratio: 16 / 9: Fixed aspect ratio boxes: video embeds and image placeholders.object-fit: cover: Crop images to fill their box: image cards without distortion.Frequently asked questions
What is the difference between Flexbox and Grid?
Flexbox is one-dimensional: it lays items along a single axis and is ideal for navbars, buttons, and small component layouts. Grid is two-dimensional with explicit rows and columns, best for full page layouts.
When should I use align-items vs justify-content?
In Flexbox, justify-content aligns along the main axis and align-items along the cross axis. In Grid, they follow the same rule for the current writing direction: justify aligns inline, align aligns block.
What does flex: 1 mean?
flex: 1 expands to flex-grow: 1, flex-shrink: 1, flex-basis: 0%: the item grows to fill available space and shares it equally with other flex: 1 siblings. Use flex: none to prevent growing or shrinking.
How do I center an element?
For a flex or grid container, margin: auto on the child is the classic trick. For fixed-size elements, absolute positioning with inset: 0 and margin: auto also works. flex centering is the most reliable.