CSS Flexbox Generator Online Free
The free CSS Flexbox Generator lets you build flex layouts without writing code first. Configure the container and each individual item, watch the live preview update instantly, then copy the generated CSS directly into your project.
What the CSS Flexbox Generator Does
The tool provides a visual interface for every core Flexbox property. On the left, a controls panel lets you set all container properties — flex-direction, flex-wrap, justify-content, align-items, align-content, and gap. Below that, each flex item has its own settings for flex-grow, flex-shrink, flex-basis, align-self, and order.
On the right, a live preview panel shows coloured boxes arranged inside a real flex container that updates as you change any property. Below the preview, the CSS Output panel shows the complete, ready-to-use stylesheet — one .container rule and one rule per item — with a Copy CSS button.
A Brief History of CSS Layout
Before Flexbox, CSS layout relied on floats, inline-block, and table-based approaches — all hacks repurposed from features designed for different purposes. Float-based layouts required clearfix hacks, overflow tricks, and constant fighting with collapsing containers. Vertically centring elements was famously difficult, with dozens of proposed workarounds none of which worked universally.
The CSS Flexible Box Layout Module (Flexbox) was introduced as a W3C specification in 2009 and reached broad browser support by around 2015–2016. It was the first CSS layout system designed explicitly for distributing space among items in a container, handling varying content sizes gracefully, and providing simple tools for alignment in both axes. CSS Grid followed, providing two-dimensional layout control that Flexbox deliberately did not address.
How to Use the Flexbox Generator
- Open the CSS Flexbox Generator. Three default items appear in a row layout.
- Use the Container dropdowns to set the direction, wrap behaviour, and alignment properties. The preview updates immediately.
- Adjust row-gap and column-gap to set spacing between items.
- In the Items section, set
flex-grow,flex-shrink, andflex-basisper item to control how each one sizes relative to the others. Usealign-selfto override the container'salign-itemsfor a specific item. - Click + Add to add more items (up to 8) or the × button to remove one.
- Click Copy CSS to copy the generated stylesheet to your clipboard.
Container Properties Reference
| Property | What It Controls | Common Values |
|---|---|---|
flex-direction | The main axis — which direction items flow | row, column, row-reverse, column-reverse |
flex-wrap | Whether items wrap to new lines when they overflow | nowrap, wrap, wrap-reverse |
justify-content | Alignment along the main axis | flex-start, center, space-between, space-evenly |
align-items | Alignment along the cross axis for a single line | stretch, flex-start, center, baseline |
align-content | Alignment of multiple lines when wrapping is active | stretch, center, space-between |
gap | Space between items (row-gap and column-gap) | 8px, 16px, 1rem |
Item Properties Reference
| Property | What It Controls | Default |
|---|---|---|
flex-grow | How much of remaining space this item claims relative to others | 0 (does not grow) |
flex-shrink | How much this item shrinks relative to others when space is tight | 1 (shrinks proportionally) |
flex-basis | The item's initial size before remaining space is distributed | auto (uses content size) |
align-self | Overrides the container's align-items for this item only | auto (inherits from container) |
order | Visual display order without changing HTML source order | 0 |
Understanding the Flex Shorthand
The flex shorthand property sets flex-grow, flex-shrink, and flex-basis in one declaration. The single-value syntax is the most common in production code:
| Shorthand | Expands To | Meaning |
|---|---|---|
flex: 1 | flex: 1 1 0% | Grow, shrink, start from zero — equal shares |
flex: auto | flex: 1 1 auto | Grow, shrink, start from content size |
flex: none | flex: 0 0 auto | Rigid — neither grows nor shrinks |
flex: 0 | flex: 0 1 0% | Shrinks but does not grow, starts at zero |
flex: 2 | flex: 2 1 0% | Claims twice as much remaining space as flex: 1 items |
Common Flexbox Patterns
Equal-width columns
Set flex: 1 on every item (flex-grow: 1, flex-shrink: 1, flex-basis: 0%). All items share the available space equally regardless of their content size. This is the most-used Flexbox pattern in component libraries.
Sticky footer
On the page wrapper, set display: flex, flex-direction: column, and min-height: 100vh. Give the main content area flex: 1(or flex-grow: 1). The footer then stays at the bottom even on short pages, because the main content stretches to fill the remaining height. This replaced dozens of older sticky-footer hacks.
Centring anything
On the container: display: flex, justify-content: center, and align-items: center. The child element is perfectly centred regardless of its size or the container's dimensions. This is now the universally recommended way to centre elements vertically — something that required complex workarounds before Flexbox.
Navigation bar
A typical nav bar: display: flex, align-items: center on the container. The logo gets flex: none to stay at its natural size. The nav links get flex: 1 or a spacer element with flex: 1 is placed between the logo and the links to push them to the right. This creates the common logo-left, links-right layout without absolute positioning.
Responsive card row
Set flex-wrap: wrap on the container and flex-basis: 220px withflex-grow: 1 on the items. Cards fill the row and wrap naturally as the viewport narrows — no media queries needed for the basic wrapping behaviour. Add amax-width to prevent cards from becoming too wide on large screens.
Sidebar layout
Container: display: flex, flex-direction: row. Sidebar:flex: 0 0 260px (fixed width, no grow, no shrink). Main content: flex: 1 (takes all remaining space). Both elements stretch to the same height automatically because align-items defaults to stretch.
Reordering without changing HTML
Use the order property on individual items to change their visual position independently of DOM order. This is useful for mobile layouts where a sidebar needs to appear after the main content in the source (for reading order and accessibility) but before it visually on large screens.
Debugging Common Flexbox Problems
Items are not growing to fill the container
Check that the container has a defined width or height (or is constrained by a parent). A flex container with no width is as wide as its content — there is no remaining space to distribute. Also check that the items have flex-grow: 1 or flex: 1 set, and that flex-basis is not forcing a specific size.
align-content is not working
align-content only applies when there are multiple lines of flex items — i.e. when flex-wrap is set to wrap or wrap-reverseand the items actually overflow onto a second line. With nowrap, all items stay on one line and align-content has no effect.
Items are shrinking below their minimum content size
By default, flex-shrink: 1 means items will shrink, but browsers will not shrink items below their minimum content size (min-width: auto). If an item with overflow text is collapsing unexpectedly, set min-width: 0 on the item to allow it to shrink below content size, then control overflow with overflow: hidden or text-overflow: ellipsis.
Flex item is stretching too tall
The default align-items: stretch makes all items in a row the same height as the tallest item. If you want items to be only as tall as their content, set align-items: flex-start on the container, or align-self: flex-start on the specific item.
justify-content: space-between leaves an odd item at the left
With flex-wrap: wrap and justify-content: space-between, the last row may have items aligned to the left with a gap where the last item should be. There is no pure Flexbox fix for this — it is a known limitation. Solutions include adding invisible spacer elements, using CSS Grid instead, or accepting the alignment as-is and only applying space-between when items always fill the row.
Flexbox vs CSS Grid
Flexbox and Grid solve different problems. Flexbox is one-dimensional — it arranges items along a single row or column and distributes leftover space within that axis. Grid is two-dimensional — it controls both rows and columns simultaneously and allows precise placement into named areas.
| Scenario | Better Choice | Reason |
|---|---|---|
| Navigation bar | Flexbox | One row of items with distribution |
| Card grid with known columns | Grid | Two-dimensional — rows and columns defined |
| Dynamic card list (variable count) | Flexbox + wrap | Items determine their own wrapping |
| Full page layout (header, aside, main, footer) | Grid | Named areas, cross-axis placement |
| Centering a single element | Flexbox | Simpler, fewer properties needed |
| Aligning items in a form | Either | Grid gives column alignment; Flexbox handles flow |
Flexbox and Accessibility
The order property changes visual order without changing DOM order. Screen readers and keyboard navigation follow DOM order, not visual order. Using order to rearrange content for visual reasons can create a confusing disconnect for keyboard users — tab order will follow the HTML source, not what is visible on screen.
The WCAG Success Criterion 1.3.2 (Meaningful Sequence) and 2.4.3 (Focus Order) require that reading and navigation order make sense independently of presentation. Use order only for purely visual reordering that does not affect reading sequence, and ensure the DOM order makes sense when CSS is disabled.
Flexbox in CSS Frameworks
Most modern CSS frameworks use Flexbox as their primary layout mechanism. Understanding the underlying CSS means you can debug framework layouts and extend them without fighting the framework.
Tailwind CSS
Tailwind exposes Flexbox via utility classes that map directly to CSS properties: flex, flex-row, flex-col, justify-between, items-center, gap-4, flex-1, flex-none. The generator output translates cleanly into Tailwind utilities — if justify-content: space-between is in the output, the Tailwind equivalent is justify-between.
Bootstrap
Bootstrap 4+ uses Flexbox for its grid system and utility classes. The d-flex, justify-content-*, align-items-*, and flex-* utilities map to the same Flexbox properties you configure in this generator.
Browser Support
Flexbox has full support in all modern browsers (Chrome, Firefox, Safari, Edge) since around 2016. The generated CSS uses standard syntax with no vendor prefixes — you do not need -webkit- or -ms- prefixes for any of the properties in the output.
The only Flexbox feature with incomplete support is gap in Safari before version 14.1 (released April 2021). If you need to support Safari 14.0 or earlier, use margins on items instead of gap. All other Flexbox properties in the generator output have been universally supported since 2016.
Common Questions
Why isn't align-content doing anything?
align-content only applies when there are multiple lines of flex items — i.e. when flex-wrap is set to wrap or wrap-reverse and the items actually overflow onto a second line. With nowrap, all items stay on one line so align-content has no effect.
What is the difference between flex-basis: auto and flex-basis: 0?
With auto, each item starts at its natural content size before remaining space is distributed. With 0, all items start at zero width and the available space is divided purely by their flex-grow ratios — making it easier to create truly equal-width columns unaffected by content length. flex: 1 uses flex-basis: 0%, which is why it produces equal-width columns.
Can I use Flexbox for the whole page layout?
Yes, though CSS Grid is often a better fit for full-page two-dimensional layouts. Flexbox works well for page-level layouts when items flow naturally in one direction — for example a vertical stack of header, main, and footer. For more complex row-and-column arrangements, Grid gives you finer control with named areas and explicit column/row sizing.
Does the generated CSS work in all browsers?
Flexbox has full support in all modern browsers. The generated CSS uses standard syntax with no vendor prefixes required. If you need to support Safari older than 14.1, replace gap with margins for the spacing.
Why is my flex item ignoring the width I set?
When an item is inside a flex container, width is overridden by flex-basis. If you set width: 200px on a flex item but the container has remaining space and the item has flex-grow: 1, the item will grow beyond 200px. Use flex-basis: 200px with flex-grow: 0 and flex-shrink: 0 (or the shorthand flex: 0 0 200px) to create a fixed-size flex item.
Build Your Flexbox Layout Free
Visual controls, live preview, and instant CSS copy. No signup, runs in your browser.
Open CSS Flexbox Generator