How Do grid-template-columns, Rows and Areas Work in CSS?

Learn How Do grid-template-columns, Rows and Areas Work in CSS? with practical examples, explanations, best practices, and real-world coding techniques.

Try Editor

What Are CSS Grid Templates?

CSS Grid templates define the structure of a grid by specifying its columns, rows, and named layout areas. They are some of the most important features of CSS Grid because they let you describe how available space should be organized before placing content inside it.

The main properties are grid-template-columns, grid-template-rows, and grid-template-areas. CSS also provides the grid-template shorthand for defining the explicit grid using a more compact syntax.

Grid templates are especially useful for dashboards, application shells, card layouts, admin panels, galleries, and page structures where the relationship between rows and columns matters.

What Parts Make Up a CSS Grid Template?

Columns

Defined with grid-template-columns.

Rows

Defined with grid-template-rows.

Areas

Named with grid-template-areas.

Shorthand

Combined using grid-template.

How Does grid-template-columns Work?

The grid-template-columns property defines the columns in the explicit grid. Each value represents the sizing of one grid track.

</pre>

        <p class="mt-5">
          This creates three columns. The first and third have fixed sizes, while the middle
          column uses the remaining flexible space.
        </p>

        <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mt-6">
          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <div class="font-mono text-blue-300">200px</div>
            <p class="text-sm text-slate-400 mt-2">
              Fixed first track.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <div class="font-mono text-blue-300">1fr</div>
            <p class="text-sm text-slate-400 mt-2">
              Flexible middle track.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <div class="font-mono text-blue-300">250px</div>
            <p class="text-sm text-slate-400 mt-2">
              Fixed third track.
            </p>
          </div>
        </div>

        <p class="mt-5">
          You can also mix flexible units, percentages, lengths, intrinsic sizing functions,
          named lines, and functions such as <code class="text-blue-300">minmax()</code> and
          <code class="text-blue-300">repeat()</code>.
        </p>
      </section>

      <!-- Equal Columns -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          How Do You Create Equal Columns with CSS Grid?
        </h2>

        <p class="mb-4">
          One of the simplest Grid patterns is creating equal-width columns using the
          <code class="text-blue-300">fr</code> unit.
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.cards {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 1rem;
}

All three columns receive an equal flexible share of the available space. For repeated patterns, you can make the CSS shorter using repeat().

</pre>
      </section>

      <!-- Rows -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          How Does grid-template-rows Work?
        </h2>

        <p class="mb-4">
          The <code class="text-blue-300">grid-template-rows</code> property defines explicit
          row tracks in the grid.
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.page {
  display: grid;
  grid-template-rows: 72px 1fr 56px;
}

This type of definition works well for layouts with a fixed-height header, a flexible content area, and a footer.

Header
Main Content
Footer

How Do You Define Both Grid Rows and Columns?

You can define both dimensions on the same grid container:

</pre>

        <p class="mt-5">
          This creates a two-column, two-row structure. The first column can represent a sidebar,
          while the second contains the main application area.
        </p>

        <div class="rounded-xl border border-slate-700 bg-slate-900 p-5 mt-6">
          <div class="grid grid-cols-[120px_1fr] grid-rows-[60px_140px] gap-2">
            <div class="row-span-2 rounded-lg border border-slate-600 p-3 text-center text-sm">
              Sidebar
            </div>

            <div class="rounded-lg border border-slate-600 p-3 text-center text-sm">
              Header
            </div>

            <div class="rounded-lg border border-slate-600 p-3 text-center text-sm">
              Main
            </div>
          </div>
        </div>
      </section>

      <!-- Repeat -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          How Does repeat() Work in Grid Templates?
        </h2>

        <p class="mb-4">
          The <code class="text-blue-300">repeat()</code> function lets you write repeated
          grid tracks more efficiently.
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
}

Instead of manually writing four equal tracks, you can express the repeated pattern using repeat(4, 1fr).

The same concept can be used for rows:

</pre>

        <p class="mt-5">
          Responsive layouts can also use <code class="text-blue-300">auto-fit</code> or
          <code class="text-blue-300">auto-fill</code>.
        </p>

        <pre class="mt-4 bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.cards {
  grid-template-columns:
    repeat(auto-fit, minmax(240px, 1fr));
}

How Does minmax() Work with Grid Templates?

The minmax() function lets you provide a minimum and maximum size for a grid track.

