Creating responsive layouts with CSS Flexbox allows developers to build websites that automatically adjust to different screen sizes. Instead of assigning fixed widths and manually positioning every element, Flexbox distributes available space between elements, allows them to grow or shrink, and moves items onto new lines when necessary.
Flexbox is particularly useful for navigation menus, card layouts, website headers, sidebars, content sections, and responsive forms. By combining properties such as display: flex, flex-wrap, flex-direction, flex-grow, flex-basis, and justify-content, developers can create flexible layouts with relatively little CSS.
This guide explains how Flexbox works and how to use it to build responsive layouts for mobile phones, tablets, laptops, and desktop screens.
What Is CSS Flexbox?
CSS Flexbox, formally known as the Flexible Box Layout Module, is a CSS layout system designed to arrange elements along one dimension: either a row or a column.
Unlike traditional fixed-width layouts, Flexbox allows elements to adjust their sizes based on the available space inside their parent container.
For example, imagine a website containing three content cards. On a desktop screen, the cards can appear next to one another. As the screen becomes narrower, they can shrink or move onto additional rows. On a mobile screen, they can occupy the available width in a single-column arrangement.
Flexbox makes these layout changes easier without requiring developers to calculate the exact position of every element.
Two important terms help explain how Flexbox works:
- Flex container: The parent element that has display: flex or display: inline-flex.
- Flex items: The direct children of the flex container that participate in its layout.
Flexbox also uses two axes:
- Main axis: The primary direction in which flex items are arranged.
- Cross axis: The direction perpendicular to the main axis.
When flex-direction: row is used, the main axis is horizontal and the cross axis is vertical in a typical horizontal writing mode. When flex-direction: column is used, these directions are reversed.
How to Create a Basic Flexbox Layout
The first step in creating a Flexbox layout is to define a parent element and place the elements you want to arrange inside it.
Consider a simple example containing three content sections.
HTML:
<div class=”flex-container”>
<div class=”flex-item”>Section One</div>
<div class=”flex-item”>Section Two</div>
<div class=”flex-item”>Section Three</div>
</div>
CSS:
.flex-container {
display: flex;
gap: 20px;
}
.flex-item {
background-color: #eef3fe;
padding: 30px;
border-radius: 8px;
flex: 1;
}
The display: flex declaration activates Flexbox on the parent element. Its direct children become flex items.
The gap property creates consistent spacing between items, while flex: 1 allows the items to share available space equally.
By default, Flexbox arranges items in a row and does not wrap them onto additional lines. This behavior is useful for basic horizontal layouts, but responsive designs often require additional flexibility.
Use flex-wrap to Make Layouts Responsive
The flex-wrap property is one of the most important properties for creating responsive Flexbox layouts.
By default, Flexbox uses:
flex-wrap: nowrap;
This keeps items on a single line. If the items cannot fit and cannot shrink sufficiently, they may overflow their container.
To allow items to move onto additional rows, use:
.flex-container {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
The available values are:
| Value | Behavior |
| nowrap | Keeps all flex items on one line. |
| wrap | Moves items onto additional lines when necessary. |
| wrap-reverse | Wraps items in the reverse cross-axis direction. |
For most responsive card layouts, flex-wrap: wrap is a practical choice.
Example: Responsive Card Layout
Suppose you are creating a website with four service cards. You want them displayed horizontally on larger screens but automatically rearranged when the available width decreases.
HTML:
<div class=”card-container”>
<div class=”card”>
<h3>Web Design</h3>
<p>Create attractive and user-friendly websites.</p>
</div>
<div class=”card”>
<h3>SEO Services</h3>
<p>Improve website visibility in search engines.</p>
</div>
<div class=”card”>
<h3>Content Marketing</h3>
<p>Publish useful content for your audience.</p>
</div>
<div class=”card”>
<h3>Social Media</h3>
<p>Connect with customers through social platforms.</p>
</div>
</div>
CSS:
.card-container {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.card {
flex: 1 1 250px;
max-width: 400px;
min-width: 0;
padding: 20px;
background-color: #f4f6f9;
border-radius: 10px;
}
The important declaration is:
flex: 1 1 250px;
It combines three Flexbox properties:
- flex-grow: 1 allows a card to expand when additional space is available.
- flex-shrink: 1 allows a card to shrink when space becomes limited.
- flex-basis: 250px establishes its initial preferred size.
As the container becomes narrower, fewer cards fit on each line. The cards automatically wrap without requiring a separate media query for every screen size.
The max-width prevents a single card on the last row from expanding indefinitely, while min-width: 0 allows the card to shrink below its content-based minimum size when necessary.
Control Layout Direction with flex-direction
The flex-direction property determines the direction in which flex items are arranged.
It supports four values:
| Value | Description |
| row | Arranges items horizontally in the main-axis direction. |
| row-reverse | Arranges items horizontally in reverse order. |
| column | Arranges items vertically. |
| column-reverse | Arranges items vertically in reverse order. |
A simple horizontal layout uses:
.container {
display: flex;
flex-direction: row;
}
A vertical layout uses:
.container {
display: flex;
flex-direction: column;
}
For responsive websites, changing the direction based on available screen width can be useful.
Example: Switch from Horizontal to Vertical Layout
A website header may display navigation links horizontally on a desktop but require a vertical arrangement on a narrow screen.
.navigation {
display: flex;
flex-direction: column;
gap: 12px;
}
@media (min-width: 768px) {
.navigation {
flex-direction: row;
align-items: center;
}
}
The default layout is vertical. When the viewport reaches 768 CSS pixels, the navigation changes to a horizontal arrangement.
The exact breakpoint should depend on the amount of navigation content and the point at which the layout becomes crowded.
Use flex-grow, flex-shrink, and flex-basis to Control Item Sizes
Responsive Flexbox layouts depend heavily on how items distribute available space.
Three properties control this behavior.
flex-grow
The flex-grow property determines how much an item can expand when additional space is available.
.container {
display: flex;
gap: 15px;
}
.item-one {
flex-grow: 1;
}
.item-two {
flex-grow: 2;
}
.item-three {
flex-grow: 1;
}
When positive free space is distributed, the second item receives twice the growth share of either of the other two items.
flex-shrink
The flex-shrink property determines how much an item can shrink when the combined flex bases exceed the available space on a line.
.container {
display: flex;
}
.sidebar {
flex-shrink: 0;
width: 220px;
}
.content {
flex-shrink: 1;
}
Here, the sidebar is prevented from shrinking, while the content area is allowed to become narrower.
Use this property carefully. Preventing an element from shrinking can create overflow on small screens if the layout does not provide another way to accommodate it.
flex-basis
The flex-basis property establishes an item’s initial main-axis size before the remaining space is distributed.
.card {
flex-basis: 250px;
}
This gives the card an initial basis of 250 pixels when the main axis is horizontal.
The property can also use percentages and other valid CSS sizing values.
Understanding the flex shorthand
Developers commonly combine all three properties using the flex shorthand.
.card {
flex: 1 1 250px;
}
This is often more convenient than writing three separate declarations.
Choosing an appropriate flex basis is particularly important for responsive layouts because it influences when items can fit together and when they need to wrap.
Align and Distribute Items with Flexbox Properties
A responsive layout should not only fit different screen sizes; its elements should also maintain appropriate alignment and spacing.
Flexbox provides several alignment properties.
justify-content
The justify-content property distributes items along the main axis.
.container {
display: flex;
justify-content: space-between;
}
Common values include:
- flex-start: Positions items at the beginning of the main axis.
- center: Centers items along the main axis.
- flex-end: Positions items at the end.
- space-between: Distributes available space between items.
- space-around: Distributes space around items.
- space-evenly: Distributes equal space between items and the container edges.
align-items
The align-items property controls alignment along the cross axis.
.container {
display: flex;
align-items: center;
}
This is useful for vertically centering icons, text, navigation items, and buttons in a horizontal flex container.
Common values include stretch, center, flex-start, flex-end, and baseline.
align-content
When a flex container has multiple lines because of flex-wrap, align-content controls how those lines are distributed along the cross axis when additional space is available.
.container {
display: flex;
flex-wrap: wrap;
align-content: space-between;
}
Do not confuse align-content with align-items. The former concerns multiple flex lines, while the latter aligns individual items within their line.
Create Responsive Layouts Using Media Queries
Flexbox can automatically distribute and wrap items, but some layouts need structural changes at particular viewport widths.
Media queries allow developers to apply CSS rules when specified conditions are met.
For example, a two-column website layout may work well on desktop screens but become difficult to read on mobile devices.
HTML:
<div class=”page-layout”>
<aside class=”sidebar”>
<h3>Sidebar</h3>
<p>Related navigation and information.</p>
</aside>
<main class=”main-content”>
<h2>Main Content</h2>
<p>This section contains the primary page content.</p>
</main>
</div>
CSS:
.page-layout {
display: flex;
flex-direction: column;
gap: 24px;
max-width: 1200px;
margin-inline: auto;
padding: 20px;
}
.sidebar {
flex: 0 0 240px;
}
.main-content {
flex: 1 1 auto;
min-width: 0;
}
@media (min-width: 768px) {
.page-layout {
flex-direction: row;
align-items: flex-start;
}
}
On smaller screens, the sidebar and main content appear vertically. On screens at least 768 CSS pixels wide, the layout changes into two columns.
This mobile-first approach starts with the simpler narrow-screen layout and adds a horizontal arrangement when sufficient space is available.
The breakpoint should be adjusted according to the actual content rather than treated as a universal device-width requirement.
Build a Complete Responsive Flexbox Layout
The following example combines the main Flexbox techniques into one practical layout. It includes a navigation area, a content section, and responsive service cards.
HTML structure
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<title>Responsive Flexbox Layout</title>
<link rel=”stylesheet” href=”style.css”>
</head>
<body>
<header class=”header”>
<h1>My Website</h1>
<nav class=”nav”>
<a href=”#”>Home</a>
<a href=”#”>Services</a>
<a href=”#”>About</a>
<a href=”#”>Contact</a>
</nav>
</header>
<main class=”container”>
<section class=”intro”>
<h2>Our Services</h2>
<p>Explore our professional digital services.</p>
</section>
<section class=”service-list”>
<article class=”service-card”>
<h3>Web Design</h3>
<p>Build responsive and visually appealing websites.</p>
</article>
<article class=”service-card”>
<h3>SEO</h3>
<p>Improve search visibility and organic performance.</p>
</article>
<article class=”service-card”>
<h3>Development</h3>
<p>Create functional and accessible web experiences.</p>
</article>
</section>
</main>
</body>
</html>
CSS styling
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: Arial, sans-serif;
color: #20242c;
}
.header {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 16px;
padding: 20px;
background-color: #2451d6;
color: white;
}
.nav {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
.nav a {
color: white;
text-decoration: none;
}
.container {
width: min(100%, 1200px);
margin-inline: auto;
padding: 24px 20px;
}
.intro {
margin-bottom: 24px;
}
.service-list {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.service-card {
flex: 1 1 260px;
min-width: 0;
padding: 24px;
background-color: #eef3fe;
border-radius: 10px;
}
@media (max-width: 480px) {
.header {
align-items: flex-start;
}
.nav {
gap: 12px;
}
}
The header uses Flexbox to distribute the website title and navigation links. The navigation itself can wrap when the available width becomes limited.
The service section uses another flex container to arrange cards. Each card has a flexible starting size and can move onto a new row when necessary.
The result is a layout that responds to changing available space without requiring separate fixed widths for every device.
Common Flexbox Problems and How to Fix Them
Even when Flexbox is configured correctly, certain CSS properties and content conditions can cause unexpected layouts.
1. Flex items overflow the container
A common problem occurs when the contents of a flex item cannot shrink sufficiently.
Long URLs, large images, and unbroken text can cause overflow.
Use:
.flex-item {
min-width: 0;
overflow-wrap: anywhere;
}
.flex-item img {
max-width: 100%;
height: auto;
}
The min-width: 0 declaration allows the item to shrink below its automatic content-based minimum size. The text wrapping rule helps prevent long strings from extending beyond the card.
2. Items refuse to move onto a new line
Check whether the container has flex-wrap: nowrap.
Change it to:
.container {
display: flex;
flex-wrap: wrap;
}
Also check the flex basis and minimum sizes of the child elements. These affect whether items can fit on the same line.
3. Cards have inconsistent widths
If cards use different content-based initial sizes, they may not appear equally wide.
For equal starting sizes, use:
.card {
flex: 1 1 0;
}
This allows available space to be divided equally, subject to minimum and maximum sizing constraints.
For responsive cards that should wrap at a preferred minimum size, use a nonzero basis instead:
.card {
flex: 1 1 250px;
}
Choose the version according to the intended design.
4. Unexpected spacing appears between elements
Avoid combining large margins with gap and distributed alignment without checking the total spacing.
For most flex layouts, gap provides a straightforward way to create consistent spacing between items.
.container {
display: flex;
gap: 20px;
}
Use justify-content separately when you want to distribute additional free space.
5. Items appear in an unexpected visual order
Properties such as order, row-reverse, and column-reverse can change visual arrangement, but they do not change the underlying HTML source order.
Keep the DOM order logical for reading and keyboard navigation. Use visual reordering only when it does not create confusion for users.
Best Practices for Responsive Flexbox Layouts
A maintainable Flexbox layout should adapt naturally to available space without relying on unnecessary CSS overrides.
Consider these practices when building responsive interfaces:
- Start with flexible dimensions: Prefer relative sizing and flexible flex bases instead of fixed widths for every item.
- Use flex-wrap where appropriate: Allow cards and navigation elements to move onto additional rows.
- Adopt a mobile-first approach: Begin with a layout that works on narrow screens, then enhance it for wider viewports.
- Choose content-based breakpoints: Add media queries where the layout needs to change, not simply for specific device names.
- Use gap for spacing: It makes the relationship between flex items easier to manage.
- Prevent unwanted overflow: Check long text, images, and nested flex containers.
- Keep HTML source order meaningful: Do not rely on visual reordering to communicate essential content relationships.
- Test different viewport widths: Check intermediate sizes as well as common mobile and desktop dimensions.
- Use CSS Grid when necessary: Flexbox is designed for one-dimensional layout. Grid is generally more appropriate when strict alignment across both rows and columns is required.
When Should You Use Flexbox Instead of CSS Grid?
Flexbox and CSS Grid are both modern CSS layout systems, but they solve different layout problems.
Flexbox is designed primarily for arranging items in one dimension. CSS Grid is designed for controlling rows and columns together.
| Requirement | Flexbox | CSS Grid |
| Horizontal navigation | Well suited | Possible |
| Vertical alignment | Well suited | Possible |
| Responsive card wrapping | Well suited | Well suited |
| Sidebar and content arrangement | Well suited | Well suited |
| Precise two-dimensional alignment | Less suitable | Well suited |
| Equal column and row track sizing | Not its primary purpose | Well suited |
| Flexible content distribution | Well suited | Well suited |
Use Flexbox for navigation, toolbars, content groups, and components where the arrangement is mainly horizontal or vertical.
Use Grid when a page requires coordinated rows and columns, such as a dashboard, complex image gallery, or structured page layout.
Both technologies can be combined within the same website. For example, CSS Grid can control the main page structure while Flexbox handles the navigation and individual components.
Frequently Asked Questions
How do you make Flexbox responsive?
Use display: flex, flex-wrap: wrap, and flexible item sizing such as flex: 1 1 250px. Add media queries when the layout needs a more specific structural change.
What does flex-wrap do in responsive design?
The flex-wrap property allows flex items to move onto additional rows or columns when they no longer fit on the same line. It helps prevent horizontal overflow in many responsive layouts.
Is Flexbox better than CSS Grid for responsive layouts?
Neither is universally better. Flexbox is useful for one-dimensional arrangements, while Grid is better suited to layouts requiring coordinated rows and columns. Both support responsive design.
Can Flexbox create a mobile-friendly navigation menu?
Yes. Flexbox can arrange navigation links horizontally on larger screens and vertically or across multiple lines on smaller screens using flex-direction, flex-wrap, and media queries.
What is the difference between flex-grow and flex-shrink?
flex-grow controls how items expand when extra space is available, while flex-shrink controls how they respond when available space is insufficient.
Do responsive Flexbox layouts require media queries?
No. Flexible sizing and wrapping can make many layouts responsive without media queries. However, media queries are useful when the overall arrangement needs to change, such as switching from a vertical mobile layout to a horizontal desktop layout.
Conclusion
Creating responsive layouts with Flexbox is an effective way to build interfaces that adapt to different screen sizes. By understanding the flex container, main and cross axes, wrapping, flexible sizing, and alignment properties, developers can create layouts without depending on rigid dimensions or complicated positioning rules.
Start with a mobile-friendly structure, use flex-wrap and appropriate flex sizing to distribute content, and introduce media queries when the layout requires a meaningful structural change.
For complex interfaces, combine Flexbox with CSS Grid rather than forcing one layout method to handle every requirement. Test the finished design at different viewport widths to ensure that content remains readable, navigation remains usable, and elements fit naturally across devices.
Must Read This Ones
Using CSS Formatter for Large Stylesheets
How to Organize CSS for Better Maintainability
Common CSS Mistakes That Break Layouts
How to Create Responsive Layouts with Flexbox
CSS Grid Playground: Practical Examples
How to Optimize CSS for Performance
Difference Between Inline, Internal and External CSS
How to Debug CSS Layout Issues Quickly