Building a Layout with CSS Grid Using Grid Areas and Grid Templates Properties
Tags: web, development
Published: 2022-11-08 13:06:57

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/CSSgrid_90f77efde1dc6755722bf7fafad48562.jpg" alt="A close-up of code in an editor."/></figure>

The [Grid layout](https://www.w3schools.com/css/css_grid.asp) introduces a two-dimensional grid system to CSS; it simplifies designing a web page and enables you to move items from one grid line to another. Thus, the grid areas and grid template are vital CSS properties that help to define and assign a name to the grid item. In addition, these properties specify the grid item's size and location within a grid. Therefore, before jumping into this subject, it’s important that you understand the grid and how you can apply it to a web page effortlessly.

The [Grid layout](https://www.w3schools.com/css/css_grid.asp) introduces a two-dimensional grid system to CSS; it simplifies designing a web page and enables you to move items from one grid line to another. Thus, the grid areas and grid template are vital CSS properties that help to define and assign a name to the grid item. In addition, these properties specify the grid item's size and location within a grid. Therefore, before jumping into this subject, it’s important that you understand the grid and how you can apply it to a web page effortlessly.

## What is CSS Grid Layout?

The [CSS Grid](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout) is a two-dimensional layout that you can use to create responsive web items. It presents a grid-based layout system with rows and columns. Also, it makes it easier to design web pages without using floats and positioning. The grid system is the foundation of a solid design, and its element consists of a parent part and one or more child parts. You can also place a component into the grid within the column and row lines.

**Let's look at the example below for a better understanding:**

```
<!DOCTYPE html>
  <html>
  <head>
  <style>
 .one {
   grid-area: headline;
   background-color: grey;
   text-align: center;
   padding: 50px 0;
   font-size: 30px;}
 .two {
   grid-area: menu;
   background-color: purple;
   text-align: center;
   padding: 50px 0;
   font-size: 30px; }
 .three {
   grid-area: news;
   background-color: white;
   text-align: center;
   padding: 50px 0;
   font-size: 30px; }
 .four {
   grid-area: attention;
   background-color: yellow;
   text-align: center;
   padding: 50px 0;
   font-size: 30px; }
 .five {
   grid-area: click ;
   background-color: pink;
   text-align: center;
   padding: 50px 0;
   font-size: 30px;}
 .grid-container {
   display: Grid;
   grid-template-areas:
   'headline headline headline headline headline headline'
   'menu news news news attention attention'
   'menu click click click click click';
 gap: 10px;
 background-color:blue;
 padding: 10px;
}
</style>
</head>
<body>
<h1>Grid Layout</h1>
<div class="grid-container">
    <div class="one">Headline</div>
    <div class="two">Menu</div>
    <div class="three">News</div>
    <div class="four">Attention</div>
    <div class="five">Click</div>
 </div>
 </body>
 </html>
```

**Here is the outcome:**

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image3_59d775c65f3cd66a2102ec3e90fcce47.png" alt="A diagram of the CSS grid layout."/></figure>

### The Importance of CSS Grid

There are several reasons why a developer should use CSS Grid:

- It enables a developer to optimize the markup to allow accessibility without compromising the manipulative capacity of the visual result.
- It makes the markup on the web page easy to understand.
- It improves the web design user interface and enhances the user experience.
- It allows various UIs to be implemented quickly and houses them across multiple contexts.
- It enhances the visual impression of the web page.
- It helps create a user-friendly web page.
- It helps to build more complex layouts using columns and rows.


## What is a Grid Area Property?

The [grid-area property](https://www.w3schools.com/cssref/pr_grid-area.asp) specifies a grid item's size and location within a grid. It is a shorthand property, contributing a line or a span to its grid placement. Thus, it specifies the edges of its grid area. A grid area is a shorthand property that includes the following: `grid-row-start`, `grid-column-start`, `grid-row-end`, and `grid-column-end`.

**Let's look at the example of the grid area:**

```
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
 display: Grid;
 grid-template-columns: auto auto auto auto;
 grid-gap: 20px;
 background-color: blue;
 padding: 20px;
}
.grid-container div {
 background-color: grey;
 text-align: center;
 padding: 20px 0;
 font-size: 30px;
}
.item7 {
 grid-area: 1 / 1 / span 2 / span 3;
}
</style>
</head>
<body>
<h1>The grid-area Property</h1>
<div class="grid-container">
 <div class="item1">One</div>
 <div class="item2">Two</div>
 <div class="item3">Three</div>
 <div class="item4">Four</div>
 <div class="item5">Five</div>
 <div class="item6">Six</div>
 <div class="item7">Seven</div>
</div>
</body>
</html>
```

**Here is the result:**

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image4_c1de99118b6e00e75668b9393b7b8585.png" alt="The seven areas of a grid property."/></figure>

## What Is a Grid Template Property?

The [grid template](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template) is a shorthand property that allows you to define the columns, rows and areas in the CSS grid in the container with one declaration. Note that a grid template has these properties: `grid-template-rows`, `grid-template-columns` and `grid-template-areas`.

**Let's look at the example of how a CSS grid template works:**

```
<!DOCTYPE html>
  <html>
  <head>
  <style>
 .grid-container {
 display: grid;
 grid-template: 150px / auto auto auto auto;
 grid-gap: 20px;
 background-color: blue;
 padding: 20px;
  }
 .grid-container div {
 background-color: grey;
 text-align: center;
 padding: 20px 0;
 font-size: 30px;
  }
  </style>
  </head>
  <body>
  <h1>The grid-template Property</h1>
  <div class="grid-container">
    <div class="item1">One</div>
    <div class="item2">Two</div>
    <div class="item3">Three</div>
    <div class="item4">Four</div>
    <div class="item5">Five</div>
    <div class="item6">Six</div>
    <div class="item7">Seven</div>
    <div class="item8">Eight</div>
  </div>
  </body>
  </html>
```

**Here is the outcome:**

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image5_7c087411f465cc66679c157d2cc6ced1.png" alt="A 4-by-2 grid template."/></figure>

## Types of Grid Area Properties

There are different types of grid area properties, which we’ll discuss in detail below. They are as follows:

- `grid-row-start`
- `grid-column-start`
- `grid-row-end`
- `grid-column-end`


### grid-row-start

The [grid-row-start](https://www.w3schools.com/cssref/pr_grid-row-start.asp) property defines the grid item's start position within the grid row by specifying the inline start edge of its grid area.

**Here is an example of grid-row-start:**

```
<style>
.grid-container {
 display: grid;
 grid-template-columns: auto auto auto auto;
 grid-gap: 10px;
 background-color: blue;
 padding: 10px;
}
.grid-container div {
 background-color: grey;
 text-align: center;
 padding: 20px 0;
 font-size: 30px;
}
.item8 {
 grid-row-start: 1;
}
</style>
```

**Here is the result:**

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image10_f308fa9b6fc21fd555be6f8de66c1f99.png" alt="A grid in which 8 begins the first row."/></figure>

### grid-column-start

The [grid-column-start](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-column-start) property defines on which particular column line an item should start. Also, there are other values for the grid-column-start property, and the user can start from anywhere with the different values. There is a specific value that is affected on the same naming block as well.

**Now, let's look at the example below:**

```
<style>
.grid-container {
 display: grid;
 grid-template-columns: auto auto auto auto;
 grid-gap: 10px;
 background-color: blue;
 padding: 10px;
}
.grid-container div {
 background-color: grey;
 text-align: center;
 padding: 20px 0;
 font-size: 30px;
}
.item3 {
 grid-column-start: 1;
}
</style>
```

**Here is the outcome:**

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image1_10b5c7fc583df4494ba4fb581622cf80.png" alt="A grid in which not all columns are filled."/></figure>

### grid-row-end

The [grid-row-end](https://www.w3schools.com/cssref/pr_grid-row-end.asp) property specifies how many rows an item will span and on which row line the item will end. It indicates the row grid line where a grid item ends in a grid layout. In addition, this property, among other line-based placement grid properties, controls a grid item's size and where it sits on the grid.

**Let's look at the example below for clarity:**

```
<style>
 .grid-container {
 display: grid;
 grid-template-columns: auto auto auto;
 grid-gap: 10px;
 background-color: blue;
 padding: 10px;
  }
 .grid-container div {
 background-color: grey;
 text-align: center;
 padding: 20px 0;
 font-size: 30px;
  }
 .item3 {
 grid-row-end: span 2;
  }
  </style>
```

**Here is the result:**

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image2_fb0115cbbc00e92dd65b798af8ea7db5.png" alt="A grid in which two cells are merged."/></figure>

### grid-column-end

The [grid-column-end](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-column-end) property defines the columns and the items that span it, and on which column-line the item will end within the grid.

**Here is an example of grid-column-end:**

```
<style>
.grid-container {
 display: grid;
 grid-template-columns: auto auto auto auto;
 grid-gap: 10px;
 background-color: blue;
 padding: 10px;
}
.grid-container div {
 background-color: grey;
 text-align: center;
 padding: 20px 0;
 font-size: 30px;
}
.item9 {
 grid-column-end: span 4;
}
</style>
```

**Here is the outcome:**

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image7_018918cb603045e5defb69f3b701566a.png" alt="A grid in which the bottom row is merged."/></figure>

Next, let's look at the types of grid templates in this study.

## Types of Grid Template Properties

Grid templates have different property types and states:

- `grid-template-rows`
- `grid-template-columns`
- `grid-template-areas`


### grid-template-rows

The [grid-template-rows](https://www.w3schools.com/cssref/pr_grid-template-rows.asp) property defines the number of rows and the heights of the rows in a grid. The values of the grid-template rows are space-separated lists, where each height of the row is a defined value.

**Let's look at an example of grid-template-rows:**

```
<style>
.grid-container {
 display: grid;
 grid-template-columns: auto auto auto auto;
 grid-template-rows: 80px 270px;
 grid-gap: 20px;
 background-color: blue;
 padding: 20px;
}
.grid-container div {
 background-color: grey;
 text-align: center;
 padding: 20px 0;
 font-size: 30px;
}
</style>
```

**Now, let's look at the result:**

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image8_3a4d9cc94e85ebcd42eb0bf9e977e16d.png" alt="A grid in which the bottom row is much larger than the top row."/></figure>

### grid-template-columns

The [grid-template-columns](https://www.w3schools.com/cssref/pr_grid-template-columns.asp) property defines the number and the widths of columns in a grid. In addition, the values are a space-separated list, where each value defines the size of the column. The number of columns in the grid is set by the numerical values defined in the list.

**Let's look at an example of grid-template-columns:**

```
<style>
.grid-container {
 display: grid;
 grid-template-columns: auto auto;
 grid-gap: 20px;
 background-color: blue;
 padding: 20px;
}
.grid-container div {
 background-color: grey;
 text-align: center;
 padding: 20px 0;
 font-size: 30px;
}
</style>
```

**Here is the outcome:**

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image9_0f00ea0e1dafc0743a1873921912972c.png" alt="A grid in which all cells are equal."/></figure>

### grid-template-areas

The [grid-template-areas](https://www.w3schools.com/cssref/pr_grid-template-areas.asp) property defines areas within the grid. Also, it helps to name the grid items using the grid-area properties, and then reference the name in the grid-template-area property. Apostrophes also define each area. It helps to use a period to refer to a grid item with no name.

**Let's look at an example of the grid-template-areas property:**

```
<style>
.item6 {
 grid-area: realArea;
}
.grid-container {
 display: grid;
 grid-template-areas: 'realArea realArea...';
 grid-gap: 20px;
 background-color: blue;
 padding: 20px;
}
.grid-container div {
 background-color: grey;
 text-align: center;
 padding: 10px 0;
 font-size: 30px;
}
</style>
```

**Here is the result:**

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image6_db4e31f35b2349c8f8999d08d6676bc9.png" alt="A grid in which two cells are merged."/></figure>

## Conclusion

A grid is a collection of intersecting horizontal and vertical lines that defines columns and rows. It eases a web page's design and allows you to place items from one grid line to another. CSS Grid is a two-dimensional layout that you can use to create responsive web items. Now, you can enjoy working with grids!