CSS Flexbox Guide & Reference
Master 1D responsive layouts with CSS Flexible Box. Learn main vs cross axis alignment, flex grow/shrink math, gap spacing, and centering techniques.
Flexbox In-Depth Overview & Use Cases
CSS Flexbox (Flexible Box Layout) revolutionized CSS layout design by providing a powerful 1-dimensional mechanism for distributing space and aligning items along a single axis (either horizontally in a row or vertically in a column). Before Flexbox, centering content vertically or creating fluid equal-height columns required complex floating hacks, table-display overrides, or absolute positioning tricks.
Flexbox is designed for component-level UI development. It gives child items the ability to dynamically grow to fill excess container space, shrink to prevent clipping, or wrap onto additional lines on mobile screens. Whether you are building navigation bars, search filter toolbars, or responsive card groups, Flexbox is the industry-standard layout choice.
Key Real-World Use Cases
Distributing brand logos, navigation links, and action buttons across header rows.
Perfectly centering login dialogs, modals, and spinners both vertically and horizontally.
Aligning input text boxes with submit buttons and icon attachments.
Aligning circular user profile avatars side-by-side with multi-line comment text.
What is CSS Flexbox?
CSS Flexbox (Flexible Box Layout) is a 1-dimensional layout model designed for distributing space along a single row or column. Unlike traditional block or inline layouts, Flexbox elements dynamically expand or shrink to fit available viewport space.
- Main Axis: The primary axis defined by flex-direction (Row: horizontal left-to-right; Column: vertical top-to-bottom).
- Cross Axis: The axis perpendicular to the main axis (Row cross axis is vertical; Column cross axis is horizontal).
- Flex Container: The parent element with display: flex or display: inline-flex applied.
- Flex Items: Direct children of the flex container that obey flex alignment rules.
Flex Container Properties Reference
These properties are applied to the parent container to control direction, alignment, and distribution of flex items:
| Property | Possible Values | Description |
|---|---|---|
| flex-direction | row | row-reverse | column | column-reverse | Sets the main axis direction. |
| justify-content | flex-start | flex-end | center | space-between | space-around | space-evenly | Aligns items along the main axis. |
| align-items | stretch | flex-start | flex-end | center | baseline | Aligns items along the cross axis. |
| flex-wrap | nowrap | wrap | wrap-reverse | Controls whether items wrap onto multiple lines. |
| gap | <length> (e.g. 1rem, 16px) | Sets spacing between flex items without extra margins. |
| align-content | flex-start | flex-end | center | space-between | space-around | stretch | Aligns multi-line flex tracks along cross axis. |
Flex Item Properties Reference
Child flex items can control their individual sizing, alignment, and visual order. The most common sizing property is the shorthand flex: <flex-grow> <flex-shrink> <flex-basis>.
| Property | Description |
|---|---|
| flex | Shorthand for flex-grow, flex-shrink, and flex-basis. |
| align-self | Allows a single item to override the container's align-items property. |
| order | Changes the visual rendering order of items (default is 0) without altering HTML. |
The Powerful Auto-Margin Flex Trick
Setting margin-left: auto or margin-top: auto on a single flex item pushes it to the far edge of the container, absorbing all free space on that axis.
Perfect Centering with Flexbox
Flexbox simplifies vertical and horizontal alignment into 3 lines of code: