duda
How To Use a CSS Grid Template Area & Why It Can Save You Time

June 11, 2021
0 minute read

For website professionals, saving time on website builds and mockups is a must to stay competitive and profitable. One way to satisfy this business need is to use CSS grid templates and CSS grid template areas.

Many modern websites are built using grids, which divide a page into major regions and define the relationship between elements in terms of size, position, and layer. Grids make it easy to place elements into both columns and rows.

This means that whether you use code to create a CSS grid template, choose from grid template starters, use a theme built via grids, or use some plug-and-play design elements in a website builder like Duda, it pays to understand how CSS grid templates and grid template areas work.

The power of CSS grid is incorporated into Duda’s editor via DudaFlex sections, which enable digital marketing agencies and their customers to create pixel-perfect designs at breakneck speed inside our intuitive drag-and-drop site editor. With DudaFlex, you get the power of CSS grid-based designs in a fraction of the time it would take to develop them using the traditional coding methods outlined below.

CSS Grid Template Area Basics

A CSS grid template area makes a visual representation of the grid using both columns and rows. This makes for a faster design process than when line-based placement or grid-column and grid-row values are used. 


Why?


Using line-based placement isn’t as streamlined as grid template areas. And, as UI/UX Designer & Front-End Developer Ahmad Shadeed points out, when you change one element in grid-column, like the width of the sidebar span, you also have to individually change the width of the footer, main element, and other elements. 


With a CSS grid template area, you set the width in a single command and then the placements of the grid cells — and everything adjusts accordingly.


Another benefit to CSS grid template areas: they are responsive.

How To Use CSS Grid Template Areas

To reap the time saving benefits of designing with this method, build a CSS grid template and name your CSS grid template areas in two CSS coding steps. Common grid template areas to specify include header, menu, footer, sidebar, main content, etc. Once these are set up, they’re easy to move around within your grid template.


First, define names of your grid template areas with the grid-area property. This sets up the next step, which will create the grid-template layout.

For instance, below is an example from Mozilla that sets the grid areas with shortcuts for header, footer, content, and sidebar.

 

.header {

    grid-area: hd;

}

.footer {

    grid-area: ft;

}

.content {

    grid-area: main;

}

.sidebar {

    grid-area: sd;

}


Second, use grid-template areas property to define how the areas appear in the rows and columns. This creates your easy-to-use webpage template.

For instance, in the example below from Mozilla, the first line command sets the display as a grid. In the next line command, the columns are defined as “9” items long. In the next line command, the rows are set. Then, the CSS grid template areas are given their arrangements.

 

wrapper {

    display: grid;

    grid-template-columns: repeat(9, 1fr);

    grid-auto-rows: minmax(100px, auto);

    grid-template-areas:

      "hd hd hd hd   hd   hd   hd   hd   hd"

      "sd sd sd main main main main main main"

      "ft ft ft ft   ft   ft   ft   ft   ft";

}


As you see in the code above, you need to use the grid-template areas (names of your content areas) multiple times to add up to the number of columns in your grid template. The header “hd” above gets all nine widths in the column. In the next row, the sidebar as defined as “sb” in the code above gets three widths in the column, and the main content gets six widths of the column. In the third row, the footer gets all nine column widths.


Here is the HTML that follows the CSS. The HTML populates the grid template with the actual content.

 

<div class="wrapper">

    <div class="header">Header</div>

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

    <div class="content">Content</div>

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

</div>


And here is the result:

a diagram of a web page showing the header, sidebar, content area and footer.

Understanding the Code

You can use the shorthand CSS fraction code (fr), meaning fraction of the free space. The fraction command eliminates overflow on the x-axis even when you set a grid-column-gap value. With the fraction command, when, as in the above example, you set each column to “1fr,” the grid accounts for the grid-column-gap value and subtracts it from the total width for each column. 


You can also use the shorthand repeat code like above to make the columns repeat. For example, “grid-template-columns:repeat (9, 1fr);” shows the combined commands that specify the fraction code and the number of columns. For 6 columns or 12 columns or 15 columns, you would change the value after it repeats.


You also can easily control other ways the grid template appears.

For instance: 

  • You can set the grid-gap.
  • You can set a background color.
  • You can set padding.



Here’s another example, which uses the “container” command instead of the “wrapper” command.

 

.item-1 {

  grid-area: head;

}

.item-2 {

  grid-area: main;

}

.item-3 {

  grid-area: side;

}

.item-4 {

  grid-area: footer;

}


.container {

  display: grid;


  grid-template-columns: 2fr 2fr 1fr 2fr;

  grid-template-rows: 100px 200px 100px;


  grid-template-areas:

    "head head . side"

    "main main . side"

    "footer footer footer footer";

}


When the HTML with the actual content (not included in the above code) is added to this CSS grid template, it will result in:

a diagram of a web page showing the header, main area, sidebar and footer.

The above example uses additional shortcuts where the dot represents an empty grid shell. 


You can do a ton with this quickly by only changing a tiny bit of the code. For the above, you can completely revamp the layout to look like this:

a diagram of a website with a header, main area, sidebar and footer .

The only code that changed were where the spaces (using dots) land in the template — see below:

 

.container {

  /* ... */


  grid-template-areas:

    "head head  .    side"

    "main main main  side"

    ". footer footer side";


  /* ... */

}


Let's Play With CSS Grid Templates

There’s even more you can quickly accomplish once you name CSS grid template areas and create a template. For instance, you can also set a column that doesn’t change values followed by columns that do. Here’s how the code would be different with the first column set at 250px followed by repeating columns set by fraction of free area:

 

.grid {

  display: grid;

  grid-template-columns: 250px repeat(12, 1fr);

  grid-column-gap: 10px;

}


a red background with red stripes on it

What makes CSS grid template areas really stand out is you can make them fully responsive by adding a snippet of additional code. With CSS grid templates, you can easily tell the design how to respond based on a device’s pixel width and height.

Here’s an example from Mozilla:

 

@media (min-width: 500px) {

    .wrapper {

        grid-template-columns: repeat(9, 1fr);

        grid-template-areas:

          "hd hd hd hd   hd   hd   hd   hd   hd"

          "sd sd sd main main main main main main"

          "sd sd sd  ft  ft   ft   ft   ft   ft";

    }

}

@media (min-width: 700px) {

    .wrapper {

        grid-template-areas:

          "hd hd hd   hd   hd   hd   hd   hd hd"

          "sd sd main main main main main ft ft";

    }

}


This results in two columns when the screen is at least 700 pixels but three columns when the screen is smaller. (Note: The template areas were defined in separate code not affected by the additional media query code.)

The below example takes the Alligator.io layout above and makes it work on smaller screens.

Here’s the additional code (added onto the already set Alligator.io code above without changing anything in the previous code) that specifies the max width as a smartphone of 40em. Anything at that size width or smaller will display the stacked look.

 

@media screen and (max-width: 40em) {

  .container {

    grid-template-areas:

      "head head head head"

      "main main main main"

      "side side side side"

      "footer footer footer footer";

  }

}


a diagram of a web page with a header, main area, sidebar and footer .

Let’s look at another example from Shadeed that adds the media query code for three different types of devices. 

Here is the initial code for defining the template areas and the three-column grid:

 

.wrapper {

    display: grid;

    grid-template-columns: 1fr 1fr 1fr;

    grid-template-areas:

       "featured item-1 item-1"

       "featured item-2 item-2";

    grid-gap: 1.25rem;

}

 

.item.featured {

  grid-area: featured;

}

 

.item-2 {

  grid-area: item-1;

}

 

.item-3 {

  grid-area: item-2;

}


This is how it looks (with the labels added afterward to demonstrate placement; these wouldn’t show up without additional HTML code):

a grid CSS template showing a featured item 1 and a featured item 2

To make this fully responsive, do not change the code that defines the template areas, but add code for stacked layout and for the three-column layout with “Featured item” in column 1 and then “item 1” in column 2 and “item 2” in column 3. 

What Shadeed also does below is take the original code that created the CSS grid template and labels it with a media query.

 

/* Stacked Layout */

main {

  display: grid;

  grid-template-columns: 1fr 1fr 1fr;

  grid-template-areas: 

            "featured featured featured" 

            "item-1 item-1 item-1" 

            "item-2 item-2 item-2";

  grid-gap: 1.25rem;

}

 

/* 3-col Layout */

@media (min-width: 600px) {

  main {

    grid-template-areas: 

            "featured item-1 item-2" 

            "featured item-1 item-2";

  }

}

/* Featured Layout */

@media (min-width: 900px) {

  main {

    grid-template-areas: 

            "featured item-1 item-1" 

            "featured item-2 item-2";

  }

}


And here's what that looks like:

an illustration showing three different types of layouts for CSS grids: stacked, 3-col and featured.

Summing Up

CSS grid templates speed up design, which is why there are so many examples of prebuilt templates available. They’re also an invaluable tool for responsive websites.

Because of this — even though in a website builder platform like Duda you can cut and paste elements around without coding them in CSS — you’ll want to understand and master how CSS grid templates work to improve your web design skills.

Related Posts



A black and white photo of a website presenting a group of people wearing sunglasses.
By Renana Dar November 28, 2024
In this article, we'll explore these three aspects to help you build a stronger online presence and drive business growth through your website.
A black and white photo of a motorcycle with the words `` up-to-date website ''.
By Renana Dar November 26, 2024
When choosing a website builder—especially as a digital agency building multiple sites—maintenance is often overlooked, but it shouldn’t be. Here's why!
A website, built on Duda, alongside a website migration checklist
By Shawn Davis November 6, 2024
Some clients love their website, but not their platform. Learn how to satisfy those clients with a seamless migration experience that completely transforms their experience.
Show More

Did you find this article interesting?


Thanks for the feedback!
Different logos of freelance websites such as Upwork and Fiverr
By Renana Dar December 3, 2024
Discover 20+ top freelance websites to hire skilled talent for your agency. From managing overflow work to finding specialized expertise, these platforms help you grow your business while avoiding scams and hiring pitfalls.
A black and white photo of a website presenting a group of people wearing sunglasses.
By Renana Dar November 28, 2024
In this article, we'll explore these three aspects to help you build a stronger online presence and drive business growth through your website.
A black and white photo of a motorcycle with the words `` up-to-date website ''.
By Renana Dar November 26, 2024
When choosing a website builder—especially as a digital agency building multiple sites—maintenance is often overlooked, but it shouldn’t be. Here's why!
Show More

Latest Posts

Share by: