Free CSS Flexbox Generator
Build flexbox layouts visually · adjust container & item properties, see live preview, copy the CSS.
Container Properties
Live Preview
Generated CSS
How It Works
- Set container properties: configure the flex container, flex-direction, justify-content, align-items, flex-wrap, and gap.
- Add and configure flex items: add child elements and set individual flex-grow, flex-shrink, flex-basis, align-self, and order values.
- Copy the CSS: get the complete container and item CSS ready to use in your project.
Why Use Flexbox Generator?
Flexbox is the essential tool for one-dimensional CSS layout, aligning items in a row or column with powerful distribution and alignment controls. But the properties are numerous and their interactions complex: justify-content vs align-items, flex-grow vs flex-basis, main axis vs cross axis. This interactive generator provides instant visual feedback as you toggle each property, making it the fastest way to learn Flexbox and get working CSS for your specific layout.
Features
- Interactive preview: drag flex items and toggle properties to see layout changes in real time.
- All container properties: flex-direction, flex-wrap, justify-content, align-items, align-content, and gap.
- Per-item control: set flex-grow, flex-shrink, flex-basis, align-self, and order for each item individually.
- Visual axis indicators: main axis and cross axis are highlighted to reinforce the conceptual model.
- CSS output: complete, copy-ready CSS for both the container and all configured items.
Frequently Asked Questions
What is the difference between justify-content and align-items?
justify-content distributes items along the main axis (horizontal by default). align-items aligns items along the cross axis (vertical by default). When flex-direction is column, the axes swap, justify-content becomes vertical and align-items becomes horizontal.
When should I use Flexbox vs CSS Grid?
Use Flexbox for one-dimensional layouts, a row of buttons, a navigation bar, a list of cards that should wrap. Use CSS Grid for two-dimensional layouts where you need to control both rows and columns simultaneously, like a full page layout or a complex card grid.
What does flex: 1 mean?
flex: 1 is shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0%. It tells the element to grow to fill available space, shrink if needed, and start from zero size before distributing space. All items with flex: 1 share space equally.
What Flexbox actually does
Flexbox (Flexible Box Layout) is a CSS layout model designed for distributing space and aligning items in a one-dimensional direction: either a row or a column. It introduces two core concepts that determine how everything behaves: the main axis (the primary direction items flow, horizontal by default) and the cross axis (perpendicular to main, vertical by default). Once you internalize main vs cross, every Flexbox property maps to one of them: justify-content works on the main axis, align-items works on the cross axis, flex-direction swaps which axis is which.
Flexbox solved a set of layout problems that plagued web developers from 1998 to 2014. Before Flexbox, centering an element vertically required hacks (display: table-cell, negative margins, position: absolute with translate transforms). Equal-height columns required the "faux columns" technique with background images. Sticky footers needed precise pixel calculations. Flexbox made all of these trivial: a one-line declaration replaces decades of CSS workarounds. The trade-off is that Flexbox is one-dimensional; for two-dimensional layouts (row AND column simultaneously), CSS Grid (shipped 2017) is the better tool.
Flexbox properties divide cleanly into two groups: container properties (applied to the parent: display: flex, flex-direction, flex-wrap, justify-content, align-items, align-content, gap) and item properties (applied to children: flex-grow, flex-shrink, flex-basis, align-self, order). The container properties control layout patterns; the item properties control individual exceptions. This generator surfaces both sets so you can experiment with how they interact without writing code.
How this tool works under the hood
The live preview is a flex container with sample child elements. As you change container properties (flex-direction, justify-content, etc.) via dropdowns and inputs, JavaScript updates the inline style of the preview container, and the browser re-renders the layout. The preview is the actual browser's Flexbox implementation, not a JavaScript simulation: you see exactly what your CSS will produce on a real page.
Per-item controls let you adjust each child individually. You can add or remove items, set their flex-grow, flex-shrink, flex-basis, align-self, and order values, and see how the container redistributes space accordingly. Visual axis indicators show the main and cross axes for the current configuration, reinforcing the mental model. The properties are real CSS that you can copy directly into your stylesheet, no transpilation or framework prefixes needed.
The Generated CSS panel updates with each interaction, producing two CSS rules: one for the container (with the chosen flex properties) and one for the items. Click Copy CSS and the rules are written to your clipboard. The tool itself never makes network requests; the preview rendering, code generation, and clipboard write all happen in JavaScript on your device. Refresh the page and your configuration is gone unless you copied the CSS first.
Brief history of Flexbox
- CSS 2.1 layout chaos, 1998 to 2008. Before Flexbox, web developers used
float: leftwith clearfix hacks,display: inline-blockwith whitespace bugs, ordisplay: tablewith table-cell tricks for layout. Vertical centering requiredvertical-alignon table-cells, negative margin tricks, or absolute positioning. Equal columns required JavaScript. The web community generates thousands of blog posts about "how to do X with CSS" workarounds. - First Flexible Box draft, 2009. The W3C CSS Working Group publishes the first Working Draft of CSS Flexible Box Layout Module in July 2009, with three syntaxes evolving over the next five years: the original
box-*syntax (rapidly deprecated), the 2011flexboxintermediate syntax (still seen in legacy IE 10 code), and the finalflexsyntax we use today. - Browser support stabilizes, 2014. Chrome 29, Firefox 28, Safari 9, and IE 11 all ship working Flexbox by 2014, with IE 11 retaining buggy edge cases. The transition from "Flexbox is the future" to "Flexbox is the present" happens in 2014-2015 as production sites adopt it for major layouts. The "Holy Grail Layout" (header, footer, two sidebars, main content) becomes trivial.
- Chris Coyier's "A Complete Guide to Flexbox", 2013. CSS-Tricks publishes Chris Coyier's comprehensive Flexbox guide (April 2013), which becomes the canonical reference. By 2016, the guide is among the top results for any Flexbox question and is translated into dozens of languages. It dramatically accelerates Flexbox adoption.
- CSS Grid arrives as complement, 2017. CSS Grid Layout ships in Chrome 57, Firefox 52, and Safari 10.1 (March 2017). Far from replacing Flexbox, Grid complements it: Flexbox excels at one-dimensional row/column layouts, Grid excels at two-dimensional layouts. Modern designs typically use both: Grid for page-level structure, Flexbox for component-internal arrangements (nav items, button groups, card content).
- gap property and subgrid land, 2020 to 2024. The
gapproperty (originally Grid-only) becomes Flexbox-compatible across all major browsers by Safari 14.1 (April 2021). The cleanergap: 1remsyntax replaces the older "margin on every child except the last" patterns. CSS Subgrid (2023+) lets nested grids align with parent grid tracks, useful for component-system layouts.
Real-world workflows
- Navigation bars. A horizontal nav with logo on the left and links on the right is the canonical Flexbox use case.
display: flex; justify-content: space-between; align-items: center;on the nav container, and you're done. Addgap: 1.5remfor spacing between links. This 4-line CSS replaces what used to require floats, clearfix, and pixel-perfect margin math. - Vertical and horizontal centering. The classic "how do I center this div?" question has a Flexbox answer:
display: flex; justify-content: center; align-items: center;on the parent. Works regardless of the child's intrinsic size, regardless of the parent's height, regardless of dynamic content changes. This is the single biggest quality-of-life improvement Flexbox brought to web development. - Card-list layouts with wrapping. A row of cards that wraps to multiple rows on narrow screens:
display: flex; flex-wrap: wrap; gap: 1rem;on the container, andflex: 1 1 250pxon each card. Cards grow to fill available space, shrink down to 250px minimum, and wrap to new rows when 250px doesn't fit. Responsive design without media queries. - Form rows with labeled inputs. Forms often need labels and inputs aligned:
display: flex; gap: 0.5rem;on the row,flex: 0 0 120pxon the label (fixed 120px width),flex: 1on the input (fills remaining space). Resizing the form container keeps the label fixed and stretches the input. Easier than CSS Grid for two-column form rows. - Sticky footers. Making the footer stick to the bottom of the viewport when content is short, but follow content when it's long:
body { display: flex; flex-direction: column; min-height: 100vh; }withmain { flex: 1 }pushes the footer down. The classic "sticky footer" problem solved in three CSS declarations, replacing the negative-margin workarounds of the 2010s. - Tag and badge groups. Lists of tags, chips, or badges that should flow horizontally and wrap to new lines:
display: flex; flex-wrap: wrap; gap: 0.5rem;. Each tag sizes itself, and the container handles arrangement. Used heavily in filtering UIs, content tagging, search interfaces. Combines well with the:has()selector for state-dependent styling.
Common pitfalls and what they mean
- The min-width: auto trap. Flex items have an implicit
min-width: auto(ormin-height: autofor column directions) that prevents them from shrinking below their content's intrinsic minimum size. This causesoverflow: hiddenand text-truncation patterns to fail unexpectedly inside flex containers. Fix: explicitly setmin-width: 0on the problematic flex item. This is the single most common "why is my flex layout broken?" cause. - flex-basis vs width confusion.
flex-basisis the initial size of a flex item along the main axis, before grow/shrink calculations.width(orheightfor column directions) is the regular CSS size. When both are set, flex-basis wins. The shorthandflex: 1 1 200pxsets flex-basis to 200px. For most cases, prefer flex-basis over width inside flex containers for clarity. - gap support in older browsers. The
gapproperty was originally Grid-only and arrived in Flexbox more recently: Safari 14.1 (April 2021), and earlier in Chrome/Firefox. For projects supporting older Safari (or older WebView in iOS apps), provide fallbacks using margins on children. Most modern projects can safely use gap; check your specific browser support targets. - Default flex-shrink can cause unexpected sizing. Flex items default to
flex-shrink: 1, meaning they can shrink below their declared width if the container is too narrow. To keep an element at its declared width regardless of available space, setflex-shrink: 0. Common with sidebars that should stay 250px wide even when the main content area pushes them:flex: 0 0 250pxinstead offlex-basis: 250px. - Percent flex-basis is relative to container.
flex-basis: 50%means "50% of the flex container's main-axis size." This works for typical layouts but interacts weirdly withflex-wrap: if the container itself has a percent-based main-axis size in a nested flex context, the math compounds. When in doubt, use pixel values ormin()/max()/clamp()for flex-basis instead of percentages. - IE 11 Flexbox has buggy edge cases. Internet Explorer 11 (retired June 2022) implemented Flexbox with several bugs: min-height not respected, broken nested flex containers,
flex: 1sometimes treating flex-basis as 0px instead of 0%, the "two-value flex" shorthand handled inconsistently. For projects no longer supporting IE, ignore. For legacy enterprise software, test thoroughly or use alternative layout approaches.
Privacy: everything runs in your browser
CSS learning and layout tools come in two flavors: simple HTML pages running client-side JavaScript (private, fast, no setup) and cloud editors with saved projects (collaborative but with privacy trade-offs). This tool is the first kind. Your property selections, your item configurations, your generated CSS: all stay in your browser session. Refresh the page and the state is gone unless you copied the CSS first.
The privacy stakes are low here because Flexbox configurations rarely contain sensitive information. Still, the lack of analytics matters: you can experiment freely without your trial-and-error process being recorded. Particularly useful in classroom or corporate training settings where having students or trainees register accounts on third-party platforms is a friction point. The tool is a single static page with no backend, usable in any browser, including locked-down enterprise machines.
When another tool is the right pick
- CSS Grid for two-dimensional layouts. Flexbox is one-dimensional; if you need to align items in both rows and columns simultaneously (e.g., a magazine-style layout with mixed-size cards), CSS Grid is dramatically better suited. Use our CSS Grid Generator for that kind of work. The two work great together: Grid for page-level structure, Flexbox for component-internal arrangements.
- HTML tables for tabular data. If your data is actually a table (rows representing records, columns representing fields), use a real
<table>element with<tr>and<td>. Tables provide semantic meaning to screen readers, sortable headers, and built-in alignment. Flexbox/Grid for tables is a 2010-era antipattern; modern accessibility guidelines explicitly recommend semantic HTML tables for tabular data. - position: absolute for overlays and tooltips. Modal overlays, tooltips, dropdown menus, and similar floating UI elements work best with
position: absoluteorposition: fixedrather than Flexbox. Flex/Grid manage layout flow; absolute positioning lifts elements out of flow entirely. Combine them: Flexbox for the main page layout, absolute positioning for the modal that overlays it. - CSS Subgrid for nested alignment. When nested layouts need to align with the parent's grid (e.g., card titles all aligning across a card grid), CSS Subgrid (Firefox 71 2019, Chrome 117 2023, Safari 16 2022) is more appropriate than nested Flexbox. Subgrid lets children inherit grid tracks from the parent, ensuring perfect alignment across nested structures. Worth learning for design-system component work.
Other frequently asked questions
What does the flex shorthand mean exactly?
The flex shorthand sets three properties at once: flex: <grow> <shrink> <basis>. Common values: flex: 1 (1, 1, 0%) for equal-distribution items, flex: 0 0 auto for fixed-size items, flex: 1 1 250px for items that grow/shrink from a 250px starting point. The shorthand has special initial values when only one or two values are provided; the explicit three-value form avoids surprises.
How do I make items wrap to multiple rows?
Set flex-wrap: wrap on the container. Items overflow to a new line when they don't fit. Combine with flex-basis on items to control how many fit per row: flex: 1 1 200px creates a responsive grid where items are at least 200px, grow to fill remaining space, and wrap to new rows on narrow screens. The container gap property handles spacing between wrapped rows and items.
Can I reverse the order of flex items?
Yes, multiple ways. Container-level: flex-direction: row-reverse (or column-reverse) reverses all items in order. Item-level: the order property assigns numeric weights; items with lower numbers appear first. Default order is 0; setting order: -1 on one item moves it to the front. Accessibility note: visual reordering doesn't affect tab order or screen-reader reading order, which can confuse keyboard and assistive-tech users. Use sparingly.
What is the difference between align-items and align-content?
align-items aligns items within their row (or column) along the cross axis. align-content aligns the rows (or columns) themselves within the container, only meaningful when there are multiple wrapped lines. If flex-wrap is set to nowrap (the default), align-content has no effect because there's only one line. Use align-items for single-line alignment, align-content for multi-line alignment.
How do I center a single element with Flexbox?
The classic answer: display: flex; justify-content: center; align-items: center; on the parent container. This centers the child both horizontally and vertically. For just horizontal: use justify-content: center only. For just vertical: use align-items: center only. If you need to center inside a parent of unknown height, add min-height: 100vh to the parent so it has vertical space to center within. The whole pattern replaces 20 years of CSS centering workarounds.
Why doesn't my text truncation work in a flex container?
Because of the min-width: auto default on flex items. Text-overflow: ellipsis requires the element to actually overflow its container; with the default minimum width set to the content's intrinsic size, the element never overflows. Fix: add min-width: 0 to the flex item containing the truncating text. This is the most common Flexbox bug Stack Overflow answers ever: searching "flex text-overflow ellipsis not working" returns thousands of versions of this exact answer.