88.CSS---Grid 网格布局教程

grid 兼容性查看请点此处 最新Grid兼容

grid 布局就是给父元素(容器)添加display:grid,然后使子元素(项目)改变布局,

1 2 3 4 5 6 7 8 9

上面九个正方形的代码如下:没有给正方形设定宽度,是为了方便观察css效果

.seven{

background:#f1c40f;

line-height:100px;

color:#fff;

font-size: 40px;

text-align: center;

}

1.grid-template-columns 属性 | grid-template-rows 属性

grid-template-columns 定义每一列的列宽

grid-template-rows 定义每一行的行高

单位有:fr(分数)、%、px

.section-one-1{

width:400px;

display: grid;

grid-template-columns: 1fr 1fr 1fr;

grid-template-rows: 1fr 1fr 1fr;

}

把九个宫格分成了三行三列,每一个项目的空间占1/3*1/3=1/9

123456789

如果容器的宽度没有限定,就会以容器最大的宽度来等比例排列

.section-one-2{

display: grid;

grid-template-columns: 1fr 1fr 1fr;

grid-template-rows: 1fr 1fr 1fr;

}

123456789

2.如果要使容器内项目居中,则可使用justify-items属性|align-items 属性|place-items 属性

justify-items属性设置单元格内容的水平位置(左中右 默认)start | center | end | stretch;

align-items属性设置单元格内容的垂直位置(上中下 默认)start | center | end | stretch;

place-items属性是align-items属性和justify-items 属性的合并简写形式。

place-items: align-items(start) justify-items(end)

当容器宽度大于项目的集合宽度,当容器高度大于项目的集合高度,项目会自动居中在每两列之间,如下代码

.section-two-1 {

display: grid;

grid-template-columns: repeat(3,1fr);

grid-template-rows: repeat(3,1fr);

place-items: center center;

height:500px;

}

123456789

3.grid-row-gap 属性|grid-column-gap 属性|grid-gap 属性

grid-row-gap 属性设置行与行的间隔(行间距)

grid-column-gap 属性设置列与列的间隔(列间距)

grid-gap属性是grid-row-gapgrid-column-gap的合并简写形式

grid-gap: grid-row-gap grid-column-gap

.section-three-1{

display: grid;

grid-template-columns: repeat(3,1fr);

grid-template-rows: repeat(3,1fr);

grid-row-gap: 20px;

grid-column-gap: 30px;

}

123456789

4.grid-template-areas属性 |grid-area属性

网格布局允许指定"区域"(area),一个区域由单个或多个单元格组成。grid-template-areas属性用于定义区域

这个属性需要搭配项目(儿子)的grid-area属性使用

grid-area属性指定项目放在哪一个区域。

.section-four-1{

display: grid;

grid-template-columns: 150px 150px 150px;

grid-template-rows:150px 150px 150px;

grid-template-areas:"header header header"

"header header header"

"two three four";

}

.section-four-1>span:first-child{

grid-area: header;

}

.section-four-1>span:nth-child(2){

grid-area: two;

}

.section-four-1>span:nth-child(3){

grid-area: three;

}

.section-four-1>span:last-child{

grid-area: four;

}

234

5.repeat() 函数

repeat 函数作用在可以把重复的简写,代码如下。第一个参数为重复的次数,第二个参数是重复的数值

.section-five-1{

width:500px;

display: grid;

grid-template-columns: repeat(3,1fr);

grid-template-rows: repeat(3,1fr);

}

123456789

repeat()重复某种模式也是可以的。

如下图定义了6列,第一列和第四列的宽度为100px,第二列和第五列为220px,第三列和第六列为1fr。

.section-five-2{

display: grid;

grid-template-columns:repeat(2,100px 220px 1fr);

grid-template-rows: repeat(3,1fr);

}

123456789

6.minmax(min,max) 函数

minmax(min,max) 函数产生一个长度范围,表示长度就在这个范围之中。它接受两个参数,分别为最小值和最大值。

.section-six-1{

display: grid;

grid-template-columns:1fr 1fr minmax(50px, 100px);

grid-template-rows: 1fr 1fr minmax(50px, 150px);

}

123456789

7.grid-auto-flow 属性

grid-auto-flow属性决定,默认值是row,即"先行后列"。也可以将它设成column,变成"先列后行"。

.section-seven-1{

display: grid;

grid-template-columns:repeat(3,1fr);

grid-template-rows:repeat(3,1fr);

grid-auto-flow: column;

}

123456789

8.justify-content属性|align-content属性|place-content属性

justify-content属性是整个内容区域在容器里面的水平位置(左中右)start | end | center | stretch | space-around | space-between | space-evenly;

align-content属性是整个内容区域的垂直位置(上中下)start | end | center | stretch | space-around | space-between | space-evenly;

place-content属性:align-content justify-content

justify-content属性值只能体现在grid-template-columns中某一个值为auto才会有效果。如下

.section-eight-1{

display: grid;

grid-template-columns:150px auto 150px;

grid-template-rows:repeat(3,1fr);

place-items: center center;

height: 500px;

justify-content: start;

}

123456789

justify-content:stretch;恢复默认情况。如下

.section-eight-2{

display: grid;

grid-template-columns:150px auto 150px;

grid-template-rows:repeat(3,1fr);

place-items: center center;

height: 500px;

justify-content:stretch;

}

123456789

justify-content:space-around;每个项目两侧的间隔相等。所以,项目之间的间隔比项目与容器边框的间隔大一倍。如下

