Skip to content
زمین‌بازی CSS Flexbox
Tools

زمین‌بازی CSS Flexbox

جدید

ویژگی‌های flex را با پیش‌نمایش زنده تنظیم کنید و CSS را کپی کنید.

Preview width
4 items
main axis · justify-content cross axis · align-items

Container properties

Selected item

Click an item in the preview to edit its flex-grow, flex-shrink, flex-basis, order and align-self — or turn it into a nested flex container.

Runs entirely in your browser. Nothing is uploaded.

A visual flexbox generator that writes the CSS for you

This flexbox generator turns CSS Flexible Box Layout into something you can see. Instead of guessing what justify-content or align-items will do, you toggle each property on a labelled control and watch a live preview rearrange instantly — then copy clean, production-ready code. It runs entirely in your browser, with no sign-up and nothing uploaded.

It's a true flexbox playground: a real flex container with editable child items, full container and per-item controls, nested containers, a resizable responsive preview, and a shareable URL. Whether you're learning flexbox or quickly building a navbar, card row, or centered hero, you can get the exact layout — and the CSS, Tailwind or Bootstrap to match — in seconds.

Every container property, with a live preview

Control the container the way the spec does. Set flex-direction (row, row-reverse, column, column-reverse) to choose the main axis, justify-content to space items along it, and align-items to align them on the cross axis. Turn on flex-wrap to let items flow onto new lines, use align-content to space those wrapped lines, and dial in independent row and column gap values.

A built-in axis legend shows which direction is the main axis and which is the cross axis as you change flex-direction, so the relationship between justify-content and align-items finally clicks. It's a hands-on flexbox cheat sheet you learn by doing.

Per-item control: grow, shrink, basis, order and align-self

Most generators stop at the container. This one lets you click any item in the preview and edit it individually: set flex-grow, flex-shrink and flex-basis (the flex shorthand), override its align-self, and change its order to reposition it without touching the HTML. Add or remove items to test how space redistributes.

That makes it straightforward to build real patterns — a fixed sidebar beside a flexible main column (flex: 0 0 240px next to flex: 1), a single centered item, or a footer link pushed to the far end with margin-left: auto.

Nested flexbox and a responsive preview

Real layouts nest. Any item can become its own nested flex container with its own direction, justify and align settings — exactly what a nested flexbox generator needs for cards that hold a header row above a body, or a toolbar inside a toolbar. The generated code includes the nested rules too.

Because flexbox is about responsive behaviour, the preview is resizable: drag the handle or pick a Mobile / Tablet / Desktop width preset and watch items wrap and realign at each size. It's the fastest way to confirm a responsive flexbox layout behaves before you ship it.

Copy as CSS, Tailwind or Bootstrap — and how this compares to other tools

Switch the output tab to get the format your project uses. CSS gives you clean .container and per-item rules using the flex shorthand. Tailwind writes the utility classes — flex, flex-col, justify-between, items-center, gap-4, flex-1, order-2 — on a ready-to-paste HTML snippet. Bootstrap outputs the equivalent d-flex utility classes. Only the properties you actually changed are emitted, so the output stays minimal and readable instead of repeating every default.

Other visual generators like Flexbox.help and Loading.io's flexbox generator give you the container properties but skip per-item controls and nested containers. Tailwind's online playground doesn't generate vanilla CSS. This tool does all three output formats, handles nesting, and runs offline once loaded — no account, no cost, no data leaving your browser.

The two-axis mental model: main axis, cross axis, and why justify vs align confuses everyone

Flexbox operates on two perpendicular axes. The main axis is the direction flex items are placed — set by flex-direction. With flex-direction: row (the default), the main axis runs left to right. With flex-direction: column, it runs top to bottom. The cross axis is always perpendicular to it: vertical when the main axis is horizontal, horizontal when the main axis is vertical.

The reason justify-content and align-items confuse developers is that their names seem arbitrary until you pin them to the axes. justify-content always controls the main axis — it distributes extra space along the direction items flow. align-items always controls the cross axis — it determines how items are positioned on the perpendicular dimension. Flip flex-direction to column and the two properties visually swap roles, because the axes have swapped, but the rule is unchanged: justify = main, align = cross.

There is also align-content, which only activates when flex-wrap: wrap produces multiple lines. It controls how those lines are spaced on the cross axis, while align-items handles individual item alignment within each line. Keeping the axis model in mind — draw a horizontal arrow and a vertical arrow, label one 'main' and one 'cross' based on flex-direction, then assign justify and align accordingly — eliminates nearly every flexbox property-confusion bug.

The flex shorthand decoded: flex: 1, flex: auto, and flex: none are not the same thing

The flex shorthand packs three values: flex-grow, flex-shrink, and flex-basis. flex-basis is the item's hypothetical size before free space is distributed — think of it as a starting point. flex-grow is a ratio that governs how an item expands to fill leftover space; 0 means it never grows, 1 means it takes a proportional share. flex-shrink mirrors this for overflow: 1 allows the item to shrink, 0 locks it at its basis even if the container is too small.

