Toolpuffin

    Loading...

    Up and down to choose, Enter to open, Esc to close.

    How to Learn CSS Flexbox and Grid: When to Use Which

    Updated December 2026

    Written by the Toolpuffin team. Tools in this guide are tested in the browser.

    Flexbox and Grid are the two layout tools of modern CSS. They solve different problems, and beginners often wonder which one to use. This guide explains each in plain words, shows the properties that matter most and gives a simple rule for choosing. The facts come from MDN Web Docs. The free course practises both with a live preview next to your code.

    Try the tool: Learn CSS Flexbox and Grid

    Flexbox: one direction at a time

    MDN describes flexbox as a one-dimensional layout model for distributing space between items, with alignment capabilities. One-dimensional means that it deals with layout in one dimension at a time, either as a row or as a column. You turn an element into a flex container with display: flex and its children become flex items.

    Flexbox works with two axes: the main axis, which runs in the direction of flex-direction, and the cross axis, which is perpendicular to it. Two properties follow from that:

    • justify-content distributes the items along the main axis (for example space-between or center).
    • align-items aligns the items along the cross axis (for example center).

    MDN also lists wrapping content onto new rows and columns and flexible sizing of the items among the things flexbox does. The property flex-wrap: wrap lets items move to a new line, and flex: 1 lets an item grow to fill the free space.

    Grid: rows and columns together

    CSS grid layout is a two-dimensional layout system: it lets you organise content into rows and columns. A grid has columns, rows and gaps between them. With display: grid you get a grid with one column, so the items still appear under each other until you define columns with grid-template-columns.

    • The fr unit is a fraction of the available space. repeat(3, 1fr) is the same as 1fr 1fr 1fr: three equal columns.
    • The gap property sets the space between rows and columns.
    • grid-column: span 2 makes an item take two columns.
    • repeat(auto-fit, minmax(200px, 1fr)) lets the browser create as many columns as fit, each at least 200 pixels wide.

    MDN shows the last pattern as the way to get a flexible number of tracks in grid, comparable to items that wrap in flexbox.

    Which one should you use?

    The author of MDN's layout guides writes that a lot of the difficulty in using flexbox is in trying to use it for the two-dimensional layouts that grid is designed for. The advice is to learn the basics of each specification and the reasons to use each method, instead of learning one first and the other later. On a production site, some patterns make sense in flexbox and some in grid.

    • A row or column of items, such as a menu, a toolbar or a row of buttons: flexbox.
    • Items that must line up in both rows and columns, such as a photo gallery or a page layout: grid.
    • A grid with a flexbox inside a cell is perfectly normal.

    The two lists are a rule of thumb that follows from the one-dimensional and two-dimensional descriptions, not an official rule.

    Practise with a live preview

    The free course has nine short lessons: a flex row, gap and direction, justify-content, align-items, wrapping, one box that takes the rest, grid columns, an item over two columns and a grid that adapts to the screen. The preview next to the editor shows the real result in your browser. The check reads your CSS with simple rules, so a correct layout written in an unexpected way can be marked as not yet right. The hint shows the expected form.

    Sources

    More guides