# Easier layout with this CSS Grid Method !

> You didn't know CSS well enough without knowing Flexbox.... and Grid !!! 

As a front-end developer, there's no doubt that you should understand the basic of **HTML and CSS** as a good start. However, lots of beginner try to avoid **CSS Grid** as much as they can due to the complex layout that they saw in every tutorials. Therefore, almost beginners start with **CSS Flexbox**. However, **CSS Grid** can have such great advantages if you want to layout your content in a more complex design, like this one perhaps:
 
![review section.jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1627534810666/vVkNYFR1G.jpeg)

#### Here's one important feature of CSS Grid that every beginner can apply to their projects right away after reading this blog

> The one and only


# Grid-template-areas

### Let's look at our design first 

We want to layout our page something look like this

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627542111889/TEwP7-W-J.png)
Look simple right? But if you use Flexbox only then it'll be harder to layout this design since it's more complex, so let's use CSS Grid :D
### Folder Structure
We only need 1 index.html file and 1 style.css file for this simple project

### HTML code
here's our HTML code to set this up:

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627542010899/vhWcITgV1.png)

First of all, the ```main class="container"``` will cover all our page so we can easily adjust the style. 

Then, we have ```nav class="navbar"``` which will be our grey area in the design. 

Then, the ```article class="layout"``` will cover all of our layout, so that's where we want to have the grid.

But before that, we'll need to reset our CSS style as a good practice,  here's how we did it in ```style.css``` file:


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627543240924/wUBneTRJ3W.png)

### CSS code

Now, we're all set, let's begin by adding ```background-color``` to our elements and make the ```font-size``` bigger


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627540668617/LugSkSbSB.png)


Here's how our page will look:


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627540732666/eEmVSLqYy.png)

Not a good layout as we expect right? That's why we'll use **CSS-Grid** 

### CSS Grid

In order to apply CSS Grid, you'll need this line of code:
```display: grid```

And we'll usually apply that ```display:grid``` in our **container** or **parents** that contains all of the elements so that we can organize our** children **

Let's take a look at our HTML above, the parent will be ```article class="layout" ``` since we use it to wrap around our content inside **except the navbar** because we want to have it on top. So we'll add that line of code for our landing class like this 

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627542241210/GlFmFJlSj.png)

Now, we can finally get to our main part for layout content

### Grid-template-areas

> What does grid-template-areas do?

So, **grid-template-areas** is a CSS Grid method that allow you to layout your grid depend on their "name", and it has many benefits, one of the obvious benefits is to make your layout more readable and easy to understand. Let's start :D

Firstly, in order to have grid-template-areas, we'll need to **name** our children elements that is wrapped by our parent ```article class="layout"``` using this line of code ```grid-area: (name of elements) ```
> Parents: .layout

> Children: #welcome, #about, #content, #link


Therefore, it'll look like this:
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627541580909/jkDN3sZQo.png)

Then, we'll go back to our parents and add this line of code ```grid-template-areas``` into our parents, which is the *.layout*

Inside that line of code we can layout our content by their name that we just set, and base on the design we'll have something like this:


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627542591613/cp6Fwt7d6.png)

This code basically say that we want to** layout our content into 2 rows and 3 columns**. Let's analyze** the first row**

>welcome about about


This mean we want the grid-area name ```welcome``` took 1/3 of the column and the ```about``` will took 2/3 the column

**The second row**
>content content link


Similarly, we want the grid-area name ```content``` took 2/3 of the column and the ```link``` will took 1/3 the column.

Our page will look something like this:



![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627542942966/Ai35E7bGp.png)

I just added **background-color** for the body, so we can see that our content doesn't expand throughout the body like in our design

> Why does that happen?

The reason is simple, because we're applying the layout for our ```article class="layout"```. Therefore, it doesn't cover the whole page, a fix to that is to add this line of code in our *.layout* class

```min-height:100vh;```

Now, our page will look something like this:


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627543289163/7zQa2qLbK.png)


Pretty close to our design, isn't it? (Since the design is based on different desktop size)

That's pretty much done with the **Grid-template-areas**, and that's the main reason for this post. Then, this method can also work for **responsive** with mobile-size, here's how it look:


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627547915965/jdyK0g55p.png)

You see the layout is still the same, and those children elements still take the same amount of column :D

Also, with the help of **media queries**, we can change the layout of our grid for different desktop's size and mobile and the code will be much more readable.

Now, if we want to center those text just like our design, we can use flexbox for each elements. That's why I said

> Flexbox +  Grid = CSS

Here's the complete code (with flexbox included)


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627543721744/FukT3nVuv.png)



And this'll be our result page:

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627543751849/lAvbvYnbq.png)


If you don't understand about **Flexbox** yet, I'll have another blog for that so don't worry, just focus on how our **grid-template-areas **work to divide our elements in each section of the page. 

Hope this help and happy coding! :D
