Tool Blog CSS Flexbox Guide
← Back to Tool
📐

CSS Flexbox Complete Guide 2025 — Learn Every Property With Real Examples

Flexbox changed web development forever. Before it existed, centering a div vertically was genuinely painful. Now it takes one line. Whether you're building navigation bars, card grids, hero sections, or complex page layouts — Flexbox handles it cleanly. This guide covers every single Flexbox property, with working code examples and real layouts you can copy and use today.

✍️ By UTS Sites 📅 Updated: 2025 ⏱️ 15 min read 🎯 Beginner to Advanced

Copy any CSS and HTML example from this guide and paste it into our free HTML Editor Online Pro to see Flexbox working live. Change values and see results instantly — the best way to actually learn this stuff.

🤔

What is CSS Flexbox and Why Does It Matter?

CSS Flexbox — officially called the Flexible Box Layout Module — is a CSS layout system that makes it incredibly easy to arrange elements in a row or column, control their spacing, alignment, and size, and make them respond to different screen sizes. It was designed specifically to solve the layout problems that plagued web developers for years.

Before Flexbox, creating common layouts required hacks — using float, negative margins, inline-block tricks, and JavaScript measurements. These approaches were fragile, hard to maintain, and broke easily on different screen sizes. Flexbox replaced all of that with clean, predictable, purpose-built CSS.

Today, Flexbox is supported in 100% of modern browsers and is used in virtually every professional website. It works alongside CSS Grid — Flexbox is best for one-dimensional layouts (rows OR columns), while Grid handles two-dimensional layouts (rows AND columns at the same time). You'll use both, but Flexbox is what you'll reach for more often in everyday coding.

🧠

The mental model that makes Flexbox click: There is a container (the parent element with display: flex) and items (the direct children). Properties applied to the container control how items are arranged. Properties applied to the items control how individual items behave within that arrangement. Keep this distinction clear and Flexbox becomes logical.

🚀

Activating Flexbox — display: flex

You activate Flexbox with a single CSS property on the parent container. Just one line turns any element into a flex container, and all its direct children automatically become flex items.

The One Line That Starts Everything /* HTML */ <div class="container"> <div class="item">Item 1</div> <div class="item">Item 2</div> <div class="item">Item 3</div> </div> /* CSS */ .container { display: flex; /* This is it. That's all you need to start. */ }

The moment you add display: flex, the child items line up in a horizontal row — left to right — by default. That's the starting point. Everything else in Flexbox is about controlling how those items are arranged, sized, and aligned from there.

Before display:flex — items stack vertically (block elements)
Item 1
Item 2
Item 3
After display:flex — items line up in a row automatically
Item 1
Item 2
Item 3

That horizontal row arrangement — without writing any other CSS — is the default Flexbox behavior. Now let's learn how to control it.

↔️

flex-direction — Control the Main Axis

The main axis is the primary direction flex items are laid out. By default it's horizontal (left to right). flex-direction lets you change this.

flex-direction — All 4 Values .container { display: flex; flex-direction: row; /* Default: left → right */ flex-direction: row-reverse; /* Right → left */ flex-direction: column; /* Top → bottom (stacks vertically) */ flex-direction: column-reverse; /* Bottom → top */ }
flex-direction: column — items stack vertically
Item 1
Item 2
Item 3

flex-direction: column is extremely useful for mobile layouts. A common pattern: use row on desktop and switch to column on mobile with a media query. This is how most responsive navigation bars and card grids work.

⚖️

justify-content — Align Items on the Main Axis

justify-content controls how flex items are distributed along the main axis (horizontal by default). This is where the magic of Flexbox spacing happens. Six values cover every layout scenario you'll ever encounter.

justify-content — All Values .container { display: flex; justify-content: flex-start; /* Default: pack to the left */ justify-content: flex-end; /* Pack to the right */ justify-content: center; /* Center horizontally */ justify-content: space-between; /* First and last touch edges, equal space between */ justify-content: space-around; /* Equal space around each item */ justify-content: space-evenly; /* Perfectly equal space everywhere */ }
justify-content: center
Item 1
Item 2
Item 3
justify-content: space-between
Item 1
Item 2
Item 3
justify-content: space-evenly
Item 1
Item 2
Item 3