.section-eight-3{

display: grid;

grid-template-columns:150px auto 150px;

grid-template-rows:repeat(3,1fr);

place-items: center center;

height: 500px;

justify-content:space-around;

}

123456789

justify-content:space-between;项目与项目的间隔相等,项目与容器边框之间没有间隔。如下

.section-eight-4{

display: grid;

grid-template-columns:150px auto 150px;

grid-template-rows:repeat(3,1fr);

place-items: center center;

height: 500px;

justify-content:space-between;

}

123456789

justify-content:space-evenly;项目与项目的间隔相等,项目与容器边框之间也是同样长度的间隔。如下

.section-eight-5{

display: grid;

grid-template-columns:150px auto 150px;

grid-template-rows:repeat(3,1fr);

place-items: center center;

height: 500px;

justify-content:space-evenly;

}

123456789

align-content属性和justify-content属性是一样的

9. grid-column-start 属性 | grid-column-end 属性

grid-row-start 属性 | grid-row-end 属性

这些属性定义在项目上面.

grid-column-start属性:左边框所在的垂直网格线

grid-column-end属性:右边框所在的垂直网格线

grid-row-start属性:上边框所在的水平网格线

grid-row-end属性:下边框所在的水平网格线

.section-nine-1{

width:400px;

display:grid;

grid-template-columns: repeat(3,1fr);

grid-template-rows: repeat(3,1fr);

}

.section-nine-1>span:first-child{

grid-column-start: 2;

grid-column-end: 4;

}

1号项目的左边框是第二根垂直网格线,右边框是第四根垂直网格线。如下

123456789

上图中,只指定了1号项目的左右边框,没有指定上下边框,所以会采用默认位置,即上边框是第一根水平网格线,下边框是第二根水平网格线。

下面的例子是指定四个边框位置的效果。

1号项目的左边框是第1根垂直网格线,右边框是第3根垂直网格线。上边框是第2根水平网格线,下边框是第4根水平垂直线。如下

.section-nine-2{

width:400px;

display:grid;

grid-template-columns: repeat(3,1fr);

grid-template-rows: repeat(3,1fr);

}

.section-nine-2>span:first-child{

grid-column-start: 1;

grid-column-end: 3;

grid-row-start: 2;

grid-row-end: 4;

}

123456789

这四个属性的值还可以使用span关键字,表示"跨越",即左右边框(上下边框)之间跨越多少个网格。

上面代码表示,1号项目的左边框距离右边框跨越3个网格。上边框距离下边框跨越2个网格

.section-nine-3{

width:400px;

display:grid;

grid-template-columns: repeat(3,1fr);

grid-template-rows: repeat(3,1fr);

}

.section-nine-3>span:first-child{

grid-column-start:span 3;

grid-row-start:span 2;

}

123456789

grid-column 属性 | grid-row 属性

grid-column属性是grid-column-startgrid-column-end的合并简写形式

grid-row属性是grid-row-start属性和grid-row-end的合并简写形式。

.section-nine-3>span:first-child{

grid-column-start:1/3;

grid-row:2/4;

}

.section-nine-3>span:first-child{

grid-column-start: 1;

grid-column-end: 3;

grid-row-start: 2;

grid-row-end: 4;

}

10.justify-self 属性 | align-self 属性 | place-self 属性

justify-self属性设置单元格内容的水平位置(左中右),跟justify-items属性的用法完全一致,但只作用于单个项目。

justify-self属性设置单元格内容的垂直位置(上中下),跟align-items属性的用法完全一致,也是只作用于单个项目。

place-self属性是align-self属性和justify-self属性的合并简写形式。

如果省略第二个值,place-self属性会认为这两个值相等。

place-self: (align-self) (justify-self);

两个属性都可以取四个值。start:对齐单元格的起始边缘。 end:对齐单元格的结束边缘。

center:单元格内部居中。stretch:拉伸,占满单元格的整个宽度(默认值)。

.section-ten-1{

width:400px;

display:grid;

grid-template-columns: repeat(3,1fr);

grid-template-rows: repeat(3,1fr);

}

.section-ten-1>span:first-child{

justify-self: start;

}

123456789

附上原css代码

header{
    margin:20px 0;
    text-align: center;
    font-size: 30px;
    color:#2ecc71;
}
.section-1{
    margin:20px 0;

}
.one{
    /*width:100px;*/
   /*height:100px;*/
    background: #1abc9c;
    color:#fff;
    font-size: 40px;
    text-align: center;
    line-height:100px;
}
.two{
   /*width:100px;*/
   /*height:100px;*/
    background: #2ecc71;
    color:#fff;
    font-size: 40px;
    text-align: center;
    line-height:100px;
}
.three{
   /*width:100px;*/
   /*height:100px;*/
    background: #3498db;
    color:#fff;
    font-size: 40px;
    text-align: center;
    line-height:100px;
}
.four{
   /*width:100px;*/
   /*height:100px;*/
    background: #9b59b6;
    color:#fff;
    font-size: 40px;
    text-align: center;
    line-height:100px;
}
.five{
   /*width:100px;*/
   /*height:100px;*/
    background:#34495e;
    color:#fff;
    font-size: 40px;
    text-align: center;
    line-height:100px;
}
.six{
   /*width:100px;*/
   /*height:100px;*/
    background:#ff9966;
    color:#fff;
    font-size: 40px;
    text-align: center;
    line-height:100px;
}
.seven{
    /*width:100px;*/
    /*height:100px;*/
     background:#f1c40f;
    color:#fff;
    font-size: 40px;
    text-align: center;
    line-height:100px;
 }
.eight{
   /*width:100px;*/
   /*height:100px;*/
    background: #e67e22;
    color:#fff;
    font-size: 40px;
    text-align: center;
    line-height:100px;
}
.nine{
   /*width:100px;*/
   /*height:100px;*/
    background: #e74c3c;
    color:#fff;
    font-size: 40px;
    text-align: center;
    line-height:100px;
}

.section-one-1{
     width:400px;
     display:grid;
     grid-template-columns: 1fr 1fr 1fr;
     grid-template-rows: 1fr 1fr 1fr;
 }
.section-one-2{
    display:grid;
    grid-template-columns:1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;

}

.section-two-1 {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    place-items: center center;
    height:500px;
}
.section-three-1{
    display: grid;
    grid-template-columns: repeat(3,1fr);
    grid-template-rows: repeat(3,1fr);
    grid-row-gap: 20px;
    grid-column-gap: 30px;
}
.section-four-1{
    display: grid;
    grid-template-columns: 150px 150px 150px;
    grid-template-rows: 150px 150px 150px;
    grid-template-areas:"header header header"
                        "header header header"
                        "two three four";
}
.section-four-1>span:first-child{
    grid-area: header;
}
.section-four-1>span:nth-child(2){
    grid-area: two;
}
.section-four-1>span:nth-child(3){
    grid-area: three;
}
.section-four-1>span:last-child{
    grid-area: four;
}
.section-five-1{
    width:500px;
    display:grid;
    grid-template-columns: repeat(3,1fr);
    grid-template-rows: repeat(3,1fr);
 }
.section-five-2{
     display: grid;
     grid-template-columns: repeat(2,100px 220px 1fr);
     grid-template-rows: repeat(3,1fr);
 }
.section-six-1{
    display: grid;
    grid-template-columns:1fr 1fr minmax(50px, 100px);
    grid-template-rows:1fr 1fr minmax(50px, 150px);
}
.section-seven-1{
    display: grid;
    grid-template-columns:repeat(3,1fr);
    grid-template-rows:repeat(3,1fr);
    grid-auto-flow: column;
}
.section-eight-1{
    display: grid;
    grid-template-columns: 150px auto 150px;
    grid-template-rows: repeat(3, 1fr);
    place-items: center center;
    height: 500px;
    justify-content: start;
}
.section-eight-2{
    display: grid;
    grid-template-columns: 150px auto 150px;
    grid-template-rows: repeat(3, 1fr);
    place-items: center center;
    height: 500px;
    justify-content: stretch;
}
.section-eight-3{
    display: grid;
    grid-template-columns: 150px auto 150px;
    grid-template-rows: repeat(3, 1fr);
    place-items: center center;
    height: 500px;
    justify-content: space-around;
}
.section-eight-4{
    display: grid;
    grid-template-columns: 150px auto 150px;
    grid-template-rows: repeat(3, 1fr);
    place-items: center center;
    height: 500px;
    justify-content: space-between;
}

.section-eight-5{
    display: grid;
    grid-template-columns: 150px auto 150px;
    grid-template-rows: repeat(3, 1fr);
    place-items: center center;
    height: 500px;
    justify-content: space-evenly;
}
.section-nine-1{
    width:400px;
    display:grid;
    grid-template-columns: repeat(3,1fr);
    grid-template-rows: repeat(3,1fr);
}
.section-nine-1>span:first-child{
    grid-column-start: 2;
    grid-column-end: 4;
}
.section-nine-2{
    width:400px;
    display:grid;
    grid-template-columns: repeat(3,1fr);
    grid-template-rows: repeat(3,1fr);
}
.section-nine-2>span:first-child{
    grid-column-start: 1;
    grid-column-end: 3;
    grid-row-start: 2;
    grid-row-end: 4;
}
.section-nine-3{
     width:400px;
     display:grid;
     grid-template-columns: repeat(3,1fr);
     grid-template-rows: repeat(3,1fr);
 }
.section-nine-3>span:first-child{
    grid-column-start: span 3;
    grid-row-start: span 2;
}
.section-ten-1{
    width:400px;
    display:grid;
    grid-template-columns: repeat(3,1fr);
    grid-template-rows: repeat(3,1fr);
}
.section-ten-1>span:first-child{
    justify-self: start;
}

*{
    margin:0;
    padding:0;
    font-family: ‘幼圆‘;
}
::selection{
    color:#fff;
    background: #1abc9c;
}
html::-webkit-scrollbar {
    width: 13px;
    height: 13px;
}
html::-webkit-scrollbar-thumb {
    background: -webkit-gradient(linear,left top,left bottom,from(#2ecc71),to(#27ae60));
    background: linear-gradient(to bottom,#2ecc71,#27ae60);
    border-radius: 30px;
    -webkit-box-shadow: inset 2px 2px 2px rgba(255,255,255,.25), inset -2px -2px 2px rgba(0,0,0,.25);
    box-shadow: inset 2px 2px 2px rgba(255,255,255,.25), inset -2px -2px 2px rgba(0,0,0,.25);
}
html::-webkit-scrollbar-track {
    background: linear-gradient(to right,#95a5a6,#95a5a6 1px,#7f8c8d 1px,#7f8c8d);
}
.container-1200{
    width:1200px;
    margin:0 auto;
}
h1{
    color:#2ecc71;
    text-align: center;
}
li{
    list-style: none;
}
span{
    display: inline-block;
}
code{
    display: inline-block;
    padding-left: 5px;
    padding-right: 5px;
    font-size: 120%;
    background-color: pink;
    border-radius: 5px;
    margin: auto 3px;
    color:#111;
}
blockquote{
    background-color: #f5f2f0;
    padding: 1em;
    margin: 2em 2em;
    -moz-border-radius: 1em;
    -webkit-border-radius: 1em;
    border-radius: 1em;
    border-width: 0.3em;
    border-color: #e0dfcc;
    border-style: solid;
    text-shadow: 0 1px white;
    overflow: auto;
}
blockquote div{
    width:39%;
    float: left;
}
blockquote img{
    width:29%;
    float: left;
}
b{
    color:red;
    font-size: 20px;
}
p{
    /*font-family: ‘微软雅黑‘;*/
    margin:10px 0;
    font-weight:600;
    font-size: 16px;
}

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>CSS Grid 网格布局教程</title>
    <link rel="stylesheet" href="/css/public.css">
    <link rel="stylesheet" href="/css/grid.css">
</head>
<body class="container-1200">
<section>
    <header>CSS Grid 网格布局教程</header>
    <h2>grid 兼容性查看请点此处 <a style="color:red;font-weight:800;" href="https://caniuse.com/#search=grid">最新Grid兼容</a></h2>
    <p>grid 布局就是给父元素(容器)添加<code>display:grid</code>,然后使子元素(项目)改变布局,</p>
    <span class="one">1</span>
    <span class="two">2</span>
    <span class="three">3</span>
    <span class="four">4</span>
    <span class="five">5</span>
    <span class="six">6</span>
    <span class="seven">7</span>
    <span class="eight">8</span>
    <span class="nine">9</span>
</section>
<p>上面九个正方形的代码如下:没有给正方形设定宽度,是为了方便观察css效果</p>
<blockquote>
    <p>.seven{</p>
    <p> background:#f1c40f;</p>
    <p>line-height:100px;</p>
    <p>color:#fff;</p>
    <p>font-size: 40px;</p>
    <p> text-align: center;</p>
    <p>}</p>
</blockquote>
<section class="section-1">
    <p><h1>1.<code>grid-template-columns</code> 属性 | <code>grid-template-rows</code> 属性</h1></p>
    <p><code>grid-template-columns</code> 定义每一列的列宽</p>
    <p><code>grid-template-rows</code> 定义每一行的行高</p>
    <p>单位有:fr(分数)、%、px</p>
    <blockquote>
        <div>
            <p>.section-one-1{</p>
            <p>width:400px;</p>
            <p>display: grid;</p>
            <p>grid-template-columns: 1fr 1fr 1fr;</p>
            <p>grid-template-rows: 1fr 1fr 1fr;</p>
            <p>}</p>
        </div>
        <img src="/image/grid/1.png" >
    </blockquote><p>把九个宫格分成了三行三列,每一个项目的空间占1/3*1/3=1/9</p>
    <div class="section-one-1">
        <span class="one">1</span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
        <span class="five">5</span>
        <span class="six">6</span>
        <span class="seven">7</span>
        <span class="eight">8</span>
        <span class="nine">9</span>
    </div>
    <p>如果容器的宽度没有限定,就会以容器最大的宽度来等比例排列</p>
    <blockquote>
        <div>
            <p>.section-one-2{</p>
            <p>display: grid;</p>
            <p>grid-template-columns: 1fr 1fr 1fr;</p>
            <p>grid-template-rows: 1fr 1fr 1fr;</p>
            <p>}</p>
        </div>
        <img style="width:50%;" src="/image/grid/2.png" >
    </blockquote>
    <div class="section-one-2">
        <span class="one">1</span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
        <span class="five">5</span>
        <span class="six">6</span>
        <span class="seven">7</span>
        <span class="eight">8</span>
        <span class="nine">9</span>
    </div>
</section>

<section class="section-1">
    <p><h1>2.如果要使容器内项目居中,则可使用<code>justify-items</code>属性|<code>align-items</code> 属性|<code>place-items</code> 属性</h1></p>
    <p><code>justify-items</code>属性设置单元格内容的水平位置(左中右 默认)<b>start | center | end | stretch</b>;</p>
    <p><code>align-items</code>属性设置单元格内容的垂直位置(上中下 默认)<b>start | center | end | stretch</b>;</p>
    <p><code>place-items</code>属性是<code>align-items</code>属性和<code>justify-items</code> 属性的合并简写形式。</p>
    <blockquote>
        <p>place-items: align-items(start) justify-items(end)</p>
    </blockquote>
    <p>当容器宽度大于项目的集合宽度,当容器高度大于项目的集合高度,项目会自动居中在每两列之间,如下代码</p>
    <blockquote>
        <div>
            <p>.section-two-1 {</p>
            <p>display: grid;</p>
            <p>grid-template-columns: repeat(3,1fr);</p>
            <p>grid-template-rows: repeat(3,1fr);</p>
            <p>place-items: center center;</p>
            <p>height:500px;</p>
            <p>}</p>
        </div>
        <img style="width:50%;" src="/image/grid/3.png" >
    </blockquote>
    <div class="section-two-1">
        <span class="one">1</span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
        <span class="five">5</span>
        <span class="six">6</span>
        <span class="seven">7</span>
        <span class="eight">8</span>
        <span class="nine">9</span>
    </div>
</section>
<section class="section-1">
    <p><h1>3.<code>grid-row-gap</code> 属性|<code>grid-column-gap</code> 属性|<code>grid-gap</code> 属性</h1></p>
    <p><code>grid-row-gap</code> 属性设置行与行的间隔(行间距)</p>
    <p><code>grid-column-gap</code> 属性设置列与列的间隔(列间距)</p>
    <p><code>grid-gap</code>属性是<code>grid-row-gap</code>和<code>grid-column-gap</code>的合并简写形式</p>
    <blockquote>
        <p>grid-gap: grid-row-gap  grid-column-gap</p>
    </blockquote>
    <blockquote>
        <div>
            <p>.section-three-1{</p>
            <p>display: grid;</p>
            <p>grid-template-columns: repeat(3,1fr);</p>
            <p>grid-template-rows: repeat(3,1fr);</p>
            <p>grid-row-gap: 20px;</p>
            <p> grid-column-gap: 30px;</p>
            <p>}</p>
        </div>
        <img style="width:61%;" src="/image/grid/4.png" >
    </blockquote>
    <div class="section-three-1">
        <span class="one">1</span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
        <span class="five">5</span>
        <span class="six">6</span>
        <span class="seven">7</span>
        <span class="eight">8</span>
        <span class="nine">9</span>
    </div>
</section>
<section class="section-1">
    <h1>4.<code>grid-template-areas</code>属性 |<code>grid-area</code>属性</h1>
    <p>网格布局允许指定"区域"(area),一个区域由单个或多个单元格组成。<code>grid-template-areas</code>属性用于定义区域</p>
    <p>这个属性需要搭配项目(儿子)的<code>grid-area</code>属性使用</p>
    <p><code>grid-area</code>属性指定项目放在哪一个区域。</p>
    <blockquote>
        <div>
            <p>.section-four-1{</p>
            <p>display: grid;</p>
            <p>grid-template-columns: 150px 150px 150px;</p>
            <p>grid-template-rows:150px 150px 150px;</p>
            <p><code>grid-template-areas</code>:"header header header"</p>
            <p>"header header header"</p>
             <p>"two three four";</p>
            <p>}</p>
            <p>.section-four-1>span:first-child{</p>
            <p><code>grid-area</code>: header;</p>
            <p>}</p>
            <p>.section-four-1>span:nth-child(2){</p>
            <p><code>grid-area</code>: two;</p>
            <p>}</p>
            <p>.section-four-1>span:nth-child(3){</p>
            <p><code>grid-area</code>: three;</p>
            <p>}</p>
            <p>.section-four-1>span:last-child{</p>
            <p><code>grid-area</code>: four;</p>
            <p>}</p>
        </div>
        <img style="width:50%;" src="/image/grid/5.png" >
    </blockquote>
    <div class="section-four-1">
        <span class="header"><img style="width:450px;height:300px" src="/image/grid/four-1.jpg" ></span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
    </div>
</section>
<section class="section-1">
    <h1>5.<code>repeat()</code> 函数</h1>
    <p>repeat 函数作用在可以把重复的简写,代码如下。第一个参数为重复的次数,第二个参数是重复的数值</p>
    <blockquote>
        <div>
            <p>.section-five-1{</p>
            <p>width:500px;</p>
            <p>display: grid;</p>
            <p>grid-template-columns: repeat(3,1fr);</p>
            <p>grid-template-rows: repeat(3,1fr);</p>
            <p>}</p></div>
        <img src="/image/grid/6.png" >
    </blockquote>
    <div class="section-five-1">
        <span class="one">1</span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
        <span class="five">5</span>
        <span class="six">6</span>
        <span class="seven">7</span>
        <span class="eight">8</span>
        <span class="nine">9</span>
    </div>

    <p><code>repeat()</code>重复某种模式也是可以的。</p>
    <p>如下图定义了6列,第一列和第四列的宽度为100px,第二列和第五列为220px,第三列和第六列为1fr。</p>
    <blockquote>
        <div>
            <p>.section-five-2{</p>
            <p>display: grid;</p>
            <p>grid-template-columns:repeat(2,100px 220px 1fr);</p>
            <p>grid-template-rows: repeat(3,1fr);</p>
            <p>}</p>
        </div>
        <img style="width:50%;" src="/image/grid/7.png" >
    </blockquote>
    <div class="section-five-2">
        <span class="one">1</span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
        <span class="five">5</span>
        <span class="six">6</span>
        <span class="seven">7</span>
        <span class="eight">8</span>
        <span class="nine">9</span>
    </div>
</section>
<section class="section-1">
    <h1>6.<code>minmax(min,max)</code> 函数</h1>
    <p><code>minmax(min,max)</code> 函数产生一个长度范围,表示长度就在这个范围之中。它接受两个参数,分别为最小值和最大值。</p>
    <blockquote>
        <div>
            <p>.section-six-1{</p>
            <p>display: grid;</p>
            <p>grid-template-columns:1fr 1fr minmax(50px, 100px);</p>
            <p>grid-template-rows: 1fr 1fr minmax(50px, 150px);</p>
            <p>}</p>
        </div>
        <img style="width:60%;" src="/image/grid/8.png" >
    </blockquote>
    <div class="section-six-1">
        <span class="one">1</span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
        <span class="five">5</span>
        <span class="six">6</span>
        <span class="seven">7</span>
        <span class="eight">8</span>
        <span class="nine">9</span>
    </div>
</section>
<section class="section-1">
    <h1>7.<code>grid-auto-flow</code> 属性</h1>
    <p><code>grid-auto-flow</code>属性决定,默认值是row,即"先行后列"。也可以将它设成column,变成"先列后行"。</p>
    <blockquote>
        <div>
            <p>.section-seven-1{</p>
            <p>display: grid;</p>
            <p>grid-template-columns:repeat(3,1fr);</p>
            <p>grid-template-rows:repeat(3,1fr);</p>
            <p>grid-auto-flow: column;</p>
            <p>}</p>
        </div>
    </blockquote>
    <div class="section-seven-1">
        <span class="one">1</span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
        <span class="five">5</span>
        <span class="six">6</span>
        <span class="seven">7</span>
        <span class="eight">8</span>
        <span class="nine">9</span>
    </div>
</section>
<section class="section-1">
    <h1>8.<code>justify-content</code>属性|<code>align-content</code>属性|<code>place-content</code>属性</h1>
    <p><code>justify-content</code>属性是整个内容区域在容器里面的水平位置(左中右)start | end | center | stretch | space-around | space-between | space-evenly;</p>
    <p><code>align-content</code>属性是整个内容区域的垂直位置(上中下)start | end | center | stretch | space-around | space-between | space-evenly;</p>
    <p><code>place-content</code>属性:align-content justify-content</p>
    <p><code>justify-content</code>属性值只能体现在<code>grid-template-columns</code>中某一个值为auto才会有效果。如下</p>
    <blockquote>
        <div>
            <p>.section-eight-1{</p>
            <p>display: grid;</p>
            <p>grid-template-columns:150px auto 150px;</p>
            <p>grid-template-rows:repeat(3,1fr);</p>
            <p>place-items: center center;</p>
            <p>height: 500px;</p>
            <p>justify-content: start;</p>
            <p>}</p>
        </div>
        <img style="width:20%;" src="/image/grid/9.png" >
    </blockquote>
    <div class="section-eight-1">
        <span class="one">1</span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
        <span class="five">5</span>
        <span class="six">6</span>
        <span class="seven">7</span>
        <span class="eight">8</span>
        <span class="nine">9</span>
    </div>
    <p><code>justify-content:stretch;</code>恢复默认情况。如下</p>
    <blockquote>
        <div>
            <p>.section-eight-2{</p>
            <p>display: grid;</p>
            <p>grid-template-columns:150px auto 150px;</p>
            <p>grid-template-rows:repeat(3,1fr);</p>
            <p>place-items: center center;</p>
            <p>height: 500px;</p>
            <p>justify-content:stretch;</p>
            <p>}</p>
        </div>
        <img style="width:50%;" src="/image/grid/10.png" >
    </blockquote>
    <div class="section-eight-2">
        <span class="one">1</span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
        <span class="five">5</span>
        <span class="six">6</span>
        <span class="seven">7</span>
        <span class="eight">8</span>
        <span class="nine">9</span>
    </div>
    <p><code>justify-content:space-around;</code>每个项目两侧的间隔相等。所以,项目之间的间隔比项目与容器边框的间隔大一倍。如下</p>
    <blockquote>
        <div>
            <p>.section-eight-3{</p>
            <p>display: grid;</p>
            <p>grid-template-columns:150px auto 150px;</p>
            <p>grid-template-rows:repeat(3,1fr);</p>
            <p>place-items: center center;</p>
            <p>height: 500px;</p>
            <p>justify-content:space-around;</p>
            <p>}</p>
        </div>
        <img style="width:50%;" src="/image/grid/11.png" >
    </blockquote>
    <div class="section-eight-3">
        <span class="one">1</span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
        <span class="five">5</span>
        <span class="six">6</span>
        <span class="seven">7</span>
        <span class="eight">8</span>
        <span class="nine">9</span>
    </div>
    <p><code>justify-content:space-between;</code>项目与项目的间隔相等,项目与容器边框之间没有间隔。如下</p>
    <blockquote>
        <div>
            <p>.section-eight-4{</p>
            <p>display: grid;</p>
            <p>grid-template-columns:150px auto 150px;</p>
            <p>grid-template-rows:repeat(3,1fr);</p>
            <p>place-items: center center;</p>
            <p>height: 500px;</p>
            <p>justify-content:space-between;</p>
            <p>}</p>
        </div>
        <img style="width:50%;" src="/image/grid/12.png" >
    </blockquote>
    <div class="section-eight-4">
        <span class="one">1</span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
        <span class="five">5</span>
        <span class="six">6</span>
        <span class="seven">7</span>
        <span class="eight">8</span>
        <span class="nine">9</span>
    </div>
    <p><code>justify-content:space-evenly;</code>项目与项目的间隔相等,项目与容器边框之间也是同样长度的间隔。如下</p>
    <blockquote>
        <div>
            <p>.section-eight-5{</p>
            <p>display: grid;</p>
            <p>grid-template-columns:150px auto 150px;</p>
            <p>grid-template-rows:repeat(3,1fr);</p>
            <p>place-items: center center;</p>
            <p>height: 500px;</p>
            <p>justify-content:space-evenly;</p>
            <p>}</p>
        </div>
        <img style="width:50%;" src="/image/grid/13.png" >
    </blockquote>
    <div class="section-eight-5">
        <span class="one">1</span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
        <span class="five">5</span>
        <span class="six">6</span>
        <span class="seven">7</span>
        <span class="eight">8</span>
        <span class="nine">9</span>
    </div>
    <p><code>align-content</code>属性和<code>justify-content</code>属性是一样的</p>
</section>
<section class="section-1">
    <h1>9. <code>grid-column-start</code> 属性 | <code>grid-column-end</code> 属性 </h1>
    <h1><code>grid-row-start</code> 属性 | <code>grid-row-end</code> 属性</h1>
    <p>这些属性定义在项目上面.</p>
    <p><code>grid-column-start</code>属性:左边框所在的垂直网格线</p>
    <p><code>grid-column-end</code>属性:右边框所在的垂直网格线</p>
    <p><code>grid-row-start</code>属性:上边框所在的水平网格线</p>
    <p><code>grid-row-end</code>属性:下边框所在的水平网格线</p>

    <blockquote>
        <div>
            <p>.section-nine-1{</p>
            <p>width:400px;</p>
            <p>display:grid;</p>
            <p>grid-template-columns: repeat(3,1fr);</p>
            <p>grid-template-rows: repeat(3,1fr);</p>
            <p>}</p>
            <p>.section-nine-1>span:first-child{</p>
            <p>grid-column-start: 2;</p>
            <p>grid-column-end: 4;</p>
            <p>}</p>
        </div>
        <img style="width:43%;" src="/image/grid/14.png" >
    </blockquote>
    <p>1号项目的左边框是第二根垂直网格线,右边框是第四根垂直网格线。如下</p>
    <div class="section-nine-1">
        <span class="one">1</span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
        <span class="five">5</span>
        <span class="six">6</span>
        <span class="seven">7</span>
        <span class="eight">8</span>
        <span class="nine">9</span>
    </div>
    <p>上图中,只指定了1号项目的左右边框,没有指定上下边框,所以会采用默认位置,即上边框是第一根水平网格线,下边框是第二根水平网格线。</p>
    <p>下面的例子是指定四个边框位置的效果。</p>
    <p>1号项目的左边框是第1根垂直网格线,右边框是第3根垂直网格线。上边框是第2根水平网格线,下边框是第4根水平垂直线。如下</p>
    <blockquote>
        <div>
            <p>.section-nine-2{</p>
            <p>width:400px;</p>
            <p>display:grid;</p>
            <p>grid-template-columns: repeat(3,1fr);</p>
            <p>grid-template-rows: repeat(3,1fr);</p>
            <p>}</p>
            <p>.section-nine-2>span:first-child{</p>
            <p>grid-column-start: 1;</p>
            <p>grid-column-end: 3;</p>
            <p>grid-row-start: 2;</p>
            <p>grid-row-end: 4;</p>
            <p>}</p>
        </div>
        <img style="width:51%;" src="/image/grid/15.png" >
    </blockquote>
    <div class="section-nine-2">
        <span class="one">1</span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
        <span class="five">5</span>
        <span class="six">6</span>
        <span class="seven">7</span>
        <span class="eight">8</span>
        <span class="nine">9</span>
    </div>
    <p>这四个属性的值还可以使用<code>span</code>关键字,表示"跨越",即左右边框(上下边框)之间跨越多少个网格。</p>
    <p>上面代码表示,1号项目的左边框距离右边框跨越3个网格。上边框距离下边框跨越2个网格</p>
    <blockquote>
        <div>
            <p>.section-nine-3{</p>
            <p>width:400px;</p>
            <p>display:grid;</p>
            <p>grid-template-columns: repeat(3,1fr);</p>
            <p>grid-template-rows: repeat(3,1fr);</p>
            <p>}</p>
            <p>.section-nine-3>span:first-child{</p>
            <p>grid-column-start:span 3;</p>
            <p>grid-row-start:span 2;</p>
            <p>}</p>
        </div>
        <img style="width:31%;" src="/image/grid/16.png" >
    </blockquote>
    <div class="section-nine-3">
        <span class="one">1</span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
        <span class="five">5</span>
        <span class="six">6</span>
        <span class="seven">7</span>
        <span class="eight">8</span>
        <span class="nine">9</span>
    </div>
    <h1><code>grid-column</code> 属性 | <code>grid-row</code> 属性</h1>
    <p><code>grid-column</code>属性是<code>grid-column-start</code>和<code>grid-column-end</code>的合并简写形式</p>
    <p><code>grid-row</code>属性是<code>grid-row-start</code>属性和<code>grid-row-end</code>的合并简写形式。</p>
    <blockquote>
        <div>
            <p>.section-nine-3>span:first-child{</p>
            <p>grid-column-start:1/3;</p>
            <p>grid-row:2/4;</p>
            <p>}</p>
            <p>.section-nine-3>span:first-child{</p>
            <p>grid-column-start: 1;</p>
            <p>grid-column-end: 3;</p>
            <p>grid-row-start: 2;</p>
            <p>grid-row-end: 4;</p>
            <p>}</p>
        </div>
    </blockquote>
</section>
<section class="section-1">
    <h1>10.<code>justify-self</code> 属性 | <code>align-self</code> 属性 | <code>place-self</code> 属性</h1>
    <p><code>justify-self</code>属性设置单元格内容的水平位置(左中右),跟<code>justify-items</code>属性的用法完全一致,但只作用于单个项目。</p>
    <p><code>justify-self</code>属性设置单元格内容的垂直位置(上中下),跟<code>align-items</code>属性的用法完全一致,也是只作用于单个项目。</p>
    <p><code>place-self</code>属性是<code>align-self</code>属性和<code>justify-self</code>属性的合并简写形式。</p>
    <p>如果省略第二个值,<code>place-self</code>属性会认为这两个值相等。</p>
    <blockquote>
        <p>place-self: (align-self)  (justify-self);</p>
    </blockquote>
    <p>两个属性都可以取四个值。start:对齐单元格的起始边缘。 end:对齐单元格的结束边缘。</p>
    <p>center:单元格内部居中。stretch:拉伸,占满单元格的整个宽度(默认值)。</p>
    <blockquote>
        <div>
            <p>.section-ten-1{</p>
            <p>width:400px;</p>
            <p>display:grid;</p>
            <p>grid-template-columns: repeat(3,1fr);</p>
            <p>grid-template-rows: repeat(3,1fr);</p>
            <p>}</p>
            <p>.section-ten-1>span:first-child{</p>
            <p> justify-self: start;</p>
            <p>}</p>
        </div>
        <img style="width:37%;" src="/image/grid/17.png" >
    </blockquote>
    <div class="section-ten-1">
        <span class="one">1</span>
        <span class="two">2</span>
        <span class="three">3</span>
        <span class="four">4</span>
        <span class="five">5</span>
        <span class="six">6</span>
        <span class="seven">7</span>
        <span class="eight">8</span>
        <span class="nine">9</span>
    </div>
</section>
</body>
</html>

原文地址:https://www.cnblogs.com/sqyambition/p/11617597.html

时间: 2024-10-09 06:13:07

88.CSS---Grid 网格布局教程的相关文章

CSS Grid 网格布局教程

一.概述 网格布局(Grid)是最强大的 CSS 布局方案. 它将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局.以前,只能通过复杂的 CSS 框架达到的效果,现在浏览器内置了. 上图这样的布局,就是 Grid 布局的拿手好戏. Grid 布局与 Flex 布局有一定的相似性,都可以指定容器内部多个项目的位置.但是,它们也存在重大区别. Flex 布局是轴线布局,只能指定"项目"针对轴线的位置,可以看作是一维布局.Grid 布局则是将容器划分成"行"

python之tkinter使用-Grid(网格)布局管理器

1 # 使用tkinter编写登录窗口 2 # Grid(网格)布局管理器会将控件放置到一个二维的表格里,主控件被分割为一系列的行和列 3 # stricky设置对齐方式,参数N/S/W/E分别表示上.下.左.右 4 # columnspan:指定控件跨越多列显示 5 # rowspan:指定控件跨越多行显示 6 # padx.pady分别设置横向和纵向间隔大小 7 8 import tkinter as tk 9 10 root = tk.Tk() 11 root.title("请登录&quo

web前端入门到实战:CSS自定义属性+CSS Grid网格实现超级的布局能力

最近我还注意到的一件事就是CSS自定义属性.CSS自定义属性的工作方式有点像SASS和其他预处理器中的变量,主要的区别在于其它方法都是在浏览器中编译后生成,还是原本的CSS写法.CSS自定义属性是真正的动态变量,可以在样式表中或使用javascript即时更新,这使得它们具有更多的可能性.如果你熟悉JavaScript,我喜欢把预处理器变量和CSS自定义属性之间的区别想象成与const和let之间的区别相似--它们都有不同的用途. CSS自定义属性可以方便的实现很多功能(例如主题变化).最近我一

CSS Grid layout布局

CSS Grid布局 (又名"网格"),是一个基于二维网格布局的系统,主要目的是改变我们基于网格设计的用户接口方式.你只需要定义一个容器元素并设置display:grid,使用grid-template-columns 和 grid-template-rows属性设置网格的列与 行的大小,然后使用grid-column 和 grid-row属性将其子元素放入网格之中.目前还处于 W3C 的工作草案之中,并且默认情况下,还不被所有的浏览器所支持.出于示例演示,建议你使用启用了特殊标志的

Grid网格布局

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>网格布局</title> <style> .container { display: grid; grid-template-columns: 100px 100px 100px 100px 100px: grid-template-rows: 100px 100px 100px 10

Pure CSS 的网格布局(比bootstrap小很多且易扩展的css UI)

(转自百度经验)http://jingyan.baidu.com/article/48a42057c44fdba9242504dd.html Pure是一个简单.实用的CSS框架,鉴于目前网上对pure的资料比较少,所以我想把使用pure的一些经验介绍给大家,相信你看完本文后也会爱上pure. Pure包含多个模块:Base(基本样式).Grids(网格系统).Forms(表单).Buttons(按钮).Tables(表格).Menus(菜单),本文主要介绍如何使用Grids(网格系统). 1.

css3 grid 网格布局

1.grid-template设置网格模板,实现四列两行布局:grid-gap设置网格间隙,包括行和列 2.grid布局,使用fr单位实现等比例分配空间.fr是分数(fraction)的缩写 .con{ display: grid; grid-template-columns: 1fr 3fr 2fr 6fr; grid-template-rows: 50px 50px 50px ; grid-gap: 10px;}.con div{ background: orange;}.con div:n

CSS多列布局Multi-column、伸缩布局Flexbox、网格布局Grid详解

新css属性为我们提供了更加便捷的网页布局方式.来自微软的thomas lewis将带你认识去Grid Alignment,Flexibox Box以及Multi-column Layout这三大领域. 这篇文章最早出现在the April 2012 issue (226)这期的.net杂志上-这杂志是面向网页设计者以及开发者,全球销量最高的杂志. 按照以往来说,用CSS来布局看起来总是一样非常繁杂的工作.然而,随着一个个新标准的推出,网页设计者已经能够实现非常轻松地进行布局工作了. 主流的浏览

如何使用Flexbox和CSS Grid,实现高效布局

CSS 浮动属性一直是网站上排列元素的主要方法之一,但是当实现复杂布局时,这种方法不总是那么理想.幸运的是,在现代网页设计时代,使用 Flexbox 和 CSS Grid 来对齐元素,变得相对容易起来. 使用 Flexbox 可以使元素对齐变得容易,因此 Flexbox 已经被广泛使用了. 同时,CSS Grid 布局也为网页设计行业带来了很大的便利.虽然 CSS Grid 布局未被广泛采用,但是浏览器逐渐开始增加对 CSS Grid 布局的支持. 虽然 Flexbox 和 CSS Grid 可