search
HomeWeb Front-endCSS TutorialDetailed explanation of the usage of CSS grid layout (grid)

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
如何通过Css Flex 弹性布局实现不规则的网格布局如何通过Css Flex 弹性布局实现不规则的网格布局Sep 28, 2023 pm 09:49 PM

如何通过CSSFlex弹性布局实现不规则的网格布局在网页设计中,常常需要使用网格布局来实现页面的分割和排版,通常的网格布局都是规则的,每个网格大小相同,而有时候我们可能需要实现一些不规则的网格布局。CSSFlex弹性布局是一种强大的布局方式,它可以很容易地实现各种网格布局,包括不规则的网格布局。下面我们将介绍如何利用CSSFlex弹性布局来实现不

CSS布局指南:实现网格布局的最佳实践CSS布局指南:实现网格布局的最佳实践Oct 26, 2023 am 10:00 AM

CSS布局指南:实现网格布局的最佳实践引言:在现代网页设计中,网格布局已经成为一种非常流行的布局方式。它可以帮助我们更好地组织页面结构,使其更具有层次感和可读性。本篇文章将介绍网格布局的最佳实践,以及具体的代码示例,帮助你更好地实现网格布局。一、什么是网格布局?网格布局是指通过网格将页面分成多个列和行,使得页面的元素可以方便地按照一定的规律进行排列。网格布局

如何使用HTML创建一个基本的网格布局页面如何使用HTML创建一个基本的网格布局页面Oct 21, 2023 am 10:37 AM

如何使用HTML创建一个基本的网格布局页面网格布局是一种常见且实用的页面布局方式,它能够以网格的形式将页面划分为多个区域,并且能够灵活地调整区域的大小和位置。在这篇文章中,我们将介绍如何使用HTML来创建一个基本的网格布局页面,并提供具体的代码示例供参考。首先,我们需要在HTML文件中设置一个容器元素,它将作为网格布局的根元素,可以是一个div或者secti

CSS网格布局:使用网格布局创建复杂的网页布局CSS网格布局:使用网格布局创建复杂的网页布局Nov 18, 2023 am 10:35 AM

CSS网格布局:使用网格布局创建复杂的网页布局,需要具体代码示例在现代的网页设计中,网页布局起着至关重要的作用。为了创建复杂的网页布局,设计师和开发人员需要使用优秀的工具和技术。其中,CSS网格布局是一种强大而灵活的方法,可以帮助我们轻松地创建复杂的网页布局。本文将具体介绍CSS网格布局的使用方法,并提供一些实用的代码示例。CSS网格布局是一种新的布局模式,

如何使用HTML和CSS创建一个响应式图片网格布局如何使用HTML和CSS创建一个响应式图片网格布局Oct 27, 2023 am 10:26 AM

如何使用HTML和CSS创建一个响应式图片网格布局在当今的互联网时代,图片占据了网络内容的重要一部分。为了展示各种类型的图片,我们需要一个有效且美观的网格布局。在本文中,我们将学习如何使用HTML和CSS创建一个响应式的图片网格布局。首先,我们将使用HTML创建一个基本的结构。以下是示例代码:&lt;!DOCTYPEhtml&gt;&lt;html&gt

如何使用HTML和CSS实现网格列表布局如何使用HTML和CSS实现网格列表布局Oct 20, 2023 pm 05:45 PM

如何使用HTML和CSS实现网格列表布局在现代网页设计中,网格列表布局成为了一个非常常见的布局模式。它可以帮助我们轻松地创建出漂亮的网页,让内容清晰地排列在网页中。本文将介绍如何使用HTML和CSS来实现网格列表布局,并提供具体的代码示例。首先,我们需要使用HTML来构建网页的基础结构。以下是一个简单的例子:&lt;!DOCTYPEhtml&gt;&lt

微信小程序中PHP开发的网格布局实现方法微信小程序中PHP开发的网格布局实现方法Jun 01, 2023 am 08:48 AM

近年来,微信小程序已经成为了移动端开发的重要工具之一,而PHP作为一门常用于Web后端开发的语言,也逐渐成为了小程序开发中不可或缺的一部分。其中,网格布局就是小程序中常用的一种布局方式,本文将介绍使用PHP开发微信小程序网格布局的实现方法。一、了解网格布局网格布局(GridLayout)是一种基于行和列的布局方式,它可以实现图片、文本、图表等各种元素的对齐

如何使用Vue实现网格布局特效如何使用Vue实现网格布局特效Sep 22, 2023 am 10:24 AM

如何使用Vue实现网格布局特效,需要具体代码示例在现代Web开发中,布局是一个非常重要的部分。而网格布局是一种常见的布局方式,能够使网页呈现出美观的排列效果。Vue作为一种流行的JavaScript框架,提供了便捷的方式来实现网格布局特效。本文将介绍如何使用Vue来实现网格布局特效,并提供代码示例。一、安装Vue和相关依赖库在开始之前,我们需要先安装Vue和

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft