Usage of CSS grid-auto-flow property

The CSS grid-auto-flow property controls how auto-placed items are inserted in the grid container. It determines whether new items are placed in rows or columns and the direction of placement.

Syntax

selector {
    grid-auto-flow: value;
}

Possible Values

Value Description
row Auto-placed items fill rows first (default)
column Auto-placed items fill columns first
row dense Fill rows and pack items densely
column dense Fill columns and pack items densely

Example 1: Column Flow

The following example uses grid-auto-flow: column to place items in columns −

<!DOCTYPE html>
<html>
<head>
<style>
    .container {
        display: grid;
        grid-auto-columns: 80px;
        grid-auto-flow: column;
        gap: 10px;
        background-color: #f0f0f0;
        padding: 15px;
        width: fit-content;
    }
    .container > div {
        background-color: #4CAF50;
        color: white;
        text-align: center;
        padding: 15px;
        font-size: 18px;
        border-radius: 5px;
    }
</style>
</head>
<body>
    <div class="container">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
    </div>
</body>
</html>
Six green boxes numbered 1-6 arranged horizontally in columns with equal spacing between them.

Example 2: Row Flow (Default)

This example shows the default grid-auto-flow: row behavior −

<!DOCTYPE html>
<html>
<head>
<style>
    .container {
        display: grid;
        grid-template-columns: repeat(3, 80px);
        grid-auto-flow: row;
        gap: 10px;
        background-color: #f0f0f0;
        padding: 15px;
    }
    .container > div {
        background-color: #2196F3;
        color: white;
        text-align: center;
        padding: 15px;
        font-size: 18px;
        border-radius: 5px;
    }
</style>
</head>
<body>
    <div class="container">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
    </div>
</body>
</html>
Six blue boxes arranged in a 3x2 grid pattern, filling rows from left to right, then moving to the next row.

Conclusion

The grid-auto-flow property provides control over how CSS Grid automatically places items. Use column for horizontal layouts and row for traditional grid patterns.

Updated on: 2026-03-15T13:31:10+05:30

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements