intermediate css 18 min read

CSS Grid: The Ultimate Layout System

Learn CSS Grid for creating complex, two-dimensional layouts with precision.

css grid grid layout css grid tutorial grid template

What is CSS Grid?

CSS Grid is a two-dimensional layout system that handles both columns and rows. It's perfect for page layouts and complex component designs.

Creating a Grid Container

.container {
    display: grid;
}

Defining Columns and Rows

.container {
    display: grid;
    grid-template-columns: 200px 1fr 1fr;
    grid-template-rows: 100px auto 100px;
}

The fr Unit

.container {
    /* Three equal columns */
    grid-template-columns: 1fr 1fr 1fr;

    /* Shorthand */
    grid-template-columns: repeat(3, 1fr);
}

Gap

.container {
    gap: 20px;           /* All gaps */
    row-gap: 20px;       /* Between rows */
    column-gap: 10px;    /* Between columns */
}

Placing Items

Line-Based Placement

.item {
    grid-column-start: 1;
    grid-column-end: 3;
    grid-row-start: 1;
    grid-row-end: 2;

    /* Shorthand */
    grid-column: 1 / 3;
    grid-row: 1 / 2;

    /* Even shorter */
    grid-area: 1 / 1 / 2 / 3;
}

Span Keyword

.item {
    grid-column: span 2;  /* Span 2 columns */
    grid-row: span 3;     /* Span 3 rows */
}

Grid Template Areas

.container {
    display: grid;
    grid-template-columns: 200px 1fr;
    grid-template-rows: auto 1fr auto;
    grid-template-areas:
        "header header"
        "sidebar content"
        "footer footer";
}

.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }
.footer { grid-area: footer; }

Alignment

Justify Items (Horizontal)

.container {
    justify-items: start;   /* Align to start */
    justify-items: end;     /* Align to end */
    justify-items: center;  /* Center items */
    justify-items: stretch; /* Default: fill cell */
}

Align Items (Vertical)

.container {
    align-items: start;
    align-items: end;
    align-items: center;
    align-items: stretch;
}

Place Items Shorthand

.container {
    place-items: center;  /* Center both ways */
}

Auto-Fit and Auto-Fill

/* Responsive grid without media queries */
.container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
}

Complete Layout Example

.page-layout {
    display: grid;
    grid-template-columns: 250px 1fr;
    grid-template-rows: 60px 1fr 40px;
    grid-template-areas:
        "header header"
        "sidebar main"
        "footer footer";
    min-height: 100vh;
}

@media (max-width: 768px) {
    .page-layout {
        grid-template-columns: 1fr;
        grid-template-areas:
            "header"
            "main"
            "footer";
    }

    .sidebar {
        display: none;
    }
}

Grid vs Flexbox

Feature Grid Flexbox
Dimensions 2D (rows & columns) 1D (row OR column)
Best for Page layouts Component layouts
Content-first No Yes
Layout-first Yes No

Use Grid for overall page structure, Flexbox for component-level alignment.

Best Practices

  1. Use named grid areas for complex layouts
  2. Combine with Flexbox for component-level layout
  3. Use minmax() for responsive columns
  4. Prefer auto-fit for responsive grids

Try our CSS Grid Generator to visually build grid layouts.

Related Tools

Continue Learning