</pre>

        <p class="mt-5">
          Each track has a minimum of <code class="text-blue-300">200px</code> and can grow
          up to a flexible share of the available space.
        </p>

        <p class="mt-4">
          This combination becomes particularly useful in responsive card layouts:
        </p>

        <pre class="mt-4 bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.cards {
  display: grid;
  grid-template-columns:
    repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
}

Can You Name Grid Lines?

Yes. Grid templates can assign names to grid lines. Named lines can make explicit placement easier to understand in larger layouts.

</pre>

        <p class="mt-5">
          An item can then reference those names:
        </p>

        <pre class="mt-4 bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.main {
  grid-column: content-start / content-end;
}

Named lines can be useful in complex layouts, but simple numeric placement or named areas may be easier to maintain when the layout does not need this level of control.

How Does grid-template-areas Work?

The grid-template-areas property lets you describe the layout using named regions that visually resemble the grid itself.

</pre>

        <p class="mt-5">
          You then assign elements to those areas using
          <code class="text-blue-300">grid-area</code>.
        </p>

        <pre class="mt-4 bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.header {
  grid-area: header;
}

.sidebar {
  grid-area: sidebar;
}

.main {
  grid-area: main;
}

.footer {
  grid-area: footer;
}

One major advantage is that the CSS itself visually communicates the page structure. This can make larger application layouts much easier to understand.

How Do You Leave an Empty Grid Cell?

A period character . represents an empty cell inside grid-template-areas.

</pre>

        <p class="mt-5">
          Empty cells can be useful when the design intentionally leaves part of the grid unused.
        </p>
      </section>

      <!-- Rectangular Areas -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          What Rules Should You Follow When Using Grid Areas?
        </h2>

        <p class="mb-4">
          The strings in <code class="text-blue-300">grid-template-areas</code> must describe a
          valid rectangular arrangement. Each row must contain the same number of cells, and a
          named area must form a rectangle rather than an arbitrary shape.
        </p>

        <div class="grid md:grid-cols-2 gap-5 mt-6">

          <div class="rounded-xl border border-emerald-900/50 bg-emerald-950/20 p-5">
            <h3 class="font-semibold text-white mb-3">Valid Shape</h3>

            <pre class="bg-slate-950 border border-slate-700 rounded-xl p-4 overflow-x-auto text-sm"><code>"header header"
"main main"
"footer footer"

Invalid Shape

"header header"
"main header"
"main main"

How Can Grid Templates Create Responsive Layouts?

Grid templates become especially powerful when combined with flexible track sizing and media queries. Instead of relying entirely on fixed dimensions, you can allow the structure to change according to available space.

</pre>

        <p class="mt-5">
          For a page layout, you might also switch the number of columns at smaller widths:
        </p>

        <pre class="mt-4 bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.layout {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
}

@media (max-width: 768px) {
  .layout {
    grid-template-columns: 1fr;
  }
}

This approach makes the responsive behavior explicit when the layout structure itself needs to change.

Can grid-template-areas Change at Different Screen Sizes?

Yes. Named areas are particularly convenient when a page needs a different structural arrangement on smaller screens.

</pre>

        <p class="mt-5">
          You can also rearrange the named areas instead of hiding content when the design calls
          for a different mobile structure.
        </p>
      </section>

      <!-- Grid Template Shorthand -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          What Is the grid-template Shorthand?
        </h2>

        <p class="mb-4">
          The <code class="text-blue-300">grid-template</code> property is a shorthand for the
          explicit grid template properties:
          <code class="text-blue-300">grid-template-rows</code>,
          <code class="text-blue-300">grid-template-columns</code>, and
          <code class="text-blue-300">grid-template-areas</code>.
        </p>

        <p class="mb-4">
          For example, a layout using separate properties can be written as:
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.page {
  display: grid;
  grid-template-rows: 70px 1fr;
  grid-template-columns: 240px 1fr;
  grid-template-areas:
    "header header"
    "sidebar main";
}

The same explicit structure can be expressed using the shorthand:

</pre>

        <p class="mt-5">
          The shorthand can be powerful, but separate properties are often easier for beginners
          and can make individual changes clearer in larger stylesheets.
        </p>
      </section>

      <!-- Practical Dashboard -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          How Do You Build a Dashboard Using Grid Templates?
        </h2>

        <p class="mb-5">
          A dashboard is a practical example because the page structure often has clear named
          regions.
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code><div class="dashboard">

  <header class="header">
    Dashboard
  </header>

  <aside class="sidebar">
    Navigation
  </aside>

  <main class="main">
    Dashboard Content
  </main>

