CSS Flexbox is a practical choice for arranging content in one dimension: across a row or down a column. This guide provides copy-ready CSS flexbox examples for navigation bars, responsive cards, centered components, wrapping controls, equal-height columns, and sticky footers. It also explains which properties belong on the flex container, which belong on its children, and when CSS Grid is a better fit.
Overview
Flexbox turns an element into a flex container with display: flex. Its direct children then become flex items that can grow, shrink, wrap, and align themselves along a main axis and a cross axis.
The default direction is a horizontal row. In that mode, the main axis runs from left to right and the cross axis runs from top to bottom. If you set flex-direction: column, the axes change: the main axis becomes vertical and the cross axis becomes horizontal.
.layout {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
gap: 1rem;
}
Flexbox is especially useful when the content size is not known in advance. A navigation bar may need to distribute links, a toolbar may need to wrap controls, and a card row may need to let items share available space. For layouts that require precise control of both rows and columns at the same time, Grid may be easier to reason about.
How to compare options
Start by identifying the direction of the layout and the behavior you need at smaller widths. Flexbox properties fall into two groups:
- Container properties control the layout context, including direction, wrapping, spacing, and alignment.
- Item properties control an individual child, including its ability to grow, shrink, change order, or align itself.
Then compare the layout against three practical questions:
- Is the layout one-dimensional? Use Flexbox when the primary concern is a row or a column. Use Grid when both rows and columns need coordinated tracks.
- Should items have equal or content-based sizes? Use
flexfor flexible sizing, or set a basis and minimum width when cards should remain readable. - What should happen when space runs out? Choose between shrinking, wrapping, scrolling, or switching direction with a media query.
Do not treat justify-content and align-items as fixed horizontal and vertical controls. Their effect depends on flex-direction. In a row, justify-content controls the horizontal main axis; in a column, it controls the vertical main axis.
| Need | Typical choice | Why |
|---|---|---|
| Arrange items in one row or column | Flexbox | It models a single flow direction clearly. |
| Align a component’s contents | Flexbox | align-items, justify-content, and gap solve common alignment tasks. |
| Build a two-dimensional page structure | CSS Grid | Rows and columns can be defined together. |
| Allow controls to move onto new lines | Flexbox with wrapping | flex-wrap: wrap preserves source order while using available space. |
Feature-by-feature breakdown
Responsive navigation bar
Use a flexible spacer to push a navigation group to the opposite side. The HTML order remains meaningful, while the layout handles distribution.
<header class="site-header">
<a class="logo" href="/">Example</a>
<nav class="nav-links" aria-label="Primary navigation">
<a href="/docs">Docs</a>
<a href="/guides">Guides</a>
<a href="/about">About</a>
</nav>
</header>
.site-header {
display: flex;
align-items: center;
gap: 1.5rem;
padding: 1rem;
}
.nav-links {
display: flex;
flex-wrap: wrap;
gap: 1rem;
margin-inline-start: auto;
}
@media (max-width: 40rem) {
.site-header {
align-items: flex-start;
flex-direction: column;
}
.nav-links {
margin-inline-start: 0;
}
}
Use real links and a labelled navigation landmark rather than relying on visual positioning. Avoid changing the visual order in a way that conflicts with keyboard or screen-reader expectations.
Centered content
For a component that must be centered on both axes, make its parent a flex container and provide a usable height. min-height is often safer than a fixed height because content can grow.
.centered-panel {
display: flex;
align-items: center;
justify-content: center;
min-height: 18rem;
padding: 2rem;
}
If the centered content is a form or message, test it with long text and increased text size. Centering should not cause content to be clipped or become difficult to reach on a short viewport.
Responsive card grid with wrapping
Flexbox can create a useful card layout when each card has a sensible minimum size. The combination of flex, min-width, and flex-wrap lets cards share space without becoming too narrow.
.card-list {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 16rem;
min-width: 0;
padding: 1.25rem;
border: 1px solid #d6d9df;
border-radius: 0.5rem;
}
.card h2 {
overflow-wrap: anywhere;
}
flex: 1 1 16rem is shorthand for a grow value of 1, a shrink value of 1, and a preferred basis of 16rem. It does not guarantee identical card widths in every row because the final row may contain fewer items. If strict columns and rows matter, compare this approach with a Grid layout.
Equal-height columns
Flex items stretch across the cross axis by default. This can produce equal-height columns when the container uses the default align-items: stretch.
.column-layout {
display: flex;
align-items: stretch;
gap: 1.5rem;
}
.column {
flex: 1 1 0;
}
@media (max-width: 48rem) {
.column-layout {
flex-direction: column;
}
}
Be careful when a child has a fixed height or when content overflows. Equal visual height is not a substitute for readable spacing and sensible content lengths.
Wrapping controls and toolbars
Wrapping is usually preferable to allowing buttons to become too small. Set a gap on the container and let items retain their natural width.
.toolbar {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 0.75rem;
}
.toolbar .search {
flex: 1 1 16rem;
}
.toolbar button {
flex: 0 0 auto;
}
Keep the DOM order aligned with the intended interaction order. A toolbar should remain usable with a keyboard, and focus indicators should not be hidden by overflow or clipped containers.
Sticky footer structure
A common page pattern uses a column flex container so the main content expands while the footer stays after it. This does not make the footer fixed to the viewport; it simply prevents short pages from leaving it awkwardly high.
<body class="page">
<header>Header</header>
<main class="page-content">Main content</main>
<footer>Footer</footer>
</body>
.page {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.page-content {
flex: 1;
}
Use min-height: 100dvh when your project specifically needs a viewport-aware height that accounts for dynamic browser UI, but test the result on the target devices. The simpler 100vh pattern remains useful when that distinction is not important.
Important defaults and debugging checks
A flex container uses flex-direction: row, flex-wrap: nowrap, and align-items: stretch by default. Flex items can also shrink by default, which may cause long words, inputs, or nested layouts to appear unexpectedly narrow.
.flex-child {
min-width: 0;
}
.flex-child img {
display: block;
max-width: 100%;
height: auto;
}
When a layout breaks, inspect the nearest flex container first. Confirm its direction, available width, wrapping behavior, gap, and minimum sizes. Then inspect the item’s flex, min-width, max-width, and overflow rules. Browser developer tools can highlight the flex container and show the free space being distributed.
Best fit by scenario
- Use Flexbox for navigation, toolbars, form rows, media objects, and component internals. These patterns usually have one dominant direction and benefit from content-aware sizing.
- Use wrapping Flexbox for card collections when approximate rows are acceptable. Set a practical basis and minimum size, then test long titles and translated text.
- Use Grid for a page shell or a strict card matrix. Grid is generally clearer when row and column tracks must line up across separate items.
- Use normal flow for ordinary document content. Flexbox should solve a layout requirement, not replace the browser’s natural block and inline layout everywhere.
For accessibility, preserve a logical source order, keep controls reachable by keyboard, and avoid using order merely to create a visual reading sequence. Check layouts at narrow widths, with zoom, with larger text, and with content that is longer than the sample copy. These tests reveal failures that a single desktop viewport can hide.
When experimenting, a flexbox playground is useful for changing flex-direction, justify-content, align-items, flex-wrap, and gap while observing the result. For a repeatable project fix, move the working rules into a small isolated test case and then integrate them with the component’s actual markup.
When to revisit
Revisit a Flexbox layout whenever its inputs change: a new navigation link is added, card content becomes longer, a component gains an icon or button, a breakpoint changes, or the design moves from one language to another. Also review it when a browser implementation, CSS specification detail, or project baseline changes. The safest practice is to verify behavior in the browsers and devices your project supports rather than relying on an old assumption.
Use this short review routine:
- Resize from a wide viewport to a narrow one and watch for overflow, unintended shrinking, or controls that become unreachable.
- Replace sample text with long headings, error messages, and translated strings.
- Test keyboard navigation and confirm that visual order matches the interaction order.
- Inspect nested flex containers for missing
min-width: 0or conflicting fixed widths. - Compare the result with CSS Grid if the layout now depends on coordinated rows and columns.
- Record the intended behavior in a component test or documentation example so a later change does not silently reintroduce the problem.
Flexbox works best when the layout’s direction and fallback behavior are explicit. Start with the smallest relevant container, choose container and item properties deliberately, and keep a tested snippet for the pattern you expect to reuse.