Home >Web Front-end >CSS Tutorial >Detailed explanation of the usage of CSS grid layout (grid)

Detailed explanation of the usage of CSS grid layout (grid)

不言
不言Original
2018-11-14 15:10:524524browse

There are various layouts in web pages. A suitable layout can make the web page more beautiful. Using CSS grid layout, you can create complex columns using a grid layout with simple descriptions. In this article, we introduce the Grid Layout of CSS based on a simple example.

Let’s take a look at the container framework first

#(id名){
    display: grid;    
    grid-template-columns: (第一列宽度) (第二列宽度) ...... (第n列宽度);    
    grid-template-rows: (第一行高) (第二行高) ...... (第n行高);
    }

or

.(class名){
    display: grid;    
    grid-template-columns: (第一列宽度) (第二列宽度) ...... (第n列宽度);  
    grid-template-rows: (第一行高) (第二行高) ...... (第n行高);
    }

There is also a way to set up an intranet grid.

#(id名){
    display: inline-grid;    
    grid-template-columns: (第一列宽度) (第二列宽度) ...... (第n列宽度);  
      grid-template-rows: (第一行高) (第二行高) ...... (第n行高);
    }

or

.(class名){
    display: inline-grid;     
    grid-template-columns: (第一列宽度) (第二列宽度) ...... (第n列宽度);  
    grid-template-rows: (第一行高) (第二行高) ...... (第n行高);
    }

Grid Framework (Project Framework)

Specify the following CSS for elements that become grid frames.

#(id名){
    grid-column: (列方向的网格的开始位置)/(列方向的网格的结束位置);   
    grid-row: (行方向的网格的开始位置)/(行方向的网格的结束位置);
    }

or

.(class名){
   grid-column: (列方向的网格的开始位置)/(列方向的网格的结束位置);   
    grid-row: (行方向的网格的开始位置)/(行方向的网格的结束位置);
    }

or

#(id名){
    grid-column-start: (列方向的网格的开始位置);    
        grid-column-end: (列方向的网格的结束位置);    
        grid-row-start: (行方向的网格的开始位置);    
        grid-row-end: (行方向的网格的结束位置);
        }

or

.(class名){
          grid-column-start: (列方向的网格的开始位置);    
          grid-column-end: (列方向的网格的结束位置);    
          grid-row-start: (行方向的网格的开始位置);    
          grid-row-end: (行方向的网格的结束位置);
        }

Description example

Use the net Grid lines specify the starting and ending positions of the grid.

In the case of the code below, the width of the cell is from the vertical line of the second grid to the vertical line of the fourth grid.

 grid-column: 2 / 4;

Code Example

Create the following CSS and HTML files.

SimpleGrid.css

.Container {
    display: grid;    
    grid-template-columns: 160px 160px 160px 160px;    
    grid-template-rows: 120px 120px;    
    border:solid #ff6a00 1px;
}
.GridItem1 {
    grid-column: 1 / 2;    
    grid-row: 1 / 2;    
    background-color: #ff9c9c;
}
.GridItem2 {
    grid-column: 2 / 3;    
    grid-row: 1 / 2;    
    background-color: #ffcb70;
}
.GridItem3 {
    grid-column: 3 / 4;    
    grid-row: 1 / 2;    
    background-color: #fffd70;
}
.GridItem4 {
    grid-column: 4 / 5;    
    grid-row: 1 / 2;    
    background-color: #b0ff70;
}
.GridItem5 {
    grid-column: 1 / 2;    
    grid-row: 2 / 3;    
    background-color: #7ee68d;
}
.GridItem6 {
    grid-column: 2 / 3;    
    grid-row: 2 / 3;    
    background-color: #7ee6e2;
}
.GridItem7 {
    grid-column: 3 / 4;    
    grid-row: 2 / 3;    
    background-color:#95a7f5
    }
.GridItem8 {
    grid-column: 4 / 5;    
    grid-row: 2 / 3;    
    background-color: #d095f5;
}

SimpleGrid.html

