Flexbox Container Properties
Apply these properties to a flex container (the parent element).
| Property | Values | Description |
|---|---|---|
| display | flex | inline-flex | Enables flexbox layout. flex = block-level, inline-flex = inline-level. |
| flex-direction | row | column | row-reverse | column-reverse | row (default) = left-to-right, column = top-to-bottom, *-reverse = reversed direction. |
| flex-wrap | nowrap | wrap | wrap-reverse | nowrap (default) = single line, wrap = break to next line, wrap-reverse = wrap upwards/backwards. |
| justify-content | flex-start | flex-end | center | space-between | space-around | space-evenly | Align items along the main axis (horizontal in row, vertical in column). |
| align-items | flex-start | flex-end | center | stretch | baseline | Align items along the cross axis. stretch (default) = fill the container. |
| align-content | flex-start | flex-end | center | space-between | space-around | stretch | Align wrapped lines. Only works with flex-wrap: wrap. |
| gap | <length> | <percentage> | Space between flex items. E.g., gap: 20px or gap: 10px 20px (row/column gap). |
flexbox-container-example.css
Common flexbox container setup
Flexbox Item Properties
Apply these properties to flex items (the children inside the container).
| Property | Values | Description |
|---|---|---|
| flex-grow | <number> (default: 0) | How much an item grows relative to others. flex-grow: 1 = equal share of remaining space. |
| flex-shrink | <number> (default: 1) | How much an item shrinks relative to others when space is tight. |
| flex-basis | auto | <length> | <percentage> | Base size of an item before growing/shrinking. auto uses the item's width/height. |
| flex | <flex-grow> <flex-shrink> <flex-basis> | Shorthand. E.g., flex: 1 1 auto or flex: 1 (grow, shrink, basis). |
| align-self | auto | flex-start | flex-end | center | stretch | baseline | Override container's align-items for a single item. |
| order | <integer> (default: 0) | Change visual order without changing HTML. Lower numbers appear first. Use carefully for accessibility. |
| margin | <length> | auto | In flexbox, margin: auto = perfect centering. Can push items apart. |
flexbox-item-example.css
Common flexbox item setup
Grid Container Properties
Apply these properties to a grid container (the parent element).
| Property | Values | Description |
|---|---|---|
| display | grid | inline-grid | Enables CSS Grid. grid = block-level, inline-grid = inline-level. |
| grid-template-columns | <track-size> ... | repeat() | fr | minmax() | Define column widths. E.g., 1fr 2fr 1fr, repeat(3, 1fr), 200px auto 1fr. |
| grid-template-rows | <track-size> ... | repeat() | fr | minmax() | Define row heights. Same syntax as grid-template-columns. |
| gap (grid-gap) | <length> | <percentage> | Space between grid cells. E.g., gap: 20px or gap: 10px 20px (row/column). |
| grid-auto-flow | row | column | dense | Direction to place auto-placed items. dense = fill gaps. Default is row. |
| grid-auto-columns | <track-size> | minmax() | fr | Size of implicitly created columns (not defined in grid-template-columns). |
| grid-auto-rows | <track-size> | minmax() | fr | Size of implicitly created rows (not defined in grid-template-rows). |
| grid-template-areas | "<string>" ... | Named grid areas. E.g., "header header" "nav main" "footer footer". Use with grid-area on items. |
| justify-items | start | end | center | stretch | Align items horizontally within their cells. |
| align-items | start | end | center | stretch | Align items vertically within their cells. |
| justify-content | start | end | center | space-between | space-around | space-evenly | Align grid columns horizontally within the container. |
| align-content | start | end | center | space-between | space-around | space-evenly | Align grid rows vertically within the container. |
grid-container-example.css
Common grid container setup
Grid Item Properties
Apply these properties to grid items (the children inside the container).
| Property | Values | Description |
|---|---|---|
| grid-column | <start> / <end> | span <number> | Span columns. E.g., 1 / 3 (columns 1-2), span 2 (span 2 columns). |
| grid-row | <start> / <end> | span <number> | Span rows. Same syntax as grid-column. |
| grid-area | <name> | <row-start> / <col-start> / <row-end> / <col-end> | Assign item to a named area. E.g., grid-area: header or grid-area: 1 / 1 / 2 / 3. |
| justify-self | auto | start | end | center | stretch | Override container's justify-items for a single item (horizontal alignment). |
| align-self | auto | start | end | center | stretch | Override container's align-items for a single item (vertical alignment). |
grid-item-example.css
Common grid item setup
Units & Functions for Layout
| Unit/Function | Example | Description |
|---|---|---|
| fr | 1fr, 2fr, 3fr | Fractional unit (grid only). Divides available space proportionally. E.g., 1fr 2fr = 1:2 ratio. |
| repeat() | repeat(3, 1fr) | Repeat a pattern. repeat(3, 1fr) = three equal columns. repeat(auto-fit, minmax(200px, 1fr)) = responsive. |
| minmax() | minmax(100px, 1fr) | Min and max size. Column min 100px, max 1fr (flexible). Used in grid-template-columns. |
| auto-fit / auto-fill | repeat(auto-fit, minmax(200px, 1fr)) | auto-fit = collapse empty tracks, auto-fill = keep empty tracks. For responsive grids. |
| px, em, rem, % | 200px, 10em, 5rem, 50% | Absolute and relative units. em = relative to parent font-size, rem = relative to root. |
layout-units-example.css
Practical examples with units
Pro tips & gotchas
Flexbox vs. Grid:
Use flexbox for 1D layouts (rows or columns, simple alignment). Use Grid for 2D layouts (rows + columns, complex positioning).
Flexbox centering:
Perfect center with display: flex; justify-content: center; align-items: center;
Grid span shorthand:
grid-column: 1 / span 2 = start at column 1, span 2 columns. Or grid-column: 1 / 3 = same thing.
fr unit gotcha:
1fr is relative to remaining space, not total space. If columns are 200px 1fr 1fr, the two 1fr columns split remaining space equally.
auto-fit vs auto-fill:
Both create responsive grids. auto-fit collapses empty tracks (shrinks grid), auto-fill keeps them (full width). Usually use auto-fit.
Grid template areas:
Name areas for readable layouts: grid-template-areas: "header header header" "nav main aside" "footer footer footer". Then grid-area: header on items.
Africa & India's Best AI Academy
Master CSS layout.
Build responsive sites.
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