CSS / CSS Grid Layout

Using Grid Template Areas Effectively

This tutorial will teach you how to use Grid Template Areas for a clean, organized and readable layout. We'll explore how to name grid areas and how to place items in these areas.

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Teaches the fundamentals of CSS Grid and advanced layout techniques.

1. Introduction

In this tutorial, we aim to understand how to use Grid Template Areas effectively for a clean, organized, and readable layout in CSS grid. The CSS grid layout is a two-dimensional layout system that gives you control over items in rows as well as columns, unlike Flexbox which is largely a one-dimensional system.

What will you learn?
- Naming grid areas
- Placing items in these areas
- Creating complex layouts with ease.

Prerequisites
Basic knowledge of HTML and CSS is required for this tutorial.

2. Step-by-Step Guide

Grid Template Areas allow us to create a template of our page layout and then place items into this layout. The areas are defined with the grid-template-areas property.

We can assign names to different grid areas and then use these names to place our items. This makes our layout code more readable and easier to understand.

Below is a simple example of how to use grid template areas:

Example:

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

In this example, we are defining a grid with three columns of equal size and auto rows. We then assign areas to the grid cells. The header, main, and footer each span across all three columns, while the sidebar only spans the first column of the second row.

3. Code Examples

Example 1: Basic Layout

<div class="container">
  <div class="item1">Header</div>
  <div class="item2">Sidebar</div>
  <div class="item3">Main</div>
  <div class="item4">Footer</div>
</div>
.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: auto;
  grid-template-areas: 
  "header header header"
  "sidebar main main"
  "footer footer footer";
}

.item1 { grid-area: header; }
.item2 { grid-area: sidebar; }
.item3 { grid-area: main; }
.item4 { grid-area: footer; }

In this example, we've assigned each div to a grid area using the grid-area property.

4. Summary

In this tutorial, we've learned how to use Grid Template Areas to create a clean, organized, and readable layout. We explored how to name grid areas and how to place items in these areas.

For more advanced layouts, you can explore nested grids and other CSS grid properties.

5. Practice Exercises

Exercise 1:
Create a basic layout for a blog post with a header, main content, sidebar, and footer.

Exercise 2:
Create a photo gallery layout with different sized photos. Some photos should span multiple columns or rows.

Solutions
1. This is similar to the example given above, but you need to adjust the grid-template-areas and grid-area properties to fit your content.
2. This is a bit more complex. You will need to assign different grid-area and span values to your images to make them span multiple columns or rows.

Remember, practice is key in mastering CSS Grid. Keep experimenting with different layouts and properties.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Image Converter

Convert between different image formats.

Use tool

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

Time Zone Converter

Convert time between different time zones.

Use tool

Keyword Density Checker

Analyze keyword density for SEO optimization.

Use tool

URL Encoder/Decoder

Encode or decode URLs easily for web applications.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help