<!DOCTYPE html><html><head>
  <meta charset="utf-8" />
  <title></title>
  <link rel="stylesheet" href="SimpleGrid.css" />
  </head>
  <body>
  <div class="Container">
    <div class="GridItem1">内容1</div>
    <div class="GridItem2">内容2</div>
    <div class="GridItem3">内容3</div>
    <div class="GridItem4">内容4</div>
    <div class="GridItem5">内容5</div>
    <div class="GridItem6">内容6</div>
    <div class="GridItem7">内容7</div>
    <div class="GridItem8">内容8</div>
  </div>
  </body>
  </html>

Description:

The following CSS description of the container creates a 4 row × 2 row grid.

.Container {
    display: grid;    
    grid-template-columns: 160px 160px 160px 160px;    
    grid-template-rows: 120px 120px;    
    border:solid #ff6a00 1px;
}

The CSS for each element of the grid will be (GridItem 1~GridItem 8). We define grid cells for each grid. Change the background color for each cell of the grid.

.GridItem1 {
    grid-column: 1 / 2;    
    grid-row: 1 / 2;    
    background-color: #ff9c9c;
}

Display results

Use Firefox browser to display the above HTML file. The effect shown below will be displayed. Create a 2 row by 4 column grid and display the string "item n" in each cell. Additionally, the background color of the cell can be set for each cell.

Detailed explanation of the usage of CSS grid layout (grid)

Likewise, the same file is displayed in Google Chrome. The effect shown below will be displayed.

Detailed explanation of the usage of CSS grid layout (grid)

#The grid display cannot be completed in IE browser and the display is collapsed.

Example of No Cells in All Grids

While the previous example introduces the case where there is an option for all grids within a cell, it Will even work if any project in all grids. Below is an example of sparse (discrete) cells in a grid.

Code

Create the following CSS, HTML files.

SimpleGridSparse.css

.Container {
    display: grid;    
    grid-template-columns: 160px 160px 160px 160px;    
    grid-template-rows: 120px 120px;    
    border: solid #ff6a00 1px;    
    background-color:#E0E0E0;
}
.GridItem1 {
    grid-column: 2 / 3;    
    grid-row: 1 / 2;    
    background-color: #ff9c9c;
}
.GridItem2 {
    grid-column: 3 / 4;    
    grid-row: 2 / 3;    
    background-color: #ffcb70;
}
.GridItem3 {
    grid-column: 4 / 5;    
    grid-row: 1 / 2;    
    background-color: #fffd70;
}

SimpleGridSparse.html

<!DOCTYPE html><html><head>
  <meta charset="utf-8" />
  <title></title>
  <link rel="stylesheet" href="SimpleGridSparse.css" />
  </head>
  <body>
  <div class="Container">
    <div class="GridItem1">内容1</div>
    <div class="GridItem2">内容2</div>
    <div class="GridItem3">内容3</div>
  </div>
  </body>
  </html>

Instructions:

By the following code, The outer frame of the grid is a 2 row × 4 column grid.

 display: grid;  
 grid-template-columns: 160px 160px 160px 160px;  
 grid-template-rows: 120px 120px;

The CSS for the unit part of the grid is as follows. This time it's a 2×4 8-cell grid, but we've only arranged 3 cells in it. Frame the container in the second column of the first row, the third column of the cell in the second row, and frame the content in three places in the fourth column of the cell in the first row.

.GridItem1 {
    grid-column: 2 / 3;    
    grid-row: 1 / 2;    
    background-color: #ff9c9c;
}
.GridItem2 {
    grid-column: 3 / 4;    
    grid-row: 2 / 3;    
    background-color: #ffcb70;
}
.GridItem3 {
    grid-column: 4 / 5;    
    grid-row: 1 / 2;    
    background-color: #fffd70;
}

The HTML part of the grid. Describes three div frames within a grid frame.

<div class="Container">
    <div class="GridItem1">内容1</div>
    <div class="GridItem2">内容2</div>
    <div class="GridItem3">内容3</div>
  </div>

Display results

We will display the above HTML in Firefox browser. The effect shown below will be displayed. The content frame is placed at the location specified by CSS.

Detailed explanation of the usage of CSS grid layout (grid)

The effect displayed in Google Chrome is as follows.

Detailed explanation of the usage of CSS grid layout (grid)

The above is the detailed content of Detailed explanation of the usage of CSS grid layout (grid). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn