关于css3:10分钟理解CSS3-Grid布局

根本介绍

Grid做前端的同学应该都很相熟了,翻译成中文为“栅格”,用过bootstrap、semantic ui、ant design的同学必定都理解grid layout(删格布局),以往css框架中的grid布局个别是通过float和百分比的宽度实现的,这种实现有几种毛病:

  • html不够简洁;
  • 须要革除浮动以防止高度塌陷;
  • 列的个数是固定的,不能灵便定义。比方bootstrap是12列,semantic ui是16列,ant design 24列。

当然grid也能够用flex实现,然而并不会比用float简略多少,而且flex善于的是一维空间的布局,而对grid这种二维空间并不善于。当初css3从标准和规范层面实现了grid,编程体验大大晋升!

兼容性

用法

Grid作为一个二维的栅格零碎,由若干列(column)和行(row)形成。

1. Column

(1) 设置column

CSS3中的Grid能够划分为任意个数的列(column),而且每个column的宽度能够任意设置!咱们先来看一个简略的例子:

<div id="content">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <div>7</div>
    <div>8</div>
    <div>9</div>
</div>

css:

body{
    color: #fff;
    text-align: center;
}
#content{
    display: grid;
    grid-template-columns: 33.3% 33.3% 33.3%;
    max-width: 960px;
    margin: 0 auto;
}
#content div{
    background: lightgrey;
    padding: 30px;
}
#content div:nth-child(even){
    background: skyblue;
}

成果:

当咱们设置了display: gridgrid-template-columns: 33.3% 33.3% 33.3%#content就被划分成了三行三列的grid,此时#content被称为grid container,而#content的子元素称为grid item。

咱们也能够任意扭转column的个数和宽度,比方:

#content{
    grid-template-columns: 10% 20% 30% 40%;
}

成果:

(2) fr(fraction)

css3中引入了一个新的单位fr(fraction),中文意思为“分数”,用于代替百分比,因为百分比(小数)存在除不尽的状况,用分数示意能够防止多位小数的写法。比方三列等宽的grid能够示意为:

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

(3) repeat

咱们也能够用repeat办法来简化column或者row的写法,repeat办法承受两个参数,第一个参数示意反复的次数,第二个参数示意反复的内容。所以,三列等宽的grid咱们还能够示意为:

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

当咱们要定义的列数很多时,repeat就会变得十分有用,比方咱们要定义一个10列等宽的grid,能够写成repeat(10, 1fr),而不必将1fr反复书写10遍。

2. Row

(1) 设置row

当咱们设置column之后,row会因为元素的换行而主动产生,然而咱们仍然能够设置row的个数和高度。

css:

`#content{`
`display``: grid;`
`grid-template-columns:` `repeat``(``3``,` `1``fr);`
`grid-template-rows:` `repeat``(``4``,` `60px``);`
`max-width``:` `960px``;`
`}`

成果:

能够看到,尽管第四行没有内容,然而row的确存在并占据了那局部空间。

(2) minmax

下面的例子中咱们给了row一个固定高度,这导致一个问题:如果某个grid item中的内容特地多,受制于固定的高度,局部内容将无奈显示,如下图:

为解决这个问题,css提供了minmax函数,让咱们能够设置row的最小高度和最大高度,最大高度取auto后便能够让row的高度自适应:

css:

`grid-auto-rows: minmax(``60px``,` `auto``);`
`// 或者`
`grid-template-rows:` `repeat``(``3``, minmax(``60px``,` `auto``));`

成果:

(3) grid gap

如果咱们想给行和列之间加上距离,也有现成的办法:

css:

`grid-gap:{`
`10px``;`
`}`

成果:

3. Grid Line

以上所有例子中,grid中的每个grid item都是按默认顺序排列的。如果咱们想从新布局扭转grid item的地位或大小呢?为此引入了grid lines的概念,所谓的grid lines就是将grid若干等分后的分割线,如下图中横向和纵向序号1~8的线即为grid lines:

`<``img` `src="[http://lc-jOYHMCEn.cn-n1.lcfi...;/](http://lc-joyhmcen.cn-n1.lcfi...%3B/)>`

通过定义grid item的起始和完结的grid line咱们就能够实现对grid item地位和覆盖面积的管制。一个简略的例子:

html:

<div id="content">
    <div class="one">1</div>
</div>

css:

#content {
  display: grid;
  grid-template-columns: repeat(8, 100px);
  grid-template-rows: repeat(8, 100px);
  grid-gap: 10px;
}

.one {
  grid-column-start: 3;
  grid-column-end: 6;
  grid-row-start: 3;
  grid-row-end: 6;
}

成果:

通过设置grid-column-start/end grid-row-start/end 相当于给grid item设置起始坐标和完结坐标,下面的css也能够简写为:

.one {
  grid-column: 3 / 6;
  grid-row: 3 / 6;
}
// 或者
.one {
  grid-area: 3 / 3 / 6 / 6;
}

如果grid item的起始grid line为默认,咱们能够只设置它的跨度(span):

.one{
  grid-column: span 3;
  grid-row: span 3;
}

4. Grid Area Template

除了通过grid lines进行布局,css3提供了一种更牛逼的布局形式:grid area template。与其用语言解释什么是grid area template,不如间接看代码:

html:

<div id="content">
    <header>Header</header>
    <main>Main</main>
    <section>Section</section>
    <aside>Aside</aside>
    <nav>Nav</nav>
    <footer>Footer</footer>
</div>

css:

body {
  color: #fff;
  text-align: center;
}

#content {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: minmax(100px, auto);
  max-width: 960px;
  margin: 0 auto;
  grid-gap: 10px;
  grid-template-areas: 
  "header header header header"
  "aside . main main"
  "nav . main main"
  "section section section section"
  "section section section section"
  "footer footer footer footer";
}

#content>* {
  background: #3bbced;
  padding: 30px;
}

header { grid-area: header; }
main   { grid-area: main; }
section{ grid-area: section; }
aside  { grid-area: aside; }
nav    { grid-area: nav; }
footer { grid-area: footer; }

成果:

看明确没有?重点在于grid container的 grid-template-areas 属性。咱们给每个grid item设置一个grid area,而后在grid container中设置一个grid area模版(grid-template-areas),模版中每行字符串示意一个row,每个area名称示意一个column,齐全将几何布局用文字模仿进去,空白的grid item用 . 示意。当然应用grid area要留神语法谨严,像 "header main header main" 这种写法css是无奈解析的,用area名称模拟出的构造在二维空间上必须是一个整体,因为每个grid item也是无奈宰割的。

应用grid area template的长处在实现响应式布局时也是不言而喻的,咱们只须要针对不同的屏幕尺寸制订不同的grid area template就行了。

5. Justify and Align

与flex相似,grid也能够设置justify和align来调整grid item横向和纵向对齐形式。同样也同时反对对grid container或单个grid item进行设置。

对grid container设置

html:

<div id="content">
    <div class="one">1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
</div>

css:

#content {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, minmax(120px, auto));
  grid-gap: 10px;
  max-width: 960px;
  align-items: start;
  justify-items: end;
}

成果:

留神:flex外面用的是 justify-content 而grid外面是 justify-items ,flex外面的值是 flex-start/flex-end,而grid外面是 start/end 。justify和align的默认值都是 stretch

对grid item设置

css:

.one{
  align-self: start;
  justify-self: end;
}

成果:

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理