CSS Flexbox revolutionized web layout when it arrived, and it remains one of the most powerful tools in modern frontend development. Short for Flexible Box Module, Flexbox gives developers a simple, predictable way to arrange elements within a container, regardless of screen size or content amount. Before Flexbox, creating responsive layouts meant battling floats, clearing hacks, and wrestling with inline-block whitespace issues. Flexbox eliminated all of that.
Today, Flexbox is supported in over 99 percent of browsers worldwide, making it a reliable foundation for any web project. Whether you are building a simple navigation bar, a complex dashboard, or a full-page application, Flexbox provides the layout superpowers you need with minimal code.
If you prefer a visual approach to building layouts, our Flexbox Generator lets you create and customize flexbox layouts in real time without writing CSS by hand. You can experiment with all the properties covered in this guide and see results instantly.
What Is CSS Flexbox?
Flexbox is a one-dimensional layout model that distributes space along a single axis, either horizontally (row) or vertically (column). Unlike older layout methods that relied on block and inline flow, Flexbox treats elements as flexible items that can grow, shrink, and reorder themselves to fill available space.
The system works through two main components:
- The flex container - the parent element that enables flexbox behavior
- The flex items - the direct children of the container that flexbox arranges
When you set display: flex on a container, all its direct children become flex items. From there, you can control their direction, alignment, order, and sizing using a set of well-designed properties.
Setting Up a Flex Container
The journey begins with the container. Apply display: flex or display: inline-flex to any element to turn it into a flex container:
.container {
display: flex;
}
That single line activates the flexbox formatting context. By default, children align in a horizontal row, stretch to match the container height, and do not wrap. Every property we discuss from here on modifies this default behavior.
Use our Box Shadow Generator to style your flex containers with custom shadows that give depth and hierarchy to your layouts.
Flex Direction
The flex-direction property determines the main axis along which items are placed:
.container {
flex-direction: row; /* default - left to right */
flex-direction: row-reverse; /* right to left */
flex-direction: column; /* top to bottom */
flex-direction: column-reverse; /* bottom to top */
}
Switching to column is common for mobile layouts, while row-reverse helps with directional content like right-aligned navigation menus. The main axis direction affects every other alignment property, so choose this first when planning your layout.
Flex Wrap
By default, flex items try to fit on one line. flex-wrap changes that behavior:
.container {
flex-wrap: nowrap; /* default - all items on one line */
flex-wrap: wrap; /* items wrap to new lines */
flex-wrap: wrap-reverse; /* items wrap in reverse direction */
}
flex-wrap: wrap is essential for responsive grids and card layouts. Without it, items would shrink to fit a single line, often becoming too narrow to be useful.
Justify Content
This property controls alignment along the main axis. It distributes space between or around items:
.container {
justify-content: flex-start; /* default - items at start */
justify-content: flex-end; /* items at end */
justify-content: center; /* items centered */
justify-content: space-between; /* equal space between items */
justify-content: space-around; /* equal space around items */
justify-content: space-evenly; /* equal space everywhere */
}
justify-content: center is the simplest way to horizontally center items. space-between works perfectly for navigation links spread across a header, and space-evenly creates balanced photo galleries.
Align Items
While justify-content works on the main axis, align-items controls alignment on the cross axis (perpendicular to the main axis):
.container {
align-items: stretch; /* default - items stretch full height */
align-items: flex-start; /* items align to top */
align-items: flex-end; /* items align to bottom */
align-items: center; /* items vertically centered */
align-items: baseline; /* items align by text baseline */
}
align-items: center combined with justify-content: center gives you perfect centering both horizontally and vertically, one of the most common layout needs in web design. Before Flexbox, this required complicated CSS tricks.
Align Content
When flex items wrap onto multiple lines, align-content distributes space between those lines:
.container {
align-content: stretch; /* default */
align-content: flex-start; /* lines at start */
align-content: flex-end; /* lines at end */
align-content: center; /* lines centered */
align-content: space-between; /* equal space between lines */
align-content: space-around; /* equal space around lines */
}
This property only works when there are multiple lines of items (when flex-wrap: wrap is active). It is the cross-axis equivalent of justify-content and behaves the same way.
Understanding Flex Items
Once your container is configured, individual flex items can override default behavior using their own properties.
Flex Grow
flex-grow controls how much an item should grow relative to other items when there is extra space:
.item {
flex-grow: 0; /* default - no growing */
flex-grow: 1; /* item takes up available space */
flex-grow: 2; /* item takes up twice as much space */
}
If one item has flex-grow: 1 and another has flex-grow: 2, the second item receives twice the extra space. This is incredibly useful for creating sidebars that expand alongside main content areas.
Flex Shrink
flex-shrink determines how much an item shrinks when there is not enough space:
.item {
flex-shrink: 1; /* default - can shrink */
flex-shrink: 0; /* item will not shrink */
}
Prevent an element from shrinking by setting flex-shrink: 0. This is common for logos, icons, or any element that must preserve its size.
Flex Basis
flex-basis sets the initial size of an item before space distribution happens:
.item {
flex-basis: auto; /* default - based on content width */
flex-basis: 200px; /* initial size of 200 pixels */
flex-basis: 50%; /* initial size of 50 percent */
}
Think of flex-basis as the starting point. After this size is assigned, flex-grow and flex-shrink adjust from there. In a row layout, flex-basis acts like width. In a column layout, it acts like height.
The Flex Shorthand
You can combine flex-grow, flex-shrink, and flex-basis into one property:
.item {
flex: 1; /* flex: 1 1 0%; */
flex: 1 1 auto; /* grow, shrink, basis */
flex: 0 0 200px; /* fixed 200px item */
}
flex: 1 is a common pattern for equal-width columns. flex: 0 0 200px creates a fixed-size element that does not grow or shrink. The shorthand saves keystrokes and reduces the chance of conflicting values.
Align Self
Each flex item can override the container's align-items using align-self:
.item {
align-self: auto; /* default - follows container */
align-self: flex-start;
align-self: flex-end;
align-self: center;
align-self: stretch;
align-self: baseline;
}
This is useful when one item needs different vertical alignment than its siblings. For example, a call-to-action button might sit at the bottom of a card while other content stays at the top.
Order
The order property lets you rearrange items without changing the HTML:
.item {
order: 0; /* default */
order: 1; /* appears after items with order 0 */
order: -1; /* appears before items with order 0 */
}
Items are sorted by ascending order value. This is invaluable for responsive designs where the visual order needs to change at different breakpoints without duplicating markup.
Practical Flexbox Layout Examples
Theory is important, but real-world examples show the true power of Flexbox. Here are five common patterns you can build immediately.
1. Responsive Navigation Bar
A horizontal navigation bar that turns into a vertical menu on mobile is one of the most common Flexbox use cases:
.nav {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
}
On wide screens, links spread horizontally. On narrow screens, they wrap into a column. Adding gap: 10px creates breathing room between items. This approach eliminates the need for complex float-based navigation and works across all devices.
Choose complementary colors for your navigation elements using our Color Picker. A well-chosen palette improves usability and brand recognition.
2. Card Grid Layout
Flexbox makes card grids straightforward:
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.card {
flex: 1 1 300px;
}
Each card starts at 300 pixels wide but grows and shrinks as needed. At 300 pixels, items wrap to the next line, creating a responsive grid without media queries. This is the same technique used by major CSS frameworks.
Add rounded corners to your cards with our Border Radius Generator. Fine-tune the corner radius until your cards look polished and professional.
3. Perfect Centering
Centering content both horizontally and vertically is trivially simple with Flexbox:
.centered {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
This pattern works for hero sections, modal overlays, loading spinners, and full-screen landing pages. The same result previously required table display hacks or absolute positioning with negative margins.
4. Holy Grail Layout
The Holy Grail layout (header, footer, three columns with a flexible middle) is a classic challenge that Flexbox solves cleanly:
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
main {
display: flex;
flex: 1;
}
nav, aside {
flex: 0 0 200px;
}
article {
flex: 1;
}
The body becomes a column flex container. The main section fills remaining space and is itself a row flex container. Sidebars stay fixed at 200 pixels while the article expands to fill everything else. Footer stays at the bottom even with minimal content.
5. Sticky Footer
A footer that sticks to the bottom of the page when content is short is a Flexbox specialty:
.page {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.content {
flex: 1;
}
By giving the content area flex: 1, it expands to push the footer down when the viewport is taller than the content. No JavaScript required.
Flexbox vs CSS Grid
Developers often ask whether to use Flexbox or CSS Grid. The answer depends on what you are building:
-
Flexbox excels at one-dimensional layouts. Use it for navigation bars, toolbars, form controls, centering, and content where items wrap naturally.
-
CSS Grid excels at two-dimensional layouts. Use it for full-page layouts, magazine-style designs, and situations where you need precise control over both rows and columns simultaneously.
Many layouts combine both. A page might use Grid for its overall structure and Flexbox for component-level alignment within each grid cell. Understanding both tools gives you complete layout flexibility.
Experiment with both approaches using our CSS Grid Generator to see how grid-based layouts compare with flexbox in different scenarios.
Responsive Design with Flexbox
Flexbox is inherently responsive. The combination of flex-wrap: wrap, percentage-based flex-basis, and the flex shorthand creates layouts that adapt to any screen without media queries:
.flexible-gallery {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
.flexible-gallery .item {
flex: 1 1 250px;
}
At 1200 pixels wide, you see four items per row. At 800 pixels, three items. At 500 pixels, two items. Below 250 pixels, single column. All without a single breakpoint.
When you do need media queries, Flexbox makes them simpler. Changing flex-direction from row to column or adjusting flex-basis values at breakpoints is cleaner than rewriting float-based layouts.
Use our Gradient Generator to create visually engaging backgrounds for your responsive flexbox layouts that adapt alongside your content.
Common Flexbox Mistakes
Even experienced developers run into these Flexbox pitfalls:
Forgetting Flex Wrap
The default flex-wrap: nowrap causes items to shrink beyond usable sizes. If your items look crushed, add flex-wrap: wrap to allow them to break into new lines.
Confusing Axes
Remember that justify-content works on the main axis (defined by flex-direction), while align-items works on the cross axis. If you change flex-direction to column, these properties swap their visual behavior.
Ignoring Gap Support
The gap property works in Flexbox for modern browsers. Instead of using margins on flex items, use gap: 10px on the container. It is cleaner and does not create double-margin issues at row edges.
Overusing Flex Grow
Giving every item flex-grow: 1 is tempting but produces cramped layouts. Use fixed flex-basis values with flex-grow: 0 for items that need consistent sizing.
Not Using Flex Shorthand
Writing flex-grow, flex-shrink, and flex-basis separately is verbose and error-prone. The flex shorthand is shorter and prevents conflicting values.
Enhance your flex items with glassmorphism effects using our Glassmorphism Generator. The frosted glass aesthetic pairs beautifully with flexbox layouts for modern UI designs.
Advanced Flexbox Techniques
Nested Flex Containers
A flex item can itself be a flex container. Nesting flexboxes lets you create complex layouts while keeping each level simple:
.card {
display: flex;
flex-direction: column;
}
.card-body {
flex: 1;
display: flex;
align-items: center;
}
The outer container arranges cards in a row. The inner container handles content alignment within each card. Each flexbox does one job well.
Auto Margins for Spacing
Setting margin-left: auto on a flex item pushes it to the far right. This is perfect for separating navigation sections:
.nav-links {
display: flex;
}
.nav-links .login {
margin-left: auto;
}
The login link moves to the right edge while all other links stay on the left. No need for extra wrapper divs or complex justification.
Using Min-Width for Breakpoints
Instead of media queries, set min-width on flex items to control when they wrap:
.item {
flex: 1 1 0%;
min-width: 200px;
}
Items stay on one line until there is less than 200 pixels per item, at which point they wrap. This creates truly content-driven breakpoints.
Experiment with clipping decorative shapes on your flex items using our CSS Clip-Path Generator. Clip paths add visual interest to cards, images, and section dividers.
Browser Support and Performance
Flexbox is supported in all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. Internet Explorer 11 has partial support (it recognizes display: flex but has bugs with some features like gap). For production sites, test your layouts in IE11 and apply fallbacks if needed.
Flexbox performance is excellent. The browser engine calculates layouts efficiently, and even complex nested flexbox structures render quickly. Unlike JavaScript-based layout solutions, Flexbox uses native CSS rendering, meaning no layout shift after paint.
Bring your flexbox layouts to life with our CSS Animation Generator. Combine flexbox structure with animations to create engaging, interactive user interfaces that capture attention.
Conclusion
CSS Flexbox has fundamentally changed how developers approach web layout. Its intuitive properties, excellent browser support, and responsive nature make it an essential tool for every web developer. By understanding flex containers, flex items, and the relationship between main and cross axes, you can build almost any layout with clean, maintainable CSS.
The key to mastering Flexbox is practice. Start with simple layouts like navigation bars and centered content, then progress to card grids and complex page structures. Use visual tools to accelerate your learning and experiment with property combinations you have not tried before.
Our Flexbox Generator is the perfect companion for hands-on learning. Generate production-ready CSS code visually, then fine-tune it as you deepen your understanding. For related layout techniques, explore our CSS tools collection including the CSS Grid Generator and Box Shadow Generator to complete your design toolkit.
For further reading, the MDN Web Docs on Flexbox provide authoritative reference material, and CSS-Tricks Complete Guide to Flexbox remains one of the best community resources available. Bookmark these references and revisit them as you build more complex layouts.