flex: 1 expands to 1 1 0%. The key is that basis of 0%: all items start with zero intrinsic size, so free space is divided purely by their grow ratios — every flex: 1 item lands at exactly the same width regardless of its content. flex: auto expands to 1 1 auto. Here the basis is auto, meaning each item's content width is taken into account first, and only the remaining space is shared out. A short item and a long item with the same flex: auto will not be equal width. flex: none is 0 0 auto: the item neither grows nor shrinks, sitting at exactly its natural size no matter what.

In practice this difference matters constantly. Equal-width navigation tabs want flex: 1 (equal division, content ignored). A tag list that should overflow gracefully wants flex: none on each tag. A form with a fixed label and a stretchy input uses flex: 0 0 120px on the label and flex: 1 on the input. Understanding the three sub-properties individually — rather than memorising shorthand values — makes it straightforward to reason about any sizing problem.

Common Flexbox layout patterns worth knowing by heart

Perfect centering is the most-searched flexbox technique: set display: flex; justify-content: center; align-items: center; on the container, give it a height (such as min-height: 100vh for full-screen), and the child sits dead-center regardless of its size. It works for modals, hero sections, loading spinners, and empty-state illustrations. Before flexbox this required a combination of absolute positioning and negative margins; now it is two declarations.

A sticky footer needs the page to fill at least the viewport height and the main content to expand to push the footer down. Wrap the page in display: flex; flex-direction: column; min-height: 100vh; and give the main content area flex: 1 — it grows to fill all remaining vertical space so the footer always sits at the bottom even on short pages. Equal-height columns come for free: because flex items on the same row stretch to match the tallest sibling by default (align-items: stretch), a multi-column card layout has uniform column heights with no JavaScript required. A navbar with logo left and links right uses justify-content: space-between on the flex row, or simply puts margin-left: auto on the link group to push it to the far end — a powerful trick because auto margins in flexbox consume all available free space before the item is positioned.

Card grids with flex-wrap combine flex-wrap: wrap on the container with flex: 1 1 280px on each card — cards fill a row and wrap when they no longer fit at 280 px. This is a clean alternative to CSS Grid for content of unknown length, and it degrades well on narrow screens because cards wrap naturally rather than overflowing. Pairing with gap instead of margins avoids the classic 'last-row gap' problem that margin caused when rows ended mid-line.

Accessibility: when the order property breaks screen readers and keyboard navigation

Flexbox's order property and the flex-direction: row-reverse / column-reverse values change how items appear visually without touching the underlying HTML source order. The browser paints them in a different sequence, but assistive technologies and keyboard navigation follow DOM order, not visual order. A sighted mouse user sees Tab 3, Tab 1, Tab 2; a screen-reader user hears Tab 1, Tab 2, Tab 3; a keyboard user pressing Tab moves through the elements in DOM order, which may skip over what appears visually adjacent.

WCAG Success Criterion 1.3.2 (Meaningful Sequence) requires that when the sequence of content affects meaning, the correct reading order can be programmatically determined — which means it must be reflected in the DOM. Reordering items purely with CSS violates this when the order is meaningful (navigation, step-by-step flows, data tables). The MDN documentation for order explicitly warns: 'Do not use order for anything that might affect how it is read by a screen reader.' The safe approach is to reorder items in the HTML source whenever the order matters, and reserve the CSS order property only for purely decorative visual shifts where the content makes equal sense in either sequence.

Related considerations apply to flex-direction: row-reverse used for right-to-left visual layouts on LTR pages — the text inside items still reads left-to-right, creating a mismatch between visual and reading order. True RTL support should use the HTML dir attribute and logical CSS properties (margin-inline-start, padding-inline-end) rather than reversing flex direction. When auditing a flexbox layout for accessibility, check whether any reordering is meaningful, confirm keyboard tab stops move in a logical visual sequence, and verify that no important content is skipped or out of sequence for users who rely on sequential navigation.

Frequently asked questions

What is Flexbox in CSS and why is it used?

Flexbox (the CSS Flexible Box Layout) is a one-dimensional layout model for arranging items in a single row or column and distributing space between them. You turn it on with one line — display: flex — on a parent element, and its children become flex items you can align, space and resize without floats or manual margins. It's used because it makes common jobs easy: centering, equal-height columns, navbars, card rows and 'push this to the right' layouts that were painful before it. Over 99% of browsers support it as of 2024.

How do you use flexbox?

Add display: flex to the container element, then control the children with the container properties — flex-direction (row or column), justify-content (spacing along the main axis), align-items (alignment on the cross axis), flex-wrap and gap. For example, .nav { display: flex; justify-content: space-between; align-items: center; gap: 16px; } lays out a logo and links in a row, vertically centered, pushed to opposite ends. Adjust the controls above and copy the generated CSS — no need to memorize every property by hand.

How do I enable or activate flexbox?

