今天刚好要做一个好多div格子错落组成的布局,不是田字格,不是九宫格,12个格子这样子,看起来有点复杂。关键的是笔者有点懒,要写那么多div和css真是不想下手啊。多看了两眼,这布局不跟网格挺像吗?css grid好像就是长这样子的?会不会很简单呢?反正也不熟,实在不行就当学习了。说干就干,说不定能偷点懒呢哈哈~
最终要求的效果图是这样子的:
那么从网格的概念和例子开始,依葫芦画瓢,边看边干吧~
大概看了看,这个和表格类似。要实现效果图的效果,首先给容器设置grid网格属性,然后再看有几行几列。显然是4行5列了,那可以先实现一个4行5列的网格,沿着这个思路实现如下:
grid-container里面有12个div,代表12个格子.
<div class="main">
<div class="grid-container">
<div class="grid-item one">1</div>
<div class="grid-item two">2</div>
<div class="grid-item three">3</div>
<div class="grid-item four">4</div>
<div class="grid-item five">5</div>
<div class="grid-item six">6</div>
<div class="grid-item seven">7</div>
<div class="grid-item eight">8</div>
<div class="grid-item nine">9</div>
<div class="grid-item ten">10</div>
<div class="grid-item eleven">11</div>
<div class="grid-item twelve">12</div>
</div>
</div>
display: grid设置容器网格属性.
grid-template-columns属性定义每一列的列宽,grid-template-rows属性定义每一行的行高.
.main{
width: 1200px;
margin: 30px auto 0;
}
.grid-container{
display: grid;
grid-template-columns: 385px 180px 180px 180px 180px;
grid-template-rows: 180px 180px 180px 180px;
}
.grid-item{
border: 2px solid rgb(233,171,88);
border-radius: 5px;
background-color: rgba(233,171,88,.5);
}
实现效果如下:
完成了第一步之后,对比效果图,发现行与行之间和列与列之间都有一定的距离。接下来就开始添加行和列之间的间距。
grid-row-gap属性设置行与行的间隔(行间距),grid-column-gap属性设置列与列的间隔(列间距)
.main{
width: 1200px;
margin: 30px auto 0;
}
.grid-container{
display: grid;
grid-template-columns: 385px 180px 180px 180px 180px;
grid-template-rows: 180px 180px 180px 180px;
grid-column-gap: 24px;
grid-row-gap: 24px;
}
.grid-item{
border: 2px solid rgb(233,171,88);
border-radius: 5px;
background-color: rgba(233,171,88,.5);
}
完成效果如下:
再对比以下设计图,那么接下来关键的一步,就是给对应的格子设置跨几行几列了,和表格的rowspan和colspan类似。如果不设置,则默认占1行1列。
grid-column-start属性:左边框所在的垂直网格线
grid-column-end属性:右边框所在的垂直网格线
grid-row-start属性:上边框所在的水平网格线
grid-row-end属性:下边框所在的水平网格线
.main{
width: 1200px;
margin: 30px auto 0;
}
.grid-container{
display: grid;
grid-template-columns: 385px 180px 180px 180px 180px;
grid-template-rows: 180px 180px 180px 180px;
grid-column-gap: 24px;
grid-row-gap: 24px;
}
.grid-item{
border: 2px solid rgb(233,171,88);
border-radius: 5px;
background-color: rgba(233,171,88,.5);
}
.grid-item.one{
grid-row-start: 1;
grid-row-end: 3;
}
.grid-item.two{
grid-column-start: 2;
grid-column-end: 4;
}
.grid-item.three{
grid-column-start: 4;
grid-column-end: 6;
}
.grid-item.six{
grid-column-start: 4;
grid-column-end: 6;
}
.grid-item.eight{
grid-column-start: 2;
grid-column-end: 4;
grid-row-start: 3;
grid-row-end: 5;
}
.grid-item.ten{
grid-column-start: 5;
grid-column-end: 6;
grid-row-start: 3;
grid-row-end: 5;
}
设置完格子所跨的行列之后,效果如下:
走到这一步的话,基本上算是大功告成了,现在的布局基本和效果图一致了。
代码里好多grid-column-start,grid-column-end, grid-row-start, grid-row-end,写起来有些繁琐,那么有没有简写形式呢?答案肯定是有的,下面就来介绍一下:
grid-column属性是grid-column-start和grid-column-end的合并简写形式,grid-row属性是grid-row-start属性和grid-row-end的合并简写形式。
上面的css可以简化为:
.main{
width: 1200px;
margin: 30px auto 0;
}
.grid-container{
display: grid;
grid-template-columns: 385px 180px 180px 180px 180px;
grid-template-rows: 180px 180px 180px 180px;
grid-column-gap: 24px;
grid-row-gap: 24px;
}
.grid-item{
border: 2px solid rgb(233,171,88);
border-radius: 5px;
background-color: rgba(233,171,88,.5);
}
.grid-item.one{
grid-row: 1 / 3;
}
.grid-item.two{
grid-column: 2 / 4;
}
.grid-item.three{
grid-column: 4 / 6;
}
.grid-item.six{
grid-column: 4 / 6;
}
.grid-item.eight{
grid-column: 2 / 4;
grid-row: 3 / 5;
}
.grid-item.ten{
grid-column: 5 / 6;
grid-row: 3 / 5;
}
grid-column和grid-row,也可以使用span关键字,表示跨越多少个网格。因此上面的代码还等价于:
.main{
width: 1200px;
margin: 30px auto 0;
}
.grid-container{
display: grid;
grid-template-columns: 385px 180px 180px 180px 180px;
grid-template-rows: 180px 180px 180px 180px;
grid-column-gap: 24px;
grid-row-gap: 24px;
}
.grid-item{
border: 2px solid rgb(233,171,88);
border-radius: 5px;
background-color: rgba(233,171,88,.5);
}
.grid-item.one{
grid-row: 1 / span 2;
}
.grid-item.two{
grid-column: 2 / span 2;
}
.grid-item.three{
grid-column: 4 / span 2;
}
.grid-item.six{
grid-column: 4 / span 2;
}
.grid-item.eight{
grid-column: 2 / span 2;
grid-row: 3 / span 2;
}
.grid-item.ten{
grid-column: 5 / span 1;
grid-row: 3 / span 2;
}
.main{
width: 1200px;
margin: 30px auto 0;
}
.grid-container{
display: grid;
grid-template-columns: 385px 180px 180px 180px 180px;
grid-template-rows: 180px 180px 180px 180px;
grid-column-gap: 24px;
grid-row-gap: 24px;
}
.grid-item{
border: 2px solid rgb(233,171,88);
border-radius: 5px;
background-color: rgba(233,171,88,.5);
}
.grid-item.one{
grid-row: span 2 / 3;
}
.grid-item.two{
grid-column: span 2 / 4;
}
.grid-item.three{
grid-column: span 2 / 6;
}
.grid-item.six{
grid-column: span 2 / 6;
}
.grid-item.eight{
grid-column: span 2 / 4;
grid-row: span 2 / 5;
}
.grid-item.ten{
grid-column: span 1 / 6;
grid-row: span 2 / 5;
}