Saved

Press the star next to a tool's name to save it.

All my tools

CSS Flexbox Playground

This CSS flexbox playground shows a live preview as you change flex-direction, wrapping, justify-content, align-items, align-content and gap, and writes the CSS and HTML to copy.

Change any setting and the preview and code update straight away. Items have different sizes so you can see the alignment.

Preview

A CSS flexbox playground lets you try every main flexbox setting and watch the layout change as you go. Pick the direction, wrapping, main-axis and cross-axis alignment, the gap and the number of items, and this CSS flexbox playground redraws the boxes instantly and writes the CSS and HTML for you to copy. It's a quick way to learn how flexbox behaves, or to get the exact code for a row of buttons, a navigation bar, a card layout or a centered box.

What flexbox is

Flexbox is a CSS layout method for laying out a set of elements in one dimension, as a row or as a column. You turn it on by giving a parent element display: flex. The parent becomes the flex container and its direct children become flex items, which line up along the container's main axis.

Two axes matter. The main axis runs in the direction the items flow: left to right for a row, top to bottom for a column. The cross axis runs at right angles to it. Almost every flexbox property works along one of those two axes, which is why changing the direction also changes what the alignment settings do.

The settings in this CSS flexbox playground

  • flex-direction sets the main axis: row (the default), row-reverse, column or column-reverse.
  • flex-wrap decides whether items stay on one line (nowrap, the default) or wrap onto new lines when there isn't room.
  • justify-content spaces items along the main axis: at the start, center or end, or with the space between, around or evenly shared.
  • align-items places items on the cross axis. The default behaves as stretch, which stretches items to fill the container in that direction; center centers them.
  • align-content spaces whole lines on the cross axis, so it only appears when wrapping is on.
  • gap adds space between items without margins.
  • flex: 1 on the items makes them grow and share the free space.

Centering something with flexbox

The classic use is centering a box both ways. MDN's flexbox guide does it with two properties on the container: align-items: center centers the items on the cross axis, and justify-content spaces them along the main axis. Set justify-content to center and align-items to center here, and the preview shows the result. The CSS is:

.flex-container { display: flex; justify-content: center; align-items: center; }

Worked example: a navigation bar

Say you want a row of five links spread across a header, with equal space between them and all of them centered vertically. Keep the direction as row, set justify-content to space-between, align-items to center and the gap to 12. The playground writes:

.flex-container { display: flex; justify-content: space-between; align-items: center; gap: 12px; }

Switch the direction to column and the same justify-content now spaces the items from top to bottom, because the main axis has turned. That's the most common surprise when people start with flexbox.

Growing and shrinking items

By default each item is only as wide as its content. The flex shorthand controls how items share leftover space: its first value is the flex-grow proportion, its second is flex-shrink, which matters when items overflow the container, and its third is the starting size. Tick the grow option and the playground adds flex: 1 to every item, so they all stretch equally to fill the row.

Wrapping onto new lines

With flex-wrap left at nowrap, items squeeze onto one line however many there are. Set it to wrap and add more items, and they move onto a second line when the first is full. The flex-flow shorthand combines flex-direction and flex-wrap into one property if you'd rather write less CSS. Once lines wrap, align-content controls how the lines themselves sit inside the container.

Using the code

The CSS leaves out any setting that's at its default, so it stays short and only shows what you changed. Copy the CSS into your stylesheet and the HTML into your page, and rename the class if you like. The playground uses plain divs; your items can be links, buttons, images or cards.

How to use the CSS flexbox playground

  1. Pick a direction to set the main axis.
  2. Choose wrapping if you want items to move onto new lines.
  3. Set justify-content and align-items and watch the boxes move in the preview.
  4. Change the number of items and the gap to match your design.
  5. Tick the grow option if items should fill the row.
  6. Copy the CSS and HTML into your project.

Press Reset at any time to go back to the browser defaults and start again.

Flexbox or grid?

Flexbox works in one dimension at a time, so it's best for rows or columns of things, like toolbars, menus, button groups and the inside of a card. When you need rows and columns lined up together, CSS grid is usually simpler; this site's CSS grid generator works the same way as this playground.

Common mistakes

  • Putting display: flex on the items. It goes on the parent container.
  • Mixing up the axes. In a column, justify-content works vertically.
  • Expecting align-content to work on one line. It needs wrapping turned on.
  • Using margins for spacing. The gap property is simpler.

What next

Use this often? Add CSS Flexbox to your Home screen.

Frequently asked questions

How does the CSS flexbox playground work?

You choose the container's flexbox settings and the number of items, and it redraws a live preview and writes the matching CSS and HTML.

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

justify-content spaces items along the main axis, the direction they flow. align-items places them on the cross axis, at right angles to it.

How do I center a div with flexbox?

Give the parent display: flex, justify-content: center and align-items: center.

When does align-content do anything?

Only when items wrap onto more than one line. It spaces the lines on the cross axis.

What does flex: 1 mean?

It sets flex-grow to 1, so the item grows to share the container's free space equally with other items set the same way.

Sources

  1. MDN: Flexbox

What went wrong?

We only receive the reason you pick, never what you typed into the tool.

Thanks for your feedback.

More Developer Tools