Home>Article>Web Front-end> Introduction to common properties of css grid layout (grid)

Introduction to common properties of css grid layout (grid)

青灯夜游
青灯夜游 forward
2021-01-02 18:04:17 3777browse

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; //为行数/列数的形式,

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: 单个网格项在列轴线的对齐方式