CSS Flexbox & Grid Playground

Flexbox and CSS grid playground: change direction, alignment, wrap, columns, rows and gap on live boxes, then copy the container CSS.

Runs in your browser. Files never leave your device.
Layout Engine
Items (4)

Live Workspace (FLEX)

display: flex
Item 1
Item 2
Item 3
Item 4
Generated CSS
.container {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: stretch;
  flex-wrap: nowrap;
  gap: 16px;
}

About the CSS Flexbox & Grid Playground

Change one CSS property at a time and watch the boxes move. The playground lays out between 1 and 12 numbered items in a live container, using flexbox or CSS grid, so you can see what justify-content, align-items, flex-wrap or grid-template-columns actually does before you write it into a stylesheet. If you've ever toggled values in DevTools until something lined up, it's the same idea with every option in front of you.

Switch between Flexbox and CSS Grid at the top and pick values from the drop-down lists. The matching .container rule appears underneath, ready to copy. Reset Layout returns to four items in a left-aligned flex row.

Flexbox or grid: what the controls set

Flexbox arranges items along one line. flex-direction picks that line (row runs left to right, column top to bottom, and the -reverse options flip the order). justify-content spreads items along the line: packed at the start, centred, at the end, or with the free space shared out between them. align-items positions them across the line, and stretch, the default, makes every item as tall as the container in a row. flex-wrap decides whether items squeeze onto one line or break onto new ones when they run out of room.

Grid places items into rows and columns at the same time. The column list gives one to four equal columns written as repeat(n, 1fr), where 1fr is one share of the free width. Rows can be auto, sized by their content, or two or three equal rows. Items fill the cells left to right, top to bottom, and any extra items start new rows automatically.

The gap setting, from 0 to 32px, works in both modes. It adds space between items but not around the outside edge. The generated rule only includes the properties shown for the current mode, so the copied flex rule has no grid lines in it and the grid rule has no flex lines.

Assumptions and limits

  • The playground covers container properties only. Item properties such as flex-grow, flex-basis, order or grid-column are not included; add them to the child elements yourself.
  • Grid columns are always equal-width fr tracks. For a fixed sidebar or a responsive grid, edit the copied value, for example 240px 1fr or repeat(auto-fit, minmax(200px, 1fr)).
  • In grid mode there are no alignment controls; justify-items, align-items and place-content are left at their defaults.
  • The preview items have a minimum size and padding so they stay visible, so a real page with different content can wrap or stretch at a different point. When items don't fit, for example many items with nowrap on a phone, the preview box scrolls sideways so you can see the overflow.
  • The copied rule is named .container; rename the class to match your HTML.

Worked examples

Centre content in a flex container

Set justify-content and align-items to center. With one item in the playground, that item sits in the middle of the box both ways, the classic way to centre a login card or an empty-state message.

.container {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  flex-wrap: nowrap;
  gap: 16px;
}

A navigation bar with the logo on the left and links on the right

Keep flex-direction on row, choose space-between and align-items center. The first item is pushed to the left edge, the last to the right edge, and the space is shared between the rest, which suits a header with a logo, a menu and a button.

A three-column card grid with 24px gaps

Switch to CSS Grid, keep 3 Columns, set rows to auto and gap to 24px, then add items up to 9. The cards line up in three rows of three, each column exactly the same width.

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: auto;
  gap: 24px;
}

justify-content values and what they do in a row

ValueWhere the items goTypical use
flex-startPacked at the start of the lineToolbars, tag lists
centerPacked together in the middleCentred buttons, hero content
flex-endPacked at the end of the lineForm action buttons on the right
space-betweenFirst and last at the edges, equal space between the restHeaders and navigation bars
space-aroundEqual space on both sides of each item, so the edges get halfIcon rows
space-evenlyExactly equal gaps, including at both edgesEvenly spread stats or logos

How to use the CSS Flexbox & Grid Playground

  1. Choose Flexbox or CSS Grid. Use the Layout Engine switch, then Add or Remove to set how many items, from 1 to 12, are in the box.
  2. Change the properties. Pick values from the flex-direction, justify-content, align-items, flex-wrap, grid column, grid row and gap lists and watch the Live Workspace.
  3. Take the .container rule. When the boxes sit the way you want, use Copy CSS under Generated CSS, add the rule to your stylesheet and rename the class to match your HTML.

Frequently asked questions

When should I use flexbox instead of CSS grid?

Use flexbox when items flow in a single row or column and their size should come from their content, such as a menu or a row of buttons. Use grid when you want items to line up in both rows and columns, such as a card gallery or a page layout.

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

justify-content works along the main direction set by flex-direction, so in a row it moves items left and right. align-items works across it, so in a row it moves items up and down. Switching to column swaps which one is horizontal.

Why don't my flex items wrap onto a new line?

flex-wrap is nowrap by default, so items shrink to fit on one line or overflow it. Set flex-wrap to wrap, then keep adding items in the playground until they run out of space: they move onto a second line.

What does 1fr mean in grid-template-columns?

fr is a fraction of the free space in the grid. repeat(3, 1fr) makes three columns that each get one equal share of the width left over after gaps, so they stay equal as the container grows or shrinks.

Is gap supported for flexbox in all browsers?

Yes, gap works with flex containers in all current major browsers, so you no longer need margins on each item to space them out. Very old browsers ignore it, and the items then sit side by side with no space between them.

This tool runs entirely in your browser. What you enter or upload stays on your device and is never sent to our servers.