CSS – Grid Layout

Tram Ho

In this article we will learn about Grid, its properties as well as how to use Grid in practice.

What is Grid?

CSS Grid Layout is a two-dimensional grid layout system, thanks to the introduction of Grid, it has completely changed the way the layout of the website is designed. In the past, when designing websites, we often used tablefloatposition but the above methods will miss some important functions (vertical centering of elements, …). Flexbox can also be used to design very good layouts, but in some cases using Grid will be much faster and more convenient.

Some important concepts

Grid Container

Screenshot from 2021-07-30 16-29-23.png

As in the picture and the code you can see container is the entire section with an orange border and a gray background containing all the grid-item nice container is an element with a CSS attribute assigned to it display: grid.

Grid Item

Grid items are the first level child elements of the container of contaier.

As in the code, div with class not-child not grid item.

Grid Line

Horizontal or vertical lines form the structure and divide the grid-item called grid line. As you can see the blue line with the numbers below it is grid line.

Screenshot from 2021-07-30 16-54-58.png

Grid Cell

Grid cell is the distance between rows and columns of grid line. But box 1 to 9 below picture is grid-cell.

Screenshot from 2021-07-30 16-58-35.png

Grid Area

Grid area is an area consisting of grid-cell. For example, the area including cells 4, 5, 7, 8 above is a grid area.

Some attributes of Grid

Container

grid-template-columns

grid-template-columns Used to define columns in the grid layout. The values are the size of the column and are separated by spaces.

grid-template-columns: value value ...

grid-template-rows

grid-template-rows Used to define rows in the grid layout. The values are the size of the row and are separated by spaces.

grid-template-rows: value value ...

grid-template-areas

grid-template-areas Used to define a template grid that helps to align the layout more intuitively and often comes with the attribute grid-area live grid-item.

Suppose we want to create a layout with 2 rows and 3 columns with the first cell having a width of 2 cells, we will have the following template.

grid-column-gap

grid-column-gap is the space between columns in the grid layout.

grid-column-gap: <value>

Screenshot from 2021-07-30 18-00-44.png

We can see that the distance between the columns has been set to 1 rem.

grid-row-gap

Similar to grid-cloumn-gap then grid-row-gap is for rows.

grid-row-gap: <value>

gap

I can use gap to stand for 2 attributes is grid-column-gap and grid-row-gap.

gap: <grid-row-gap> <grid-column-gap> / <value> (cho cả 2 row và column)

Screenshot from 2021-07-30 18-06-52.png

grid-auto-columns and grid-auto-rows

These two properties are used to specify the size of the column or row.

grid-auto-columns/rows: min-content | max-content | auto | <value>

place-items

place-items is the shorthand of align-items: start | end | center | stretch (vertically aligned(column)) and justify-items: start | end | center | stretch (horizontally aligned (row)).

place-items: <align-items> / <justify-items>

Screenshot from 2021-07-30 18-11-19.png

Item

grid-column

I use grid-column to determine if the CSS cell has a length of grid-line (line vertically).

grid-column: grid-line-start(số bắt đầu)/grid-line-end(số kết thúc)

Screenshot from 2021-07-30 18-26-01.png

We can see that the first cell has a width extending from line 1 to line 3 and the last cell has a width extending from line 2 to line 4.

grid-row

I use grid-row to determine how tall the cell gets the CSS to be equal to grid-line (line vertically).

grid-row: grid-line-start(số bắt đầu)/grid-line-end(số kết thúc)

Screenshot from 2021-07-30 18-30-58.png

Similar to the above, we see that the first cell has the length from line 1 to line 3 and the height from line 1 to line 4.

grid-area

Give the name defined in the template grid-template-areasgrid-item that will have the same layout as in the template.

grid-area: <name-in-template>.

Screenshot from 2021-07-30 18-45-05.png

As we can see it’s easy to create a layout by taking the name defined in grid-template-areas to assign a value to grid-area belong to grid-item.

Practice

I will create a simple example of using grid to be responsive.

Here I use repeat()minmax() and auto-fit to be responsive. The columns of the grid will have widths ranging from 250px to 1fr and auto=fit Here, when the length of the block is larger than the container, the blocks will automatically go down the line and expand. The results can be viewed here.

Summary

So through this article, I have helped you learn about Grid. It helps us to layout easily. There are also some other useful properties of Grid that I haven’t mentioned, you can refer to here. I hope through this article you can use Grid for your projects. Thank you for following the article to the end.

Share the news now