CSS Flexbox Generator
Visual flexbox layout builder — copy the CSS instantly
Live Preview
Generated CSS
.container {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: stretch;
gap: 8px;
}🔒 Runs entirely in your browser. No data is sent to any server.
Related Tools
CSS Gradient Generator
Build linear, radial and conic CSS gradients visually.
CSS Box Shadow Generator
Build CSS box shadows visually with multiple layers and presets.
PX to REM Converter
Convert pixels to rem and back with configurable base font size.
Color Contrast Checker
Check color contrast ratios for WCAG AA and AAA accessibility.
What Is the CSS Flexbox Generator?
The CSS Flexbox Generator is a visual layout tool that lets you configure a flex container by clicking options rather than memorizing property names and values. Adjust flex-direction, justify-content, align-items, flex-wrap, align-content, and gap interactively, see the result immediately in the live preview, then copy the ready-to-paste CSS with one click. Everything runs in your browser — no account, no uploads, and the CSS is generated locally without sending anything to a server.
How to Use the Flexbox Generator
- Set the flex-direction. Choose
rowto lay items horizontally (the default) orcolumnto stack them vertically. The-reversevariants reverse the order of items. - Pick a justify-content value. This distributes space along the main axis.
space-betweengives equal gaps between items with no gap at the edges;space-evenlygives equal gaps including at the edges. - Pick an align-items value. This aligns items on the cross axis.
stretch(the default) makes items fill the container height;centervertically centers them. - Enable flex-wrap if needed. Switch to
wrapto let items flow onto new lines. Thealign-contentoption appears when wrapping is on, controlling how the rows themselves are distributed. - Set a gap. Type a value like
16pxor1remto add uniform spacing between items. Clear the gap field to reveal separate row-gap and column-gap inputs. - Adjust item count. Drag the slider to add or remove preview boxes and see how the layout responds.
- Copy the CSS. Click Copy CSS and paste the generated code directly into your stylesheet.
Understanding the Main Flexbox Properties
flex-direction
flex-direction defines the main axis of the flex container, which determines which direction items flow. The default is row, which places items left-to-right in left-to-right languages. column stacks items top-to-bottom. The -reverse variants swap the start and end points: row-reverse places items right-to-left and column-reverse places them bottom-to-top. This also switches which side flex-start and flex-end refer to for other properties.
justify-content
justify-content distributes free space along the main axis after all items and gaps are placed. The six values each produce a different spacing pattern:
- flex-start — items pack toward the start of the main axis (left in a row).
- flex-end — items pack toward the end of the main axis (right in a row).
- center — items cluster in the middle of the main axis.
- space-between — equal gaps between adjacent items; no gap at the start or end.
- space-around — each item gets equal space on both sides; edge items have half the gap of adjacent items.
- space-evenly — equal gaps between all items and at the start and end edges.
align-items
align-items controls how items sit on the cross axis within their line. stretch makes items expand to fill the container height (the default). flex-start aligns items to the top of the row; flex-end to the bottom; center vertically centers them. baseline aligns items so their text baselines line up — useful when mixing different font sizes or padding amounts.
flex-wrap and align-content
With flex-wrap: wrap, items that cannot fit on a single line break onto new lines. align-content then controls how those lines are distributed within the container. It only has a visible effect when there are multiple lines — if all items fit on one line, align-content does nothing. Think of align-content as the multi-line equivalent of justify-content: it distributes space between rows rather than between individual items.
Real-World Flexbox Layout Examples
Example 1: Navigation Bar
A typical navigation bar has a logo on the left and links on the right. The classic approach uses flexbox with justify-content: space-between on the container:
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
gap: 16px;
}The logo element takes up as much space as it needs, and the remaining space is pushed to the right, pressing the nav links against the right edge. Adding align-items: center ensures the logo and links are vertically centered in the bar regardless of their individual heights.
Example 2: Centering a Modal
Centering a modal or card perfectly in the viewport is a two-line pattern that trips up many developers who still reach for margin: auto with position: absolute:
.overlay {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}The overlay covers the full viewport height, and flexbox automatically centers the modal child element in both dimensions. No calculations, no negative margins, no top: 50%; transform: translate(-50%, -50%).
Example 3: Responsive Card Grid
A simple responsive card row that wraps to fewer columns on narrow screens:
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
.card {
flex: 1 1 280px; /* grow, shrink, min-width */
max-width: 400px;
}Each card has a minimum width of 280px. If the container is wide enough for three cards, three appear per row. As the viewport narrows, cards wrap to fewer per row, and at small screen sizes each card takes the full width. This pattern eliminates the need for media query breakpoints in most cases.
Flexbox vs CSS Grid: Which Should You Use?
The most common question once you learn flexbox is when to switch to CSS Grid. The short answer: flexbox is one-dimensional, Grid is two-dimensional.
Use flexbox when your layout flows in one direction: a row of buttons, a column of form fields, a horizontal navigation. Flexbox excels at distributing space among items that vary in size and letting them grow or shrink to fill available room.
Use CSS Grid when you need explicit rows and columns simultaneously: a magazine-style page layout, a photo gallery with fixed grid lines, or a dashboard with named areas. Grid gives you precise control over placement in both dimensions.
In practice, most UIs combine both. A Grid might define the overall page structure (header, sidebar, main, footer), while Flexbox arranges the items inside each section. They compose naturally because flex items can themselves be grid containers and vice versa.
Common Flexbox Mistakes and How to Fix Them
Mistake 1: Putting flex properties on children instead of the parent
justify-content and align-items belong on the flex container (the element with display: flex), not on the flex items. Properties that go on children are flex-grow, flex-shrink, flex-basis, align-self, and order.
Mistake 2: Using margin to add gaps (pre-gap era habit)
Before the gap property was widely supported, developers used margin on items and then negated the edge margins on the container. The gap property is now supported in all modern browsers and is far simpler — it only applies spacing between items, never on the outer edges.
Mistake 3: Forgetting that the cross axis flips with flex-direction
In a row layout, the main axis is horizontal and the cross axis is vertical. In a column layout, the axes swap: the main axis is now vertical and the cross axis is horizontal. This means justify-content: center in a column layout centers items vertically (along the column), and align-items: center centers them horizontally (across the column).
Mistake 4: Using flexbox for grid-like layouts
If you find yourself fighting flexbox to get items to align in a strict grid — giving every item the same fixed width, preventing uneven last rows — you probably want CSS Grid. grid-template-columns: repeat(3, 1fr) enforces a strict three-column layout that flexbox cannot achieve without hacks.
The gap Property in Detail
The gap shorthand accepts one or two values: gap: 16px sets equal row and column spacing; gap: 8px 24px sets 8px between rows and 24px between columns. You can use any CSS length unit — px, rem, em, vw, or percentages. A value of 0 removes all spacing.
A subtle but important fact: gap only applies between items, never outside them. If you need spacing between the container edge and the first/last item, use padding on the container instead. Combining padding on the container with gap between items is the cleanest pattern for card grids and toolbars.
Controlling Individual Items: flex-grow, flex-shrink, and flex-basis
The generator handles container-level properties, but flex items have their own three properties that control how each item sizes itself relative to its siblings. All three are combined in the flex shorthand:
- flex-grow — a unitless number that determines how much of the free space a flex item claims. A value of
1on every item splits free space equally; a value of2on one item gives it twice as much free space as siblings with1. Default is0(items do not grow). - flex-shrink — controls how much an item shrinks when the container is too small to hold all items at their ideal size. The default is
1, meaning all items shrink proportionally. Settingflex-shrink: 0on an item prevents it from shrinking — useful for fixed-width sidebars or logos. - flex-basis — sets the item's ideal size before flex-grow and flex-shrink are applied. It works like
widthin a row layout andheightin a column layout. Useflex-basis: auto(the default) to size the item from its content, or a length like200pxor50%for an explicit starting size.
The most commonly used shorthand values are: flex: 1 (equal share of all available space), flex: 0 0 auto (fixed size, no grow or shrink), and flex: 1 1 280px (start at 280px, grow and shrink freely — the responsive card pattern). The W3C recommends always using the shorthand flex instead of individual properties because the shorthand sets sensible defaults for the other two values automatically.
Browser Support
CSS Flexbox has been supported in all major browsers since 2015. The gap property for flexbox (distinct from the older Grid-only grid-gap) has been supported since Chrome 84, Firefox 63, Safari 14.1, and Edge 84 — safe to use without fallbacks in all modern environments. You only need to worry about flexbox fallbacks if you still need to support Internet Explorer 11, which has a partial and buggy implementation.
Frequently Asked Questions
What is CSS flexbox and when should I use it?▾
CSS Flexbox (Flexible Box Layout) is a one-dimensional layout model that distributes space along a single axis — either a row or a column. You apply it by setting display: flex on a container element; all direct children become flex items that can grow, shrink, and align automatically. Use flexbox when you need to align items in a single direction: navigation bars, button groups, centering a card, distributing items with equal spacing, or building a responsive toolbar. For two-dimensional layouts (rows AND columns simultaneously), CSS Grid is the better choice.
What is the difference between justify-content and align-items in flexbox?▾
justify-content controls alignment along the main axis (the direction set by flex-direction). In a row layout the main axis runs left-to-right, so justify-content moves items horizontally. align-items controls alignment along the cross axis, which is perpendicular to the main axis. In a row layout the cross axis is vertical, so align-items moves items up or down within the container. A common pattern to center an item both ways is: display: flex; justify-content: center; align-items: center — this centers horizontally (main axis) and vertically (cross axis) at the same time.
What does flex-wrap do and when should I use it?▾
By default, all flex items are forced onto a single line even if they overflow the container (flex-wrap: nowrap). Setting flex-wrap: wrap tells the browser to wrap items onto new lines when they run out of horizontal (or vertical, in column layouts) space. This is essential for responsive designs where items should naturally reflow as the viewport shrinks. Use flex-wrap: wrap when your flex items have fixed or minimum widths and you want them to automatically stack into rows on smaller screens. wrap-reverse does the same but stacks new lines above (for row layouts) rather than below.
What is the difference between align-items and align-content?▾
align-items applies to each individual line of flex items, controlling how items are aligned within their own row (or column in a column layout). align-content controls how multiple lines of flex items are distributed within the container when flex-wrap is set to wrap and there is extra space in the cross axis. If all items fit on a single line — which is the default with flex-wrap: nowrap — align-content has no visual effect. A practical example: if you have three rows of wrapped flex items inside a tall container, align-content: space-between would spread those three rows to the top and bottom of the container with equal gaps between them.
How does the gap property work in flexbox?▾
The gap property (previously called grid-gap) sets the space between flex items — both between rows and columns if you use the shorthand. It is equivalent to writing row-gap and column-gap separately. For example, gap: 16px puts 16px between every pair of adjacent items; gap: 8px 16px sets 8px between rows and 16px between columns. Unlike margin, gap only applies to the space between items and never adds space before the first item or after the last. This makes it far cleaner than applying margin to every item and then removing the margin on edge items. Gap is supported in all modern browsers.
How do I center a div horizontally and vertically with flexbox?▾
The classic two-line centering trick in CSS: add display: flex; justify-content: center; align-items: center to the parent container. This centers the child along both the main axis (horizontal by default) and the cross axis (vertical). If the container does not have an explicit height, add height: 100vh to fill the full viewport height, or any other height value. You can also center a single item in a container that holds multiple items by giving the target child margin: auto — flex distributes any free space equally on all sides of the item when its margin is auto.
What are the most common CSS flexbox mistakes?▾
The five most common flexbox mistakes are: (1) Forgetting that flex properties apply to the container, not the items — justify-content and align-items go on the parent with display: flex, not on the children. (2) Expecting align-content to work without flex-wrap: wrap — align-content is only effective when there are multiple lines. (3) Confusing the main axis direction — when flex-direction is column, justify-content moves items vertically and align-items moves them horizontally, which is the reverse of a row layout. (4) Using width: 100% on flex items inside a column container when you meant height: 100%. (5) Overusing flexbox for two-dimensional layouts — if you need items in both rows and columns at the same time, CSS Grid is the correct tool.