Skip to content

Flexbox Generator

Main axis left to right Cross axis top to bottom Click an item to edit it
Items
4
Dashed lines and number badges only show here, they're not in the code.
display turns flexbox on
flex-direction sets the main axis
flex-wrap one line or many
justify-content along the main axis
align-items across, inside each line
align-content moves whole lines

Only works with flex-wrap: wrap or wrap-reverse, so turn on wrap first.

gap
Container size untick for auto

CSS

.container {
  display: flex;
  gap: 12px;
  height: 220px;
}

.item {
  /* Demo look: swap for your own styles */
  padding: 12px 16px;
  border-radius: 8px;
  background: #a3e635;
  color: #0b0f19;
  font-weight: 600;
}

.item-3 {
  font-size: 28px;
}

HTML

<div class="container">
  <div class="item item-1">1</div>
  <div class="item item-2">2 has more text</div>
  <div class="item item-3">3</div>
  <div class="item item-4">4</div>
</div>

Values left at their default (row, nowrap, flex-start, stretch) aren't written.

    Presets Common layouts, one click to load

    CSS Flexbox Generator: Build Flex Layouts Visually

    What is flexbox?

    Flexbox is the part of CSS that lines things up in a row or a column. You put display: flex on a parent element (the container), and its direct children (the items) sit side by side. Then a handful of properties decide where they go: justify-content spreads them out, align-items lines them up the other way, gap puts space between them, and flex-grow lets one of them take the space that’s left.

    Navbars, toolbars, button rows, cards with a footer at the bottom and the famous “center a div” are all flexbox jobs. This generator lets you click through every option on real boxes and copies the CSS and HTML for you.

    How to Use It, Step by Step

    Five quick steps. Everything happens in your browser, and your last layout is remembered for next time.

    Step 1 Start From a Preset

    Pick one under Presets: Center everything, Navbar, Equal columns, Sidebar + main, Card grid, Sticky footer, Holy grail-ish or Space between buttons. Or keep the four default boxes and build from there. Already have CSS? Click Paste CSS, paste your rules and press Load.

    Step 2 Set Up the Container

    On the Container tab, click a value for each property. Every value has a small drawing that shows what it does, and the drawings turn when you switch to a column. The badges above the preview tell you which way the main axis and the cross axis run right now.

    Step 3 Click an Item to Change Just That One

    Click a box in the preview (or pick its number on the Item tab) to set its flex-grow, flex-shrink, flex-basis, order, align-self, size and text. Use the + and − buttons under the preview for 1 to 12 items.

    Step 4 Read the Notes

    The notes under the code tell you when a setting does nothing (like align-content without wrap), when a growing item eats the free space, and when you need a height to see a change.

    Step 5 Copy the Code

    Copy all gives you the CSS in a <style> tag plus the HTML, ready to paste into a page. The preview runs exactly that code. Untick Demo colors in the code if you only want the layout, or switch to Tailwind for classes like flex justify-between items-center gap-4.

    Main Axis and Cross Axis

    This is the one idea that makes flexbox click. Every flex container has two directions. The main axis is the way the items flow, set by flex-direction. The cross axis runs across it.

    flex-direction: row: main axis left to right, cross axis top to bottom.
    flex-direction: column: the two axes swap.

    Flexbox Cheat Sheet: Container Properties

    These go on the parent, the element with display: flex.

    display: flex and inline-flex

    display: flex turns flexbox on. The container itself is still a block, so it takes the full width, and its children become flex items. display: inline-flex does the same inside, but the container is only as wide as its items and sits in a line of text, like a button with an icon. Only direct children are flex items. Grandchildren are laid out normally.

    flex-direction

    flex-direction: row;

    The default. Items go left to right.

    flex-direction: row-reverse;

    Right to left, starting at the right edge.

    flex-direction: column;

    Top to bottom, like normal blocks.

    flex-direction: column-reverse;

    Bottom to top.

    flex-wrap

    flex-wrap: nowrap;

    The default. Items squeeze onto one line.

    flex-wrap: wrap;

    Items that don’t fit move to a new line.

    flex-wrap: wrap-reverse;

    New lines go above instead of below.

    💡 flex-flow: row wrap is a shorthand for flex-direction and flex-wrap in one line.

    justify-content (main axis)

    justify-content: flex-start;

    The default. Packed at the start.

    justify-content: center;

    Packed in the middle.

    justify-content: flex-end;

    Packed at the end.

    justify-content: space-between;

    First and last touch the edges, equal space between.

    justify-content: space-around;

    Equal space around each item, so the edges get half.

    justify-content: space-evenly;

    Exactly equal space everywhere, edges too.

    start and end also work. They follow the writing direction of the page instead of the flex direction, so with row-reverse, flex-start is on the right but start stays on the left. For everyday layouts, the flex- versions are what you want.

    align-items (cross axis)

    align-items: stretch;

    The default. Items fill the line’s height.

    align-items: flex-start;

    Lined up at the top.

    align-items: center;

    Centered vertically. The classic one.

    align-items: flex-end;

    Lined up at the bottom.

    align-items: baseline;

    The first lines of text sit on one line.

    align-content (only with wrap)

    align-items moves items inside their line. align-content moves the lines themselves. It only works when flex-wrap is wrap or wrap-reverse (with nowrap the browser ignores it), and you only see it when the container has room to spare across the lines. Even a single wrapped line follows it, so flex-wrap: wrap; align-content: center centers one row in a tall container.

    flex-wrap: wrap; align-content: flex-start;

    Lines packed at the top.

    flex-wrap: wrap; align-content: center;

    Lines packed in the middle.

    flex-wrap: wrap; align-content: space-between;

    First line at the top, last at the bottom.

    gap

    gap puts space between items, never at the outer edges. One value sets both directions. Two values are row gap then column gap: gap: 20px 8px puts 20px between lines and 8px between items on a line. It’s much easier than adding margins to every item and removing the last one.

    Flexbox Cheat Sheet: Item Properties

    These go on the children. In the generator, click an item to set them.

    flex-grow: who takes the free space

    .item-1 { flex-grow: 1; }

    Item 1 takes all the space the others don’t need.

    flex-grow: 1; / flex-grow: 2;

    The free space is split 1 : 2. The free space, not the whole width, so the sizes aren’t exactly 1 : 2.

    flex-shrink: who gives up space first

    When items don’t fit, they shrink. The default is flex-shrink: 1 for everyone. Set flex-shrink: 0 on anything that must keep its size, like a logo, an avatar or a sidebar. A bigger number makes an item shrink faster than the others.

    flex-basis: the starting size

    flex-basis is the size an item starts from before growing or shrinking, along the main axis. So in a row it acts like a width, in a column like a height. auto (the default) uses the item’s width or its content. 0 means “start from nothing”, which is why items with flex-grow: 1; flex-basis: 0 end up the same size (as long as none of them holds a word too long to fit).

    The flex shorthand

    flex sets grow, shrink and basis in one go. These are the values you’ll see most:

    You writeIt means (grow shrink basis)Use it for
    flex: 1 1 1 0% Grow and shrink, start from zero. Items with flex: 1 end up the same size, as long as none of them holds a word too long to fit (add min-width: 0 for that). Use it for equal columns.
    flex: auto 1 1 auto Grow and shrink, but start from the content’s size, so bigger content gets a bigger share.
    flex: none 0 0 auto Don’t grow, don’t shrink. The item keeps its size, like a fixed sidebar or an icon.
    flex: 0 0 200px 0 0 200px 200px, unless its content can’t fit in that (add min-width: 0 to stop it growing). Same as flex-basis: 200px with no grow and no shrink.
    flex: initial 0 1 auto What every item starts with: no growing, shrinking only when it has to.

    💡 The generator writes the long versions (flex-grow, flex-basis…) so you can see what each one does. Swap them for the shorthand any time: flex-grow: 1; flex-basis: 0 does the same job as flex: 1 in a row (flex: 1 uses 0%, and the generator keeps that when you paste it).

    order and align-self

    .item-2 { order: -1; }

    Items are sorted by order (default 0), so -1 jumps to the front. Only the look changes, not the HTML.

    .item-2 { align-self: flex-end; }

    Overrides align-items for one item.

    Common Flexbox Layouts

    Center a div (both ways)

    .container {
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
    }

    Three lines on the parent. The container needs a height (here the full screen) or there’s no room to center in vertically. More ways to center text are in our how to center text in HTML tutorial.

    A navbar: logo left, links right

    nav { display: flex; align-items: center; gap: 12px; }
    .logo { margin-right: auto; }

    margin-right: auto on the logo eats all the free space and pushes the links to the right. The generator’s Navbar preset does the same job with flex-grow: 1 on the logo.

    Equal columns

    .container { display: flex; gap: 10px; }
    .item { flex: 1; }

    flex: 1 on every column makes them the same width, however much text each one has, as long as none of them holds a word too long to fit (add min-width: 0 for that). Add flex-wrap: wrap and a flex-basis like 220px to let them stack on phones.

    A footer that stays at the bottom

    body {
      display: flex;
      flex-direction: column;
      min-height: 100vh;
    }
    main { flex-grow: 1; }

    On a short page the main content grows to fill the screen, so the footer sits at the bottom instead of floating in the middle. On a long page it just follows the content.

    Flexbox or Grid?

    Use flexbox when things flow in one direction and their size comes from their content: a row of buttons, a navbar, a list of tags, a card’s header and footer. Use CSS grid when you want rows and columns that line up with each other, like a photo gallery, a dashboard or a whole page layout. They work well together: a grid for the page, flexbox inside each part. Try the CSS Grid Generator for the two-way layouts.

    Common Mistakes

    Putting display: flex on the wrong element

    display: flex goes on the parent, and it only affects its direct children. If justify-content seems to do nothing, check that it’s on the same element as display: flex, and that the boxes you want to move are its children, not grandchildren.

    Mixing up align-items and align-content

    align-items lines up items inside each line. align-content moves whole lines, so it does nothing without flex-wrap: wrap. If you want to center one row of items vertically, you want align-items.

    Long text that won’t shrink

    A flex item won’t get narrower than its longest word or a long URL, because its min-width is auto. If text pushes your layout off the screen, add min-width: 0 to the item, then overflow-wrap: anywhere or text-overflow: ellipsis.

    Centering a column with no height

    In a column, justify-content: center works vertically, but the container is only as tall as its items unless you give it a height or min-height. No extra room means nothing to center in.

    Using order to fix the HTML

    order and row-reverse only change what people see. Screen readers and keyboard users still go through the HTML order, so put things in a sensible order in the HTML first.

    What This Generator Can’t Do

    Next steps: round the corners of your boxes with the CSS Border Radius Generator, add depth with the CSS Box Shadow Generator, or style the buttons in your flex row with the HTML Button Generator.

    Frequently Asked Questions

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

    justify-content places items along the main axis, the direction they flow in. align-items places them across it. In a normal row, justify-content moves items left and right and align-items moves them up and down. Switch to flex-direction: column and the two swap, so justify-content now works vertically.

    How do I center a div with flexbox?

    Put three lines on the parent: display: flex; justify-content: center; align-items: center. The parent needs some height for the vertical part to show, for example min-height: 100vh for the whole screen or a fixed height like 300px. Click the Center everything preset in the generator to see it.

    Why does align-content do nothing?

    align-content moves whole lines of items, and the browser ignores it unless flex-wrap is wrap or wrap-reverse. Even a single wrapped line follows it. You also need spare room across the lines to see anything move, so in a row give the container a height. To line up items inside their line, use align-items instead.

    What does flex: 1 mean?

    It’s short for flex-grow: 1, flex-shrink: 1 and flex-basis: 0%. The item starts from zero width and takes an equal share of the free space, so several items with flex: 1 end up the same size. The one exception is content that can’t shrink, like a long URL or a wide image: flex items won’t get narrower than that unless you add min-width: 0. It’s the usual way to make equal columns.

    What is the difference between flex-basis and width?

    In a row they’re almost the same: flex-basis is the size an item starts from before it grows or shrinks, and when both are set, flex-basis wins. The difference is that flex-basis follows the main axis, so in a column it acts like a height. flex-basis: auto (the default) means “use the width or height if there is one, otherwise the content’s size”.

    How do I push one item to the right?

    Give it margin-left: auto. The auto margin takes all the free space on that side, so the item and anything after it move to the right edge. Another way is flex-grow: 1 on the item before it, which is what the Navbar preset in the generator does.

    Why does my flex item overflow instead of shrinking?

    Flex items have min-width: auto by default, so they won’t get narrower than their longest word, a long URL or a wide image. Add min-width: 0 to the item (or overflow: hidden), then let the text break with overflow-wrap: anywhere or cut it with text-overflow: ellipsis. In a column the same thing happens with min-height.

    Should I use flexbox or CSS grid?

    Use flexbox for one direction, a row or a column, where the content decides the sizes: navbars, button rows, toolbars, tags. Use grid when rows and columns need to line up with each other, like a gallery or a page layout. They mix well, for example a grid page with flexbox inside each card.

    Does gap work with flexbox in all browsers?

    Yes. gap on flex containers works in all current versions of Chrome, Edge, Firefox and Safari (Safari added it in version 14.1, in 2021). You only need margins instead if you have to support much older browsers.