Flexbox Preview
Generated CSS

CSS Flexbox Generator

CSS Flexbox (Flexible Box Layout) is a one-dimensional layout model that gives you powerful tools for distributing space and aligning items inside a container — even when their size is unknown or dynamic. It replaced decades of float-based and table-based layout hacks and is now the go-to tool for navigation bars, toolbars, card rows, centred hero sections, and any component where items sit along a single axis.

This visual flexbox generator lets you experiment with every container property in real time, see how your layout responds as you add or remove items, vary item sizes, and download or copy the clean CSS output ready for your project.

How to Use This Flexbox Generator

  1. Set flex-direction — choose whether flex items flow in a row (left to right) or column (top to bottom). The -reverse variants flip the order.
  2. Choose justify-content — controls spacing along the main axis. space-between is the classic navbar pattern; center works for hero sections.
  3. Set align-items — controls alignment on the cross axis. stretch makes all items the same height; center vertically centres items of different heights.
  4. Toggle flex-wrapwrap lets items flow onto a new line when space runs out. Essential for responsive card grids.
  5. Adjust gap — row-gap and column-gap add space between items without needing margin hacks on individual elements.
  6. Add or remove items — use the + / − buttons to see exactly how your layout behaves with different numbers of children. Toggle "Vary item sizes" to test unequal widths.
  7. Download or copy — click Download HTML for a complete standalone demo file, or Copy CSS for just the container properties.

Flexbox Container Properties

All flexbox properties on the container (the parent element) are covered by this generator. Here is the complete reference:

.container {
  display: flex;               /* or inline-flex */
  flex-direction: row;         /* row | row-reverse | column | column-reverse */
  justify-content: flex-start; /* flex-start | flex-end | center | space-between | space-around | space-evenly */
  align-items: stretch;        /* stretch | flex-start | flex-end | center | baseline */
  flex-wrap: nowrap;           /* nowrap | wrap | wrap-reverse */
  gap: 16px;                   /* row-gap and column-gap together */
  row-gap: 16px;               /* space between rows when wrapping */
  column-gap: 16px;            /* space between columns */
}

Flexbox Item Properties

Individual flex items (children of the flex container) can override the container's alignment and control how they grow or shrink. These are not generated by this tool but are essential for production layouts.

.item {
  flex-grow: 0;    /* How much the item grows to fill available space (default 0) */
  flex-shrink: 1;  /* How much the item shrinks when space is tight (default 1) */
  flex-basis: auto;/* The item's starting size before grow/shrink applies */
  flex: 1;         /* Shorthand: grow=1, shrink=1, basis=0 — equal width items */
  align-self: auto;/* Override align-items for this item only */
  order: 0;        /* Change visual order without changing DOM order */
}

Common Flexbox Patterns

These are the most frequently used flexbox patterns in real production codebases. Copy and adapt them directly.

/* Perfect centring — both axes */
.centered {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Navigation bar — logo left, links right */
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
  height: 64px;
}

/* Equal-width columns */
.cols > * { flex: 1; }
.cols {
  display: flex;
  gap: 24px;
}

/* Sticky footer — push footer to bottom */
body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
main { flex: 1; }

/* Responsive card row that wraps */
.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}
.card {
  flex: 1 1 280px; /* grow, shrink, minimum width */
}

The Main Axis and Cross Axis

Understanding flexbox requires knowing which axis is which. The main axis runs in the direction of flex-direction. The cross axis is always perpendicular to it. justify-content always controls the main axis; align-items always controls the cross axis. The axes flip when you change flex-direction from row to column.

flex-directionMain axisCross axisjustify-content controlsalign-items controls
row (default)Horizontal →Vertical ↓Horizontal spacingVertical alignment
row-reverseHorizontal ←Vertical ↓Horizontal spacingVertical alignment
columnVertical ↓Horizontal →Vertical spacingHorizontal alignment
column-reverseVertical ↑Horizontal →Vertical spacingHorizontal alignment

Browser Support

Flexbox is fully supported in all modern browsers with no vendor prefixes required. The gap property for flexbox (as opposed to Grid) gained universal support in 2021 — use with confidence in all current projects.

FeatureChromeFirefoxSafariEdge
display: flex29+28+9+12+
flex-wrap29+28+9+12+
gap (in flexbox)84+63+14.1+84+
align-self29+28+9+12+
order29+28+9+12+

Frequently Asked Questions

What is the difference between justify-content and align-items?

justify-content distributes space along the main axis — horizontal spacing when flex-direction: row. align-items controls alignment on the cross axis — vertical alignment when flex-direction: row. Both axes flip when you switch to flex-direction: column, so justify-content then controls vertical distribution and align-items controls horizontal alignment.

How do I centre a div both horizontally and vertically with flexbox?

Apply display: flex; justify-content: center; align-items: center; to the parent container. This works regardless of the child's size and is the most reliable centring technique in CSS — replacing older negative-margin and transform hacks. For full-page centring, also add min-height: 100vh to the parent.

When should I use flexbox vs CSS Grid?

Use flexbox for one-dimensional layouts where items flow in a single row or column and content drives sizing. Use CSS Grid for two-dimensional layouts where you define explicit rows AND columns. In practice: flexbox for navigation bars, button groups, form rows, and component internals; Grid for page layouts, card grids, and any layout where both row and column positions matter.

What does flex: 1 mean?

flex: 1 is shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0%. It means the item will grow to fill available space equally with other flex: 1 siblings. Applying flex: 1 to all children of a flex row creates equal-width columns. flex: 2 on one item makes it twice as wide as flex: 1 siblings.

What does flex-wrap do and when should I use it?

By default flexbox forces all items onto one line, shrinking them to fit. flex-wrap: wrap allows items to flow onto new lines when they run out of space. This is essential for responsive layouts — combined with flex: 1 1 280px on items, it creates a card grid that automatically reflows from multi-column to single-column on small screens without any media queries.

What is the difference between gap, margin, and padding in flexbox?

gap adds space between flex items only — not on the outer edges of the container. This avoids the classic "first/last child extra spacing" problem you get with margin. padding on the container adds inner spacing inside its boundaries. Use gap for spacing between items and padding for the container's inner breathing room.

How do I make one item take up all remaining space?

Apply flex: 1 or flex-grow: 1 to that item. This tells it to expand and fill all available space not claimed by other items. A common pattern for the main content area in a navigation layout: the logo and action buttons have fixed sizes, and the search bar or spacer in the middle has flex: 1.

How do I reverse the order of flex items without changing the HTML?

Use flex-direction: row-reverse or column-reverse to reverse all items visually. To reorder individual items, use the order property on each item — lower values appear first (default is 0). Note that changing visual order with flexbox does not change DOM order, which can cause accessibility issues for keyboard navigation and screen readers if overused.

Related CSS Tools

Flexbox works alongside many other CSS properties. These tools on CSSTools.io help you build complete layouts:

Tool last updated: