Home Web Front-end CSS Tutorial Introduction to common properties of css grid layout (grid)

Introduction to common properties of css grid layout (grid)

Jan 02, 2021 pm 06:04 PM
css grid grid layout

This article will introduce to you the common properties of grid layout (grid). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Introduction to common properties of css grid layout (grid)

Tutorial recommendation: css video tutorial

Different from flex layout, grid layout (grid) is a two-dimensional layout, you can create any row or column layout.

First of all, let’s introduce a few concepts;

Imagine a layout with three rows and three columns. The grid lines are all the lines that make up the grid, three rows and three columns. The layout will have 4 grid lines per row.

The grid track is the part between two adjacent parallel grid lines.

Like flex layout, it will have parent containers and sub-items, here we call them grid containers and grid items.

Next, we will introduce the grid layout from the grid container to the basic properties of the grid items.

Introduction to common properties of css grid layout (grid)

##Grid container

The grid container determines how many times the grid is divided into Rows and columns, so first to implement grid layout, the container must have the following attributes:

display: grid;

grid-template-columns

grid-template-rows

grid-gap

grid-template-areas

grid-auto-flow: dense | row(default) | column

justify-items: start|end|center|stretch(default)

align-items:

start|end|center|stretch(default)

grid- auto-columns:

The basic properties of the parent container of grid layout are the above.

  • display: grid

is expressed as grid layout, which has the same meaning as display: flex in flex layout. This attribute can also take values: inline-grid and subgrid

  • ##grid-template-columns

  • Create network In addition to the percentage form, the number of columns in the grid also supports various combinations of units, such as grid-template-columns: 100px 20% 1em 1vw 20%;

But there is a repeat function that can simplify the same The value, such as grid-template-columns: repeat(5, 20%), represents five widths of 20%, which has exactly the same meaning as in the example.

fr is used to divide the remaining space equally. Its size is the size of the remaining space after removing all the calculable values ​​​​(including various units and percentages) on the attribute.

It is recommended to use fr. It will also automatically calculate the rest except grid-gap.

For example, grid-template-columns: 100px 1fr 2fr repeat(2, 20%). For the same 5-column layout, 1fr means that the width is the total width minus 100px on the left and 20% of the two columns on the right, divided by three. That is, the width of the second column will be half that of the third column.

  • grid-template-rows

  • The attribute value is exactly the same as the attribute value of grid-template-columns.

  • grid-template

  • is the abbreviation of grid-template-rows and grid-template-columns, attributes The value is written as
grid-template: 1fr 50px/1fr 4fr; //为行数/列数的形式,
Copy after login

This code represents a layout of two rows and two columns. The height of the first row is the remaining height after determining the 50px of the bottom row. The width of the first column divides the container into five equal parts, with the first column occupying one part and the second column occupying four parts.

  • grid-gap

  • can take one or two values ​​to represent the gap between rows and columns.

  • grid-template-areas

  • The grid area (Grid Area) specified by referencing the grid-area attribute Name to define the grid template. Repeating the name of a grid area causes content to span those cells. A dot (.) represents an empty grid cell. This syntax itself can be thought of as the visual structure of a grid.

  • grid-template-areas:

"header header header header"

"main main . sidebar"

"footer footer footer footer";

Introduction to common properties of css grid layout (grid)

    ##grid-auto -flow:
  • row: tells the automatic layout algorithm to fill each row in turn, adding new rows as needed
    • column: tells automatic The layout algorithm fills in each column in turn, adding new columns as needed
    • dense:告诉自动布局算法在稍后出现较小的网格项时,尝试填充网格中较早的空缺

  • justify-items

沿着 行轴线(row axis) 对齐 网格项(grid items) 内的内容

  • align-items

沿着 列轴线(row axis) 对齐 网格项(grid items) 内的内容

  • grid-auto-columns:

隐式网格的宽度

  • grid-auto-rows:

隐式网格的高度

网格项

网格项表示网格内部的直接子元素,不包括子元素的子元素。

常用属性:

  • grid-column-start: 列网格线 开始,

  • grid-column-end: 列网格线 结束

  • grid-column: start/end | start/span count

  • order: 与z-index的属性相同,表示层叠的位置。

  • grid-area: 网格名,在使用grid-template-areas时比较有用。

  • justify-self: 单个网格项在行轴线的对齐方式

  • align-self: 单个网格项在列轴线的对齐方式

<div class=&#39;container&#39;>
        <div class=&#39;child&#39;></div>
    </div>
    <style>
    .container {
        display: grid;
        width: 580px;
        height: 580px;
        grid-gap: 3px;
        grid-template-rows: repeat(5, 1fr);
        grid-template-columns: repeat(5, 1fr);
        background-color: blanchedalmond;
    }
    .child {
        grid-column-start: 6;
        grid-column-end: 3;
        grid-row-start: 1;
        grid-row-end: 5;
        background: url(./babar.png);
        background-size: 116px 116px;
        order:1;
    }
Copy after login

Introduction to common properties of css grid layout (grid)

在审查元素的时候毫不意外的发现该布局为五行五列的布局,上述的child中前四行代码属性的值都是以网格线的顺序为基准,前两行代码表示第六列网格线开始,到第三列网格线结束,即表示后三列。其中的start不一定要比end小。第三四行代码表示第一个网格线开始,第五个网格线结束,即表示前四行,四行代码综合起来就如图所示。 

当然,child中的前四行代码也可以简写成这样:

grid-row: 1/ 5;

grid-column: 6/ 3;

或者

grid-row: 1/ span 4; // 横向第一个网格线开始,以下的4个网格轨道

grid-column: 3/span 3; // 竖向第三条网格线开始,以后的3个网格轨道

甚至简写成这样

grid-area: 1/3/ span 4/span 3; 

更多编程相关知识,请访问:编程教学!!

The above is the detailed content of Introduction to common properties of css grid layout (grid). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

See all articles