</div>
</pre>

        <p class="mt-5">
          This approach keeps the page structure centralized in the grid container while each
          semantic element identifies the region it belongs to.
        </p>
      </section>

      <!-- Card Templates -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          How Do You Create a Responsive Card Grid with Templates?
        </h2>

        <p class="mb-4">
          For repeated content, a flexible track definition often works better than manually
          specifying every column.
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.features {
  display: grid;
  grid-template-columns:
    repeat(auto-fit, minmax(220px, 1fr));
  gap: 1.25rem;
}

.feature {
  min-width: 0;
  padding: 1.5rem;
}

This pattern allows the browser to determine how many columns fit while respecting a minimum track size and distributing extra space between the generated tracks.

Can Grid Templates Use subgrid?

Modern CSS Grid also provides the subgrid value for grid-template-columns or grid-template-rows. A subgrid allows a nested grid to adopt the relevant tracks from its parent grid.

</pre>

        <p class="mt-5">
          Subgrid can be useful when nested components need to align their internal content with
          the tracks established by a parent grid.
        </p>
      </section>

      <!-- Common Mistakes -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          What Are Common CSS Grid Template Mistakes?
        </h2>

        <div class="space-y-5">

          <div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
            <h3 class="text-lg font-semibold text-white mb-2">
              Mixing Up Columns and Rows
            </h3>

            <p>
              <code class="text-blue-300">grid-template-columns</code> controls columns,
              while <code class="text-blue-300">grid-template-rows</code> controls rows.
              When debugging a layout, check that the property is applied to the dimension
              you are actually trying to change.
            </p>
          </div>

          <div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
            <h3 class="text-lg font-semibold text-white mb-2">
              Using Too Many Fixed Tracks
            </h3>

            <p>
              Large collections of rigid pixel widths can create overflow or make responsive
              layouts difficult to maintain. Flexible units and functions are often better.
            </p>
          </div>

          <div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
            <h3 class="text-lg font-semibold text-white mb-2">
              Invalid grid-template-areas
            </h3>

            <p>
              Every row in a <code class="text-blue-300">grid-template-areas</code> definition
              needs the same number of cells, and named regions must form valid rectangles.
            </p>
          </div>

          <div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
            <h3 class="text-lg font-semibold text-white mb-2">
              Forgetting Content Constraints
            </h3>

            <p>
              A flexible track can still interact with the minimum size requirements of its content.
              Patterns such as <code class="text-blue-300">minmax(0, 1fr)</code> are often useful
              when a flexible region must be allowed to shrink.
            </p>
          </div>

          <div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
            <h3 class="text-lg font-semibold text-white mb-2">
              Making the Template Too Complicated
            </h3>

            <p>
              Complex line names and shorthand syntax are powerful, but they should improve
              maintainability rather than make the layout harder to understand.
            </p>
          </div>

        </div>
      </section>

      <!-- Best Practices -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          What Are the Best Practices for CSS Grid Templates?
        </h2>

        <div class="grid md:grid-cols-2 gap-5">

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Start with the layout structure
            </h3>
            <p>
              Decide the important rows, columns, and regions before writing complicated placement rules.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Prefer flexible tracks
            </h3>
            <p>
              Use <code class="text-blue-300">fr</code>, <code class="text-blue-300">minmax()</code>,
              and responsive repetition when the content needs to adapt.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Use named areas for page layouts
            </h3>
            <p>
              Named areas can make dashboards and application shells much easier to read.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Use repeat() for repeated tracks
            </h3>
            <p>
              Avoid manually writing the same track definition multiple times.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Keep semantic HTML
            </h3>
            <p>
              CSS Grid should control presentation while meaningful HTML describes the document.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Test realistic content
            </h3>
            <p>
              Long labels, wide tables, dynamic cards, and smaller screens often expose sizing problems.
            </p>
          </div>

        </div>
      </section>

      <!-- Quick Reference -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          CSS Grid Template Properties at a Glance
        </h2>

        <div class="overflow-x-auto rounded-xl border border-slate-700">
          <table class="w-full text-left text-sm">
            <thead class="bg-slate-800 text-white">
              <tr>
                <th class="px-4 py-3">Property</th>
                <th class="px-4 py-3">Purpose</th>
                <th class="px-4 py-3">Example</th>
              </tr>
            </thead>

            <tbody class="divide-y divide-slate-700 bg-slate-900">
              <tr>
                <td class="px-4 py-3 font-mono text-blue-300">grid-template-columns</td>
                <td class="px-4 py-3">Defines explicit columns.</td>
                <td class="px-4 py-3 font-mono">repeat(3, 1fr)</td>
              </tr>

              <tr>
                <td class="px-4 py-3 font-mono text-blue-300">grid-template-rows</td>
                <td class="px-4 py-3">Defines explicit rows.</td>
                <td class="px-4 py-3 font-mono">72px 1fr</td>
              </tr>

              <tr>
                <td class="px-4 py-3 font-mono text-blue-300">grid-template-areas</td>
                <td class="px-4 py-3">Defines named areas.</td>
                <td class="px-4 py-3 font-mono">"header header"</td>
              </tr>

              <tr>
                <td class="px-4 py-3 font-mono text-blue-300">grid-template</td>
                <td class="px-4 py-3">Shorthand for explicit grid templates.</td>
                <td class="px-4 py-3 font-mono">"main" / 1fr</td>
              </tr>
            </tbody>
          </table>
        </div>
      </section>

      <!-- Final Example -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          What Is a Complete CSS Grid Template Example?
        </h2>

        <p class="mb-5">
          The following example combines rows, columns, named areas, flexible tracks,
          and responsive behavior.
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code><div class="app">
  <header class="header">
    Header
  </header>

  <aside class="sidebar">
    Sidebar
  </aside>

  <main class="main">
    Main Content
  </main>

  <footer class="footer">
    Footer
  </footer>