space-between is one of the most used values in real projects — it's perfect for navigation bars where the logo is on the left and links are on the right, or for putting items at opposite ends of a container.

↕️

align-items — Align Items on the Cross Axis

align-items controls alignment on the cross axis — perpendicular to the main axis. When flex-direction is row (horizontal), the cross axis is vertical. So align-items controls vertical alignment.

This is the property that finally solves the "how do I center this vertically" problem that drove developers crazy for years.

align-items — All Values .container { display: flex; height: 120px; /* Container needs a height for vertical alignment to work */ align-items: stretch; /* Default: items stretch to full height */ align-items: flex-start; /* Items align to the top */ align-items: flex-end; /* Items align to the bottom */ align-items: center; /* Items align to the vertical middle */ align-items: baseline; /* Items align by their text baseline */ }
align-items: center — items are vertically centered
Short
Taller Item
Short

The Perfect Center — Both Horizontally and Vertically

This is probably the most googled CSS layout question of all time. With Flexbox, it's two lines:

CSS — Perfect Center in 2 Lines .container { display: flex; justify-content: center; /* Center horizontally */ align-items: center; /* Center vertically */ height: 200px; /* Container needs a height */ }
Perfect center — justify-content: center + align-items: center
🎯 Perfectly Centered
🎯

This two-property combination — justify-content: center + align-items: center — is used in virtually every modern website. Buttons, hero text, modal dialogs, icon circles, loading spinners — all centered with this pattern.

🔀

flex-wrap — Let Items Wrap to Next Lines

By default, flex items all try to fit onto one line — even if it means shrinking them. flex-wrap changes this behavior, allowing items to wrap onto multiple lines when they run out of space.

flex-wrap — Values .container { display: flex; flex-wrap: nowrap; /* Default: all items on one line, may shrink */ flex-wrap: wrap; /* Items wrap to next line when needed */ flex-wrap: wrap-reverse; /* Items wrap upward instead of downward */ } /* Shorthand: flex-flow combines flex-direction and flex-wrap */ .container { flex-flow: row wrap; /* Same as: flex-direction: row; flex-wrap: wrap; */ }
flex-wrap: wrap — items wrap to next line on small screens
Card 1
Card 2
Card 3
Card 4
Card 5

flex-wrap: wrap combined with flex: 1 1 250px on the items is one of the most powerful responsive layout patterns in CSS — it creates a grid that automatically adapts to any screen size without any media queries. Cards fill the row, and when there isn't enough space, they wrap to the next line.

📏

flex-grow, flex-shrink, flex-basis — Controlling Item Size

These three properties are applied to flex items (the children), not the container. Together they define how a flex item should grow, shrink, and what its base size should be. They're often written together using the flex shorthand.

flex-grow — How Much an Item Can Grow

flex-grow defines how much extra space an item takes when there is leftover space in the container. A value of 0 means "don't grow." A value of 1 means "take equal share of available space." A value of 2 means "take twice as much space as items with flex-grow: 1."

flex-grow Examples /* All items grow equally — each takes 1/3 of the space */ .item { flex-grow: 1; } /* One item takes twice as much space */ .item-normal { flex-grow: 1; } .item-wide { flex-grow: 2; } /* Gets 2x the space */ /* Sidebar + main content layout */ .sidebar { flex-grow: 0; width: 240px; } /* Fixed width sidebar */ .main { flex-grow: 1; } /* Main content takes all remaining space */
flex-grow: 1 on all items — they share space equally
flex-grow: 1
flex-grow: 1
flex-grow: 2

flex-shrink — How Much an Item Can Shrink

flex-shrink controls how much an item shrinks when the container runs out of space. The default is 1 — all items shrink equally. Setting it to 0 prevents an item from shrinking at all, keeping its original size.

flex-shrink Examples .logo { flex-shrink: 0; } /* Logo never shrinks — keeps its size */ .nav-links { flex-shrink: 1; } /* Nav links can shrink if needed */

