Sign up: Designing Enterprise UI Directly on Production Code

What are best AI tools? Take the State of AI survey

Builder.io
Builder.io
Contact sales

Sign up: Designing Enterprise UI Directly on Production Code

What are best AI tools? Take the State of AI survey

Builder.io
Builder.io
< Back to blog

web development

Become a CSS Grid wizard with grid-template-areas

March 9, 2023

Written By Sami Jaber

One of the most fascinating aspects of CSS Grid to me is how you can use a single property (grid-template-areas) on a grid parent to describe the entirety of its complex layout.

In this article, I’m going to go over how easily you can use this property to describe a grid layout like this one:

Setup

Let’s start with a list of boxes wrapped in a container:

See the Pen grid-template-areas by sami (@samijaber) on CodePen.

Add the CSS Grid

Now, to convert this into a CSS Grid, we need to:

  • Set display: grid on the parent and an initial grid-template-areas.
  • Add a grid-area property to each child and give it a unique name.

How does grid-template-areas work? You provide it with a sequence of strings that instruct the grid on where each individual grid-area will be located.

Let’s start with a basic grid-template-areas: "a b c d"; . This means that you will have all four boxes lined up in a row:

See the Pen grid-template-areas - 2 by sami (@samijaber) on CodePen.

Shuffle things around

What happens if we change it to grid-template-areas: "c a d b"; ? We get to reorder our elements without actually reordering them in the DOM! How cool is that?!

See the Pen grid-template-areas - 3 by sami (@samijaber) on CodePen.

Let’s get multi-dimensional

grid-template-areas allows you to describe a 2D grid by writing a multi-string expression by writing their grid-area name multiple times. If we write:

Then we end up with a 2x2 grid!

See the Pen 2d grid with empty cells by sami (@samijaber) on CodePen.

The magical . area

Another cool property of grid-template-areas is that you can describe empty cells using a dot. So if you write:

Then the grid will allocate empty space to those 2 cells.

See the Pen grid-template-areas - 4 by sami (@samijaber) on CodePen.

Multi-cells grid areas

The last feature I want to discuss is stretching grid areas. By writing the same grid area name multiple times to make a rectangle shape, you can stretch an element!

We can combine everything we learnt today to build a complex 3x4 grid with stretched elements and empty cells between them:

See the Pen grid-template-areas - 5 by sami (@samijaber) on CodePen.

Notice that I explicitly set grid-template-columns and grid-template-rows. This is needed because the CSS Grid will otherwise collapse the stretched elements, resulting in fewer rows/columns than we want. We use the repeat(x, 1fr) function to tell CSS that we want x rows (or columns) to split the available free space, fr, equally.

Warning

There are two important things to keep in mind when using grid-template-areas:

  • Each row must have the exact same number of cells (add . to fill in any blanks).
  • An element can only stretch into a rectangle. This means that this example:

is invalid, because b makes a complex shape that is not a rectangle.

If you don’t respect either of these two rules, your grid will completely break, so watch out!

Wrap up

Isn’t it amazing that, besides setting the grid-area properties, we didn’t have to touch any of the grid children or reorder them? We were able to modify our layout to an extreme degree by modifying the container’s CSS class over and over.

This can be extremely useful when you need to:

  • iterate very quickly on a design and test different ideas
  • build responsive designs that involve a lot of resizing and reordering between sizes

I hope you are as pleased to have learned this as I am to share it with you. Let me know what you end up building with CSS Grid!

Share

Twitter
LinkedIn
Facebook

Generate high quality code that uses your components & design tokens.

Try it nowGet a demo

Continue Reading
AI8 MIN
Subagents: When and How to Use Them
WRITTEN BY Alice Moore
February 3, 2026
AI9 MIN
Best LLMs for coding in 2026
WRITTEN BY Matt Abrams
January 28, 2026
AI8 MIN
Lovable Alternatives for 2026
WRITTEN BY Alice Moore
January 27, 2026

Product

Visual CMS

Theme Studio for Shopify

Sign up

Login

Featured Integrations

React

Angular

Next.js

Gatsby

Resources

User Guides

Developer Docs

Forum

Blog

Github

Get In Touch

Chat With Us

Twitter

Linkedin

Careers

© 2020 Builder.io, Inc.

Security

Privacy Policy

Terms of Service

Get the latest from Builder.io

By submitting, you agree to our Privacy Policy

  • Fusion

  • Publish

  • Product Updates

  • Design to Code

  • Headless CMS

    Multi-Brand CMS

  • Landing Pages

  • Web Apps

  • Prototypes

  • Marketing Sites

  • Headless Commerce

  • Documentation

  • Fusion Docs

  • Publish Docs

  • Figma AI to Production Code

  • AI Prototyping for Product Managers

  • Figma to Storybook

  • Figma to App Converter

  • Blog

  • Webinars

  • Guides

  • Case Studies

  • Community Forum

  • Partners

  • Affiliate Program

  • CMS Integrations

  • CMS Blueprints

  • Glossary

  • Figma to Code Guide

  • Headless CMS Guide

  • Headless Commerce Guide

  • Composable DXP Guide

  • About

  • News

  • Careers

  • Contact Sales

Security

Privacy Policy

SaaS Terms

Trust Center

Cookie Preferences

YouTube icon
Github icon
Blsky Icon
Twitter "X" icon
LinkedIn icon
Feed Icon