Set display: flex on the parent (or display: inline-flex if you want the container itself to flow inline like text). That single declaration activates flexbox for all of its direct children — no extra setup, library or class is needed. Nothing happens to grandchildren unless you make those nested elements flex containers too, which this tool also supports with nested container controls.

Is CSS Flexbox still used in 2025?

Yes — flexbox is fully supported in every modern browser (over 99% global coverage) and is the standard tool for one-dimensional layouts: navbars, toolbars, button groups, form rows and centering. It hasn't been replaced by CSS Grid; the two complement each other. Reach for flexbox when you're laying out items along a single line, and Grid when you need rows and columns at the same time. Most real projects use both.

What's the difference between flexbox and grid?

Flexbox is one-dimensional — it lays content out along a single axis (a row or a column) and is content-first, sizing items by their content and available space. CSS Grid is two-dimensional, controlling rows and columns together with an explicit track structure. Use flexbox for a navbar, a row of cards or vertical centering; use Grid for a full page layout or an image gallery that must align in both directions. They're often combined: Grid for the page skeleton, flexbox inside each region for fine-grained alignment.

What does justify-content vs align-items do?

justify-content positions items along the main axis, while align-items positions them along the cross axis — and the main axis is whatever flex-direction sets. With the default flex-direction: row, justify-content spaces items horizontally (start, center, space-between…) and align-items aligns them vertically (top, center, bottom). Switch to flex-direction: column and the two swap: justify-content now works vertically and align-items horizontally. The live axis legend in the tool shows which is which as you change direction.

How do I center a div with flexbox?

Make the parent a flex container and center on both axes: .parent { display: flex; justify-content: center; align-items: center; } — that places the child in the exact middle horizontally and vertically. Give the parent a height (for example min-height: 100vh for full-screen centering) so there's space to center within. It's the cleanest way to perfectly center one element. Set both justify-content and align-items to 'center' in the controls above to generate the code.

What are flex-grow, flex-shrink, and flex-basis?

They're the three per-item properties that decide how a flex item sizes, usually written together as the flex shorthand. flex-basis is the starting size before free space is shared (e.g. 200px or auto). flex-grow is how greedily an item expands to fill leftover space (0 = don't grow, 1 = take a share). flex-shrink is how readily it shrinks when space is tight (1 = allowed, 0 = stay fixed). The common flex: 1 means equal growing for all items. Click an item in the preview to set its grow, shrink and basis.

How do I make flex items wrap?

Set flex-wrap: wrap on the container so items that don't fit move onto a new line instead of overflowing or squashing. By default flex-wrap is nowrap, forcing everything onto one line. With wrapping on, give items a flex-basis (such as flex: 1 1 200px) so each row fills neatly, and use align-content to space the resulting lines. Drag the resizable preview narrower to watch items wrap in real time inside this tool.

How do I space items evenly?

Use justify-content on the main axis. space-between pushes the first and last items to the edges with equal gaps between the rest; space-around gives each item equal space on both sides (so edge gaps look half-size); and space-evenly makes every gap — including the outer edges — identical. For four items in a 400px row, space-between leaves no outer margin, while space-evenly puts an equal gap before, between and after each item. A fixed gap value adds a guaranteed minimum spacing on top.

How do I reorder flex items?

Give an item the order property — order: -1 pulls it before items with the default order: 0, and a higher number pushes it later, all without touching the HTML source order. For example, order: 1 on the third item moves it to the end visually. It's useful for responsive tweaks like moving a sidebar after the main content on mobile. Note it's a visual reorder only, so keep the DOM order logical for accessibility. Select an item above and change its order to see the effect live.

How do I convert a flexbox layout to Tailwind classes?

Each flex property maps to a Tailwind utility: display: flex → flex, flex-direction: column → flex-col, justify-content: space-between → justify-between, align-items: center → items-center, flex-wrap: wrap → flex-wrap, and gap: 16px → gap-4. Per-item, flex: 1 → flex-1, order: 1 → order-1. So a centered row becomes class=\"flex justify-center items-center gap-4\". Switch the output tab above to Tailwind (or Bootstrap) and the tool writes the full class list — container and items — for you to copy in one click.

How does this compare to other CSS flexbox generators like Flexbox Froggy or Wes Bos's guide?

Flexbox Froggy and Wes Bos's What The Flexbox are learning tools — great for understanding the spec, but they don't generate code for your actual project. CSS Tricks's flexbox guide is a reference, not a generator. This tool is built for production use: you configure a real flex container with real child items, choose how many items you want, set per-item properties individually, and get CSS, Tailwind, or Bootstrap output you can paste straight into your project. It also handles nested containers, which most generators skip.

Is this flexbox generator free and private?

Free with no signup required. The entire playground runs in your browser — your layout, the generated CSS, and the share link are all built on your device and never uploaded to a server. You can bookmark it and use it offline once the page has loaded. There's no account to create and no limit on how many layouts you configure.