flex-basis — The Starting Size

flex-basis sets the initial size of a flex item before any growing or shrinking happens. Think of it as the "ideal size" the item starts at. It can be a pixel value, a percentage, or auto (uses the item's content width).

The Powerful flex Shorthand /* flex: grow shrink basis */ .item { flex: 1 1 auto; } /* grow=1, shrink=1, basis=auto */ .item { flex: 1; } /* Shorthand for: flex: 1 1 0 */ .item { flex: 0 0 200px; } /* Fixed 200px, won't grow or shrink */ /* Responsive card grid — no media queries needed */ .card { flex: 1 1 250px; } /* Each card is at least 250px, grows to fill */
💡

flex: 1 1 250px is a game-changer for responsive layouts. Applied to cards inside a flex container with flex-wrap: wrap, cards automatically fill the row, wrap to the next line when the screen is too small, and you never need to write a single media query for the card grid. This pattern is used constantly in real projects.

🎯

align-self — Override Alignment for One Item

align-self is applied to individual flex items and overrides the container's align-items value for just that one item. It accepts the same values as align-items: auto, flex-start, flex-end, center, stretch, baseline.

align-self — Override for One Item .container { display: flex; align-items: flex-start; /* All items align to top by default */ height: 120px; } .special-item { align-self: flex-end; /* This one item goes to the bottom */ } .centered-item { align-self: center; /* This one is vertically centered */ }
align-self overrides — each item aligns differently
Top
Center
Bottom
🔢

order — Change the Visual Order

The order property lets you change the visual order of flex items without changing the HTML. By default, all items have order: 0 and appear in the order they appear in the HTML. Lower order values appear first, higher values appear last.

order — Rearranging Items Visually /* HTML order: Item A, Item B, Item C */ /* Visual order after CSS: Item C, Item A, Item B */ .item-a { order: 2; } /* Appears second */ .item-b { order: 3; } /* Appears last */ .item-c { order: 1; } /* Appears first */ /* Practical use: Show content before sidebar on mobile */ @media (max-width: 768px) { .sidebar { order: 2; } /* Sidebar goes below content on mobile */ .content { order: 1; } /* Content shows first */ }
🏗️

Real-World Flexbox Layouts — Copy and Use

Theory only goes so far. Here are the most common real-world layouts built with Flexbox — complete, copy-ready code for each one.

Layout 1 — Responsive Navigation Bar

The navigation bar is the most common Flexbox use case. Logo on the left, links on the right, perfectly vertically centered.

Responsive Navigation Bar /* HTML */ <header class="navbar"> <a href="/" class="logo">UTS Sites</a> <nav class="nav-links"> <a href="#">Home</a> <a href="#">About</a> <a href="#">Contact</a> </nav> </header> /* CSS */ .navbar { display: flex; justify-content: space-between; align-items: center; padding: 16px 32px; background: white; box-shadow: 0 2px 10px rgba(0,0,0,.08); } .nav-links { display: flex; gap: 24px; align-items: center; } @media (max-width: 600px) { .navbar { flex-direction: column; gap: 12px; } }
Live Preview — Navigation Bar

Layout 2 — Card Grid (Responsive, No Media Queries)

Responsive Card Grid — No Media Queries Needed .card-grid { display: flex; flex-wrap: wrap; gap: 20px; } .card { flex: 1 1 260px; /* Min 260px, grows to fill row */ background: white; border: 1px solid #e5e7eb; border-radius: 12px; padding: 24px; box-shadow: 0 2px 8px rgba(0,0,0,.06); }
Live Preview — Card Grid
🚀
Fast

Optimized for speed

🔒
Secure

Enterprise security

📱
Mobile

Works on all devices

Layout 3 — Sidebar + Main Content

Sidebar + Main Content Layout .page-layout { display: flex; gap: 24px; align-items: flex-start; } .sidebar { flex: 0 0 260px; /* Fixed 260px sidebar, won't shrink */ position: sticky; top: 20px; } .main-content { flex: 1; /* Takes all remaining space */ min-width: 0; /* Prevents content overflow bug */ } @media (max-width: 768px) { .page-layout { flex-direction: column; } .sidebar { flex: none; width: 100%; } }

Layout 4 — Sticky Footer

A footer that always stays at the bottom of the screen — even when page content is short — is one of the hardest things to achieve without Flexbox. With it, it's simple:

Sticky Footer That Always Stays at the Bottom body { display: flex; flex-direction: column; min-height: 100vh; /* At least full screen height */ } main { flex: 1; /* Main content grows to push footer down */ } footer { /* Footer always stays at the bottom */ }

Test Every Flexbox Layout Instantly — For Free

Copy any code from this guide into HTML Editor Online Pro and see it working live. Change values, experiment, and learn by seeing — the fastest way to master Flexbox.

📐 Open HTML Editor Free →
📋

Complete Flexbox Properties Reference

Quick reference for every Flexbox property — container properties and item properties in one place.

Container Properties (Applied to the Parent)

PropertyValuesWhat it does
displayflex | inline-flexActivates Flexbox. inline-flex makes the container itself inline.
flex-directionrow | row-reverse | column | column-reverseSets the main axis direction
flex-wrapnowrap | wrap | wrap-reverseControls whether items wrap to multiple lines
flex-flowShorthand for direction + wrape.g. flex-flow: row wrap
justify-contentflex-start | flex-end | center | space-between | space-around | space-evenlyAligns items on the main axis
align-itemsstretch | flex-start | flex-end | center | baselineAligns items on the cross axis
align-contentSame as align-itemsAligns multiple lines of items (only works when flex-wrap: wrap)
gapAny CSS length valueSpacing between items. Can set row-gap and column-gap separately.
scroll

Item Properties (Applied to the Children)

PropertyValuesWhat it does
flex-growNumber (default: 0)How much the item grows relative to others
flex-shrinkNumber (default: 1)How much the item shrinks relative to others
flex-basisLength | autoThe starting size before grow/shrink
flexShorthand: grow shrink basise.g. flex: 1 1 250px or just flex: 1
align-selfSame as align-items valuesOverrides align-items for this specific item
orderInteger (default: 0)Controls visual order — lower numbers appear first
scroll

Frequently Asked Questions About CSS Flexbox

Q: When should I use Flexbox vs CSS Grid?
Use Flexbox when you're working in one dimension — either a row or a column, but not both at the same time. Navigation bars, button groups, centering an item, card rows — these are Flexbox situations. Use CSS Grid when you need two-dimensional control — defining both rows and columns simultaneously, like a full page layout or a photo gallery. In practice, most websites use both: Grid for the overall page layout, Flexbox for components within that layout.

Q: Why aren't my flex items centered vertically?
The most common reason: the flex container doesn't have a defined height. align-items: center centers items within the container's height — but if the container is only as tall as its content, there's no extra vertical space to center within. Add a height or min-height to the container: height: 200px or min-height: 100vh for a full-screen section.

Q: What does min-width: 0 on a flex item do?
This is one of Flexbox's quirks. Flex items have a default minimum width based on their content — they won't shrink below the width of their longest word. This causes overflow in many layouts. Setting min-width: 0 removes this restriction, allowing the item to shrink fully. You'll see this especially in sidebar layouts where the main content overflows.

Q: What is the difference between gap and margin in Flexbox?
gap (also called grid-gap) adds spacing only between items — not on the outer edges. This is perfect for card grids where you want spacing between cards but not on the sides. margin adds spacing to all sides including the outside edges. For most Flexbox layouts, gap is cleaner and easier to control than using margins on individual items.

Q: Does Flexbox work in all browsers?
Yes — CSS Flexbox has 100% support in all modern browsers as of 2025: Chrome, Firefox, Safari, Edge, Opera, and all their mobile versions. You do not need any vendor prefixes or fallbacks for current browser versions. The only situation where you might need to worry is if you need to support Internet Explorer 11, which has partial and buggy Flexbox support — but IE11 has been officially discontinued and has a global usage under 0.5%.