Home  >  Article  >  Web Front-end  >  Introduction to two methods of CSS grid layout (Grid) (with code)

Introduction to two methods of CSS grid layout (Grid) (with code)

不言
不言Original
2018-08-03 15:45:362496browse

CSS Grid layout (Grid) can divide web pages into rows and columns with simple attributes. You can directly use CSS to position and adjust each element in the grid, and there is no need to perform multi-layer embedding in order to achieve a certain layout. Set, in short, css grid layout is very easy to use. Let’s take a look at the content of css grid layout described in this article.

1. CSS Grid Layout (Grid)

CSS Grid layout consists of two core components: parent element and child element. The parent element is the actual grid (grid). Child elements are the content within the grid.
The following is a parent element and six child elements

   <div>
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
    </div>

To turn the parent element into a grid (grid), simply set its display attribute to grid

Effect Picture:

Introduction to two methods of CSS grid layout (Grid) (with code)


The following is the grid layout:

 .box {
            width: 350px;
            height: 350px;
            /* background: #ccc; */
            margin: 0 auto;
            grid-gap: 5px;
            display: grid;
            grid-template-columns: 100px 100px 100px;
            grid-template-rows: 100px 100px 100px;
        }

        .item {
            border: 1px solid black;
            box-sizing: border-box;
        }

        .div1 {
            grid-column-start: 1;
            grid-column-end: 3;
            /*(div1从占据从第一条网格线开始,到第三条网格线结束)*/
            line-height: 100px;
            text-align: center;
            background: rgb(252, 0, 0);
            /* grid-column: 1/3;(这是缩写的形式) */
        }

        .div2 {
            line-height: 100px;
            text-align: center;
            background: rgb(252, 134, 0);
        }

        .div3 {
            grid-row-start: 2;
            grid-row-end: 4;
            /* grid-row: 2/4;(这是缩写的形式) */
            line-height: 200px;
            text-align: center;
            background: rgb(21, 207, 108);
        }

        .div4 {
            grid-column-start: 2;
            grid-column-end: 4;
            line-height: 100px;
            text-align: center;
            background: rgb(18, 21, 189);
            /* grid-column: 2/4;(这是缩写的形式) */
        }

        .div5 {
            line-height: 100px;
            text-align: center;
            background: rgb(16, 170, 197);
        }

        .div6 {
            line-height: 100px;
            text-align: center;
            background: rgb(172, 126, 199);
        }

The grid lines in the above code (arrows in the figure) The place pointed to is a grid line):

Introduction to two methods of CSS grid layout (Grid) (with code)

2. Responsive grid layout

Similar to the above (with the following content added)
Use the grid-template-columns property to create a 12-column grid, each column is a unit width (1/12 of the total width)
Create 3 rows using the grid-template-rows property

Use the grid-gap property to add a gap between grid items in the grid.

The code is as follows:

       <div class="container">
        <div class="header">顶部(一个点表示一个空白的格子),所以距离左边和右边各有一个格子的距离。</div>
        <div class="menu">中间1</div>
        <div class="content">中间2所以可以利用空白的格子来对你所要拍的网页来进行布局</div>
        <div class="footer">底部(一个点表示一个空白的格子),所以距离右边有三个格子的距离。</div>
    </div>

Add grid-template-areas
This attribute is called the grid area, also called the template area, which allows us to easily conduct layout experiments.
Rendering:
Introduction to two methods of CSS grid layout (Grid) (with code)

##The code is as follows:

        .container {
            display: grid;
            grid-template-columns: repeat(12, 1fr);
            grid-template-rows: 50px 350px 50px;
            grid-gap: 5px;
            grid-template-areas: ". h h h h h h h h h h ." " m m c c c c c c c c c c" "f f f f f f f f f . . .";
        }

        .container>p {
            border: 1px solid #ccc;
            box-sizing: border-box;
        }

        .header {
            text-align: center;
            line-height:50px;
            grid-area: h;
            color:rgb(219, 52, 169);
        }

        .menu {
            grid-area: m;
            text-align: center;
            line-height:350px;
        }

        .content {
            text-align: center;
            line-height:350px;
            grid-area: c;
            color:rgb(25, 158, 69);
        }

        .footer {
            color:rgb(212, 112, 18);
            text-align: center;
            line-height:50px;
            grid-area: f;
        }
Recommended related articles:

Detailed explanation of Grid layout in CSS

Basic knowledge of CSS3 grid layout

The above is the detailed content of Introduction to two methods of CSS grid layout (Grid) (with code). 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