</div>


        

This example demonstrates why Grid templates are so useful: the entire application structure can be described directly in the CSS, and the structure can be changed at smaller widths without rewriting the HTML.

Key Takeaways

  • grid-template-columns defines explicit column tracks.
  • grid-template-rows defines explicit row tracks.
  • repeat() simplifies repeated track definitions.
  • minmax() defines flexible minimum and maximum track sizes.
  • grid-template-areas lets you describe named layout regions visually.
  • A period . can represent an empty cell in a named-area template.
  • Named areas must form valid rectangular regions and each template row must have the same number of cells.
  • grid-template is the shorthand for explicit rows, columns, and areas.
  • auto-fit and auto-fill can help create responsive repeated track patterns.
  • subgrid can allow nested grids to align with parent grid tracks.
  • Grid templates are particularly useful for dashboards, application shells, structured cards, galleries, and responsive page layouts.

Code Examples

Three Equal Grid Columns

css
.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}
    

Responsive Grid Template

css
.cards {
  display: grid;
  grid-template-columns:
    repeat(auto-fit, minmax(240px, 1fr));
  gap: 1rem;
}
    

Named Grid Areas

css
.page {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
  grid-template-rows: 72px 1fr 60px;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
}

.header {
  grid-area: header;
}

.sidebar {
  grid-area: sidebar;
}

.main {
  grid-area: main;
}

.footer {
  grid-area: footer;
}
    

Responsive Dashboard Template

html
<div class="dashboard">
  <header class="header">Header</header>
  <aside class="sidebar">Sidebar</aside>
  <main class="main">Main Content</main>
  <footer class="footer">Footer</footer>
</div>

<style>
.dashboard {
  display: grid;
  grid-template-columns:
    240px minmax(0, 1fr);

  grid-template-rows:
    72px 1fr 60px;

  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";

  min-height: 100vh;
}

.header {
  grid-area: header;
}

.sidebar {
  grid-area: sidebar;
}

.main {
  grid-area: main;
  min-width: 0;
}

.footer {
  grid-area: footer;
}

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

  .sidebar {
    display: none;
  }
}
</style>
    

Pro Tip

For page-level layouts, start by sketching the rows and columns you actually need. For named regions such as header, sidebar, main, and footer, grid-template-areas can make the structure much easier to reason about. For repeated responsive content, repeat(auto-fit, minmax(...)) is often a strong starting pattern.

More CSS lessons

🎨 IgKnight Docs

Master HTML, CSS, JS, Python, .NET and more with easy-to-follow lessons and practical code examples.

© 2025 IgKnight Learning Platform. All rights reserved.