CSS Grid & Flexbox Layout Generator

Layout Mode

Container (Parent)

Selected Item (Child)

Click an item in the preview to select it.

Manage Items

Parent CSS

Child CSS

Using the Layout Generator

This tool provides a visual way to configure and generate code for both CSS Grid and CSS Flexbox layouts. It helps you understand how the different properties affect the container and its children.

  1. Choose a Layout Mode. Start by selecting either Grid or Flexbox. The available controls will change based on your selection.
  2. Configure the Container. Use the controls in the "Container (Parent)" section to define the main layout properties. For Grid, this includes columns, rows, and gaps. For Flexbox, this includes direction, wrap, and gap.
  3. Manage and Select Items. Use the "Add Item" and "Remove Selected" buttons to change the number of children in the preview. Click on any child item in the preview to select it for individual styling.
  4. Style Individual Items. Once an item is selected, use the controls in the "Selected Item (Child)" section to apply specific properties like grid-column or flex-grow.
  5. Copy the CSS. Two code blocks are generated in real-time. The "Parent CSS" contains the code for the container, and the "Child CSS" contains specific rules for the currently selected child.

Understanding Flexbox

CSS Flexbox is a one-dimensional layout model. It excels at distributing space along a single axis, either horizontally or vertically. Think of it as arranging items in a single row or a single column.

PropertyDescription
display: flex;Initializes a flex container.
flex-directionSets the main axis of the container (row, column, etc.).
justify-contentAligns items along the main axis.
align-itemsAligns items along the cross axis.
flex-wrapControls whether items wrap to a new line.
gapSets the space between items.

Understanding Grid

CSS Grid is a two-dimensional layout model. It is designed for arranging items in both rows and columns simultaneously, making it ideal for creating complex, structured page layouts.

PropertyDescription
display: grid;Initializes a grid container.
grid-template-columnsDefines the number and size of columns (e.g., 1fr 1fr 2fr).
grid-template-rowsDefines the number and size of rows.
justify-contentAligns the entire grid along the row axis.
align-itemsAligns items within their grid cells along the column axis.
gapSets the space between grid rows and columns.

When to Use Flexbox vs Grid

A common question is when to choose one model over the other. A simple guideline is to consider the dimensions of your layout.

  • Use Flexbox for arranging items in one dimension. This is ideal for component-level layouts like navigation bars, button groups, or aligning items within a card.
  • Use Grid for arranging items in two dimensions (rows and columns). This is perfect for overall page layouts, complex dashboards, and any design where you need precise control over both horizontal and vertical alignment.

It is important to note that they are not mutually exclusive. It is very common and powerful to use Grid for the main page structure and Flexbox to arrange the content inside individual grid items.