整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

一文详解CSS常见的五大布局

文概要

本文将介绍如下几种常见的布局:

一、单列布局

常见的单列布局有两种:

  • header,content 和 footer 等宽的单列布局
  • header 与 footer 等宽,content 略窄的单列布局

1.如何实现

对于第一种,先通过对 header,content,footer 统一设置 width:1000px;或者 max-width:1000px(这两者的区别是当屏幕小于 1000px 时,前者会出现滚动条,后者则不会,显示出实际宽度);然后设置 margin:auto 实现居中即可得到。

<div class="header"></div> 
<div class="content"></div> 
<div class="footer"></div> 
.header{ 
 margin:0 auto; 
 max-width: 960px; 
 height:100px; 
 background-color: blue; 
} 
.content{ 
 margin: 0 auto; 
 max-width: 960px; 
 height: 400px; 
 background-color: aquamarine; 
} 
.footer{ 
 margin: 0 auto; 
 max-width: 960px; 
 height: 100px; 
 background-color: aqua; 
} 

对于第二种,header、footer 的内容宽度不设置,块级元素充满整个屏幕,但 header、content 和 footer 的内容区设置同一个 width,并通过 margin:auto 实现居中。

<div class="header"> 
 <div class="nav"></div> 
</div> 
<div class="content"></div> 
<div class="footer"></div> 
.header{ 
 margin:0 auto; 
 max-width: 960px; 
 height:100px; 
 background-color: blue; 
} 
.nav{ 
 margin: 0 auto; 
 max-width: 800px; 
 background-color: darkgray; 
 height: 50px; 
} 
.content{ 
 margin: 0 auto; 
 max-width: 800px; 
 height: 400px; 
 background-color: aquamarine; 
} 
.footer{ 
 margin: 0 auto; 
 max-width: 960px; 
 height: 100px; 
 background-color: aqua; 
} 

二、两列自适应布局

两列自适应布局是指一列由内容撑开,另一列撑满剩余宽度的布局方式

1.float+overflow:hidden

如果是普通的两列布局, 浮动+普通元素的 margin 便可以实现,但如果是自适应的两列布局,利用 float+overflow:hidden 便可以实现,这种办法主要通过 overflow 触发 BFC,而 BFC 不会重叠浮动元素。由于设置 overflow:hidden 并不会触发 IE6-浏览器的 haslayout 属性,所以需要设置 zoom:1 来兼容 IE6-浏览器。具体代码如下:

<div class="parent" style="background-color: lightgrey;"> 
 <div class="left" style="background-color: lightblue;"> 
 <p>left</p> 
 </div> 
 <div class="right" style="background-color: lightgreen;"> 
 <p>right</p> 
 <p>right</p> 
 </div> 
</div> 
.parent { 
overflow: hidden; 
zoom: 1; 
} 
.left { 
float: left; 
margin-right: 20px; 
} 
.right { 
overflow: hidden; 
zoom: 1; 
} 

注意点:如果侧边栏在右边时,注意渲染顺序。即在 HTML 中,先写侧边栏后写主内容

2.Flexbox 布局

Flexbox 布局,也叫弹性盒子布局,区区简单几行代码就可以实现各种页面的的布局。

//html部分同上 
.parent { 
display:flex; 
} 
.right { 
margin-left:20px; 
flex:1; 
} 

3.Grid 布局

Grid 布局,是一个基于网格的二维布局系统,目的是用来优化用户界面设计。

//html部分同上 
.parent { 
display:grid; 
grid-template-columns:auto 1fr; 
grid-gap:20px 
} 

三、三栏布局

特征:中间列自适应宽度,旁边两侧固定宽度 ,实现三栏布局有多种方式:

1.浮动布局

<!DOCTYPE html> 
<html> 
<head> 
 <meta charset="utf-8"> 
 <title>Layout</title> 
 <style media="screen"> 
 html * { 
 padding: 0; 
 margin: 0; 
 } 
 .layout article div { 
 min-height: 150px; 
 } 
 </style> 
</head> 
<body> 
 <!--浮动布局 --> 
 <section class="layout float"> 
 <style media="screen"> 
 .layout.float . left { 
 float: left; 
 width: 300px; 
 background: red; 
 } 
 .layout.float .center { 
 background: yellow; 
 } 
 .layout.float . right { 
 float: right; 
 width: 300px; 
 background: blue; 
 } 
 </style> 
 <h1>三栏布局</h1> 
 <article class="left-right-center"> 
 <div class="left"></div> 
 <div class="right"></div> // 右栏部分要写在中间内容之前 
 <div class="center"> 
 <h2>浮动解决方案</h2> 
 1.这是三栏布局的浮动解决方案; 2.这是三栏布局的浮动解决方案; 3.这是三栏布局的浮动解决方案; 4.这是三栏布局的浮动解决方案; 5.这是三栏布局的浮动解决方案; 6.这是三栏布局的浮动解决方案; 
 </div> 
 </article> 
 </section> 
</body> 
</html> 

这种布局方式,dom 结构必须是先写浮动部分,然后再中间块,否则右浮动块会掉到下一行。浮动布局的优点就是比较简单,兼容性也比较好。但浮动布局是有局限性的,浮动元素脱离文档流,要做清除浮动,这个处理不好的话,会带来很多问题,比如父容器高度塌陷等 。

2.绝对定位布局

<!--绝对布局 --> 
 <section class="layout absolute"> 
 <style> 
 .layout.absolute . left-center- right>div{ 
 position: absolute;//三块都是绝对定位 
 } 
 .layout.absolute . left { 
 left:0; 
 width: 300px; 
 background: red; 
 } 
 .layout.absolute .center { 
 right: 300px; 
 left: 300px;//离左右各三百 
 background: yellow; 
 } 
 .layout.absolute . right { 
 right: 0; 
 width: 300px; 
 background: blue; 
 } 
 </style> 
 <h1>三栏布局</h1> 
 <article class="left-center-right"> 
 <div class="left"></div> 
 <div class="center"> 
 <h2>绝对定位解决方案</h2> 
 1.这是三栏布局的浮动解决方案; 2.这是三栏布局的浮动解决方案; 3.这是三栏布局的浮动解决方案; 4.这是三栏布局的浮动解决方案; 5.这是三栏布局的浮动解决方案; 6.这是三栏布局的浮动解决方案; 
 </div> 
 <div class="right"></div> 
 </article> 
 </section> 

绝对定位布局优点就是快捷,设置很方便,而且也不容易出问题。缺点就是,容器脱离了文档流,后代元素也脱离了文档流,高度未知的时候,会有问题,这就导致了这种方法的有效性和可使用性是比较差的。

3.flexbox 布局

<!--flexbox布局--> 
 <section class="layout flexbox"> 
 <style> 
 .layout.flexbox .left-center- right{ 
 display: flex; 
 } 
 .layout.flexbox .left { 
 width: 300px; 
 background: red; 
 } 
 .layout.flexbox .center { 
 background: yellow; 
 flex: 1; 
 } 
 .layout.flexbox .right { 
 width: 300px; 
 background: blue; 
 } 
 </style> 
 <h1>三栏布局</h1> 
 <article class="left-center-right"> 
 <div class="left"></div> 
 <div class="center"> 
 <h2>flexbox解决方案</h2> 
 1.这是三栏布局的浮动解决方案; 2.这是三栏布局的浮动解决方案; 3.这是三栏布局的浮动解决方案; 4.这是三栏布局的浮动解决方案; 5.这是三栏布局的浮动解决方案; 6.这是三栏布局的浮动解决方案; 
 </div> 
 <div class="right"></div> 
 </article> 
 </section> 

flexbox 布局是 css3 里新出的一个,它就是为了解决上述两种方式的不足出现的,是比较完美的一个。目前移动端的布局也都是用 flexbox。 flexbox 的缺点就是 IE10 开始支持,但是 IE10 的是-ms 形式的。

4.表格布局

<!--表格布局--> 
 <section class="layout table"> 
 <style> 
 .layout.table . left-center- right { 
 display: table; 
 height: 150px; 
 width: 100%; 
 } 
 .layout.table . left-center- right>div { 
 display: table-cell; 
 } 
 .layout.table . left { 
 width: 300px; 
 background: red; 
 } 
 .layout.table .center { 
 background: yellow; 
 } 
 .layout.table . right { 
 width: 300px; 
 background: blue; 
 } 
 </style> 
 <h1>三栏布局</h1> 
 <article class="left-center-right"> 
 <div class="left"></div> 
 <div class="center"> 
 <h2>表格布局解决方案</h2> 
 1.这是三栏布局的浮动解决方案; 2.这是三栏布局的浮动解决方案; 3.这是三栏布局的浮动解决方案; 4.这是三栏布局的浮动解决方案; 5.这是三栏布局的浮动解决方案; 6.这是三栏布局的浮动解决方案; 
 </div> 
 <div class="right"></div> 
 </article> 
 </section> 

表格布局的兼容性很好,在 flex 布局不兼容的时候,可以尝试表格布局。当内容溢出时会自动撑开父元素 。

表格布局也是有缺陷:① 无法设置栏边距;② 对 seo 不友好;③ 当其中一个单元格高度超出的时候,两侧的单元格也是会跟着一起变高的,然而有时候这并不是我们想要的效果。

5.网格布局

<!--网格布局--> 
<section class="layout grid"> 
 <style> 
 .layout.grid .left-center- right { 
 display: grid; 
 width: 100%; 
 grid-template-columns: 300px auto 300px; 
 grid-template-rows: 150px;//行高 
 } 
 .layout.grid .left { 
 background: red; 
 } 
 .layout.grid .center { 
 background: yellow; 
 } 
 .layout.grid .right { 
 background: blue; 
 } 
 </style> 
 <h1>三栏布局</h1> 
 <article class="left-center-right"> 
 <div class="left"></div> 
 <div class="center"> 
 <h2>网格布局解决方案</h2> 
 1.这是三栏布局的浮动解决方案; 2.这是三栏布局的浮动解决方案; 3.这是三栏布局的浮动解决方案; 4.这是三栏布局的浮动解决方案; 5.这是三栏布局的浮动解决方案; 6.这是三栏布局的浮动解决方案; 
 </div> 
 <div class="right"></div> 
 </article> 
</section> 

CSS Grid 是创建网格布局最强大和最简单的工具。就像表格一样,网格布局可以让 Web 设计师根据元素按列或行对齐排列,但他和表格不同,网格布局没有内容结构,从而使各种布局不可能与表格一样。例如,一个网格布局中的子元素都可以定位自己的位置,这样他们可以重叠和类似元素定位 。

但网格布局的兼容性不好。IE10+上支持,而且也仅支持部分属性 。

6.圣杯布局

① 特点

比较特殊的三栏布局,同样也是两边固定宽度,中间自适应,唯一区别是 dom 结构必须是先写中间列部分,这样实现中间列可以优先加载 。

.container { 
 padding-left: 220px;//为左右栏腾出空间 
 padding-right: 220px; 
} 
.left { 
 float: left; 
 width: 200px; 
 height: 400px; 
 background: red; 
 margin-left: -100%; 
 position: relative; 
 left: -220px; 
} 
.center { 
 float: left; 
 width: 100%; 
 height: 500px; 
 background: yellow; 
} 
.right { 
 float: left; 
 width: 200px; 
 height: 400px; 
 background: blue; 
 margin-left: -200px; 
 position: relative; 
 right: -220px; 
} 
<article class="container"> 
 <div class="center"> 
 <h2>圣杯布局</h2> 
 </div> 
 <div class="left"></div> 
 <div class="right"></div> 
</article> 

② 实现步骤

  • 三个部分都设定为左浮动, 否则左右两边内容上不去,就不可能与中间列同一行 。然后设置 center 的宽度为 100%( 实现中间列内容自适应 ),此时,left 和 right 部分会跳到下一行

  • 通过设置 margin-left 为负值让 left 和 right 部分回到与 center 部分同一行

  • 通过设置父容器的 padding-left 和 padding-right,让左右两边留出间隙。

  • 通过设置相对定位,让 left 和 right 部分移动到两边。

③ 缺点

  • center 部分的最小宽度不能小于 left 部分的宽度,否则会 left 部分掉到下一行
  • 如果其中一列内容高度拉长(如下图),其他两列的背景并不会自动填充。(借助等高布局正padding+负margin可解决,下文会介绍)

7.双飞翼布局

① 特点

同样也是三栏布局,在圣杯布局基础上进一步优化,解决了圣杯布局错乱问题,实现了内容与布局的分离。而且任何一栏都可以是最高栏,不会出问题 。

.container { 
 min-width: 600px;//确保中间内容可以显示出来,两倍 left宽+ right宽 
} 
.left { 
 float: left; 
 width: 200px; 
 height: 400px; 
 background: red; 
 margin-left: -100%; 
} 
.center { 
 float: left; 
 width: 100%; 
 height: 500px; 
 background: yellow; 
} 
.center .inner { 
 margin: 0 200px; //新增部分 
} 
.right { 
 float: left; 
 width: 200px; 
 height: 400px; 
 background: blue; 
 margin-left: -200px; 
} 
<article class="container"> 
 <div class="center"> 
 <div class="inner">双飞翼布局</div> 
 </div> 
 <div class="left"></div> 
 <div class="right"></div> 
</article> 

② 实现步骤(前两步与圣杯布局一样)

  • 三个部分都设定为左浮动,然后设置 center 的宽度为 100%,此时,left 和 right 部分会跳到下一行;
  • 通过设置 margin-left 为负值让 left 和 right 部分回到与 center 部分同一行;
  • center 部分增加一个内层 div,并设 margin: 0 200px;

③ 缺点

多加一层 dom 树节点,增加渲染树生成的计算量

④ 圣杯布局和双飞翼布局实现方式对比:

  • 两种布局方式都是把主列放在文档流最前面,使主列优先加载。
  • 两种布局方式在实现上也有相同之处,都是让三列浮动,然后通过负外边距形成三列布局。
  • 两种布局方式的不同之处在于如何处理中间主列的位置: 圣杯布局是利用父容器的左、右内边距+两个从列相对定位 ; 双飞翼布局是把主列嵌套在一个新的父级块中利用主列的左、右外边距进行布局调整

四、等高列布局

等高布局是指子元素在父元素中高度相等的布局方式。等高布局的实现包括伪等高和真等高,伪等高只是看上去等高而已,真等高是实实在在的等高。

1.利用背景图片

这种方法是我们实现等高列最早使用的一种方法,就是使用背景图片,在列的父元素上使用这个背景图进行Y轴的铺放,从而实现一种等高列的假象。实现方法简单,兼容性强,不需要太多的css样式就可以轻松实现,但此方法不适合流体布局等高列的布局。

在制作样式之前需要一张类似下面的背景图:

<div class=”container clearfix”> 
 <div class=”left”></div> 
 <div class=”content”></div> 
 <div class=”right”></div> 
</div> 
.container { 
background: url("column.png") repeat-y; 
width: 960px; 
margin: 0 auto; 
} 
.left { 
float: left; 
width: 220px; 
} 
.content { 
float: left; 
width: 480px; 
} 
.right { 
float: left; 
width: 220px; 
} 

2.利用正padding+负margin

我们通过等布局便可解决圣杯布局的第二点缺点,因为背景是在 padding 区域显示的, 设置一个大数值的 padding-bottom,再设置相同数值的负的 margin-bottom,并在所有列外面加上一个容器,并设置 overflow:hidden 把溢出背景切掉 。这种可能实现多列等高布局,并且也能实现列与列之间分隔线效果,结构简单,兼容所有浏览器。新增代码如下:

.center, 
 .left, 
 .right { 
 padding-bottom: 10000px; 
 margin-bottom: -10000px; 
 } 
 .container { 
 padding-left: 220px; 
 padding-right: 220px; 
 overflow: hidden;//把溢出背景切掉 
 } 

3.模仿表格布局

这是一种非常简单,易于实现的方法。不过兼容性不好,在ie6-7无法正常运行。

 <div class="container table"> 
 <div class="containerInner tableRow"> 
 <div class="column tableCell cell1"> 
 <div class="left aside"> 
 .... 
 </div> 
 </div> 
 <div class="column tableCell cell2"> 
 <div class="content section"> 
 ... 
 </div> 
 </div> 
 <div class="column tableCell cell3"> 
 <div class="right aside"> 
 ... 
 </div> 
 </div> 
 </div> 
 </div> 
.table { 
width: auto; 
min-width: 1000px; 
margin: 0 auto; 
padding: 0; 
display: table; 
} 
.tableRow { 
display: table-row; 
} 
.tableCell { 
display: table-cell; 
width: 33%; 
} 
.cell1 { 
background: #f00; 
height: 800px; 
} 
.cell2 { 
background: #0f0; 
} 
.cell3 { 
background: #00f; 
} 

4.使用边框和定位

这种方法是使用边框和绝对定位来实现一个假的高度相等列的效果。结构简单,兼容各浏览器,容易掌握。假设你需要实现一个两列等高布局,侧栏高度要和主内容高度相等。

#wrapper { 
width: 960px; 
margin: 0 auto; 
} 
#mainContent { 
border-right: 220px solid #dfdfdf; 
position: absolute; 
width: 740px; 
height: 800px; 
background: green; 
} 
#sidebar { 
background: #dfdfdf; 
margin-left: 740px; 
position: absolute; 
height: 800px; 
width: 220px; 
} 
<div id="wrapper"> 
 <div id="mainContent">...</div> 
 <div id="sidebar">...</div> 
</div> 

五、粘连布局

1.特点

  • 有一块内容 <main> ,当 <main> 的高康足够长的时候,紧跟在 <main> 后面的元素 <footer> 会跟在 <main> 元素的后面。
  • 当 <main> 元素比较短的时候(比如小于屏幕的高度),我们期望这个 <footer> 元素能够“粘连”在屏幕的底部

具体代码如下:

main

main

main

footer

* { 
 margin: 0; 
 padding: 0; 
 } 
 html, 
 body { 
 height: 100%;//高度一层层继承下来 
 } 
 #wrap { 
 min-height: 100%; 
 background: pink; 
 text-align: center; 
 overflow: hidden; 
 } 
 #wrap .main { 
 padding-bottom: 50px; 
 } 
 #footer { 
 height: 50px; 
 line-height: 50px; 
 background: deeppink; 
 text-align: center; 
 margin-top: -50px; 
 } 

2.实现步骤

(1)footer 必须是一个独立的结构,与 wrap 没有任何嵌套关系

(2)wrap 区域的高度通过设置 min-height,变为视口高度

(3)footer 要使用 margin 为负来确定自己的位置

(4)在 main 区域需要设置 padding-bottom。这也是为了防止负 margin 导致 footer 覆盖任何实际内容。

参考文章

  • 双飞翼布局介绍-始于淘宝 UED
  • CSS 三栏布局的四种方法
  • CSS 两列布局---左侧固定,右侧自适应
  • 两列自适应布局的四种思路
  • css 常见布局之九:三栏布局的常见实现
  • 【布局】聊聊为什么淘宝要提出「双飞翼」布局
  • CSS 的单列布局与二&三列布局
  • 八种创建等高列布局

HTML的世界里,一切都是由容器和内容构成的。容器,就如同一个个盒子,用来装载各种元素;而内容,则是这些盒子里的珍宝。理解了这一点,我们就迈出了探索HTML布局的第一步。

在HTML中,布局标签主要用于控制页面的结构和样式。本文将介绍一些常用的布局标签及其使用方法,并通过代码示例进行演示。

一、理解布局的重要性

布局在我们前端开发中担任什么样的角色呢?想象一下,你面前有一堆散乱的积木,无序地堆放在那里。

而你的任务,就是将这些积木按照图纸拼装成一个精美的模型。HTML布局标签的作用就像那张图纸,它指导浏览器如何正确、有序地显示内容和元素,确保网页的结构和外观既美观又实用。

下面我们就来看看在HTML中常用的基础布局标签有哪些,如何使用这些布局标签完成我们的开发目标。

二、常用的布局标签

1、div标签

div标签是一个块级元素,它独占一行,用于对页面进行区域划分。它可以包含其他HTML元素,如文本、图片、链接等。通过CSS样式可以设置div的布局和样式。

示例代码:

<!DOCTYPE html>
<html>
<head>
<style>
  .box {
    width: 200px;
    height: 200px;
    background-color: red;
  }
</style>
</head>
<body>

<div>这是一个div元素

</div>

</body>
</html>

运行结果:

2、span标签

span标签是一个内联元素,它不独占一行,用于对文本进行区域划分。它主要用于对文本进行样式设置,如字体、颜色等。与div类似,span也可以包含其他HTML元素。
示例代码:

<!DOCTYPE html>
<html>
<head>
<style>
  .text {
    color: blue;
    font-size: 20px;
  }
</style>
</head>
<body>

<p>这是一个<span>span元素</span>。</p>

</body>
</html>

运行结果:

3、table标签

table标签用于创建表格,它包含多个tr(行)元素,每个tr元素包含多个td(单元格)或th(表头单元格)元素。

<table> 定义一个表格,<tr> 定义表格中的行,而 <td> 则定义单元格。通过这三个标签,我们可以创建出整齐划一的数据表,让信息的展示更加直观明了。

需要注意的是:

  • <table>和</table>标记着表格的开始和结束。
  • <tr>和</tr>标记着行的开始和结束,几组表示该表格有几行。
  • <td>和</td>标记着单元格的开始和结束,表示这一行中有几列。

示例代码:

<!DOCTYPE html>
<html>
<head>
<style>
  table, th, td {
    border: 1px solid black;
  }
</style>
</head>
<body>
<table>
  <tr>
    <th>姓名</th>
    <th>年龄</th>
  </tr>
  <tr>
    <td>张三</td>
    <td>25</td>
  </tr>
  <tr>
    <td>李四</td>
    <td>30</td>
  </tr>
</table>
</body>
</html>

运行结果:

4、form标签

<form>标签的主要作用是定义一个用于用户输入的HTML表单。这个表单可以包含各种输入元素,如文本字段、复选框、单选按钮、提交按钮等。

<form>元素可以包含以下一个或多个表单元素:<input>、<textarea>、<button>、<select>、<option>、<optgroup>、<fieldset>、<label>和<output>等。

示例代码:

<!DOCTYPE html>
<html>
<head>
<style>
  form {
    display: flex;
    flex-direction: column;
  }
</style>
</head>
<body>

<form>
  <label for="username">用户名:</label>
  <input type="text" id="username" name="username">
  <br>
  <label for="password">密码:</label>
  <input type="password" id="password" name="password">
  <br>
  <input type="submit" value="提交">
</form>

</body>
</html>

运行结果:

5、列表标签

1)无序列表

  • 指没有顺序的列表项目
  • 始于<ul>标签,每个列表项始于<li>
  • type属性有三个选项:disc实心圆、circle空心圆、square小方块。 默认属性是disc实心圆。

示例代码:

<!DOCTYPE html>
<htmml>
<head>
<meta charst = "UTF-8">
<title>html--无序列表</title>
</head>
<body>
<ul>
<li>默认的无序列表</li>
<li>默认的无序列表</li>
<li>默认的无序列表</li>
</ul>
<ul>
<li type = "circle">添加circle属性</li>
<li type = "circle">添加circle属性</li>
<li type = "circle">添加circle属性</li>
</ul>
<ul>
<li type = "square">添加square属性</li>
<li type = "square">添加square属性</li>
<li type = "squaare">添加square属性</li>
</ul>
</body>
</html>

运行结果:


也可以使用CSS list-style-type属性定义html无序列表样式。

想要快速入门前端开发吗?推荐一个前端开发基础课程,这个老师讲的特别好,零基础学习无压力,知识点结合代码,边学边练,可以免费试看试学,还有各种辅助工具和资料,非常适合新手!点这里前往学习哦!「链接」

2)有序列表

  • 指按照字母或数字等顺序排列的列表项目。
  • 其结果是带有前后顺序之分的编号,如果插入和删除一个列表项,编号会自动调整。
  • 始于<ol>标签,每个列表项始于<li>。

示例代码:

<ol>
<li>默认的有序列表</li>
<li>默认的有序列表</li>
<li>默认的有序列表</li>
</ol>
<ol type = "a" start = "2">
<li>第1项</li>
<li>第2项</li>
<li>第3项</li>
<li value ="20">第四项</li>
</ol>
<ol type = "Ⅰ" start = "2">
<li>第1项</li>
<li>第2项</li>
<li>第3项</li>
</ol>

运行结果:


同样也可以使用CSS list-style-type属性定义html有序列表样式。

3)自定义列表

  • 自定义列表不仅仅是一列项目,而是项目及其注释的组合。
  • 以<dl>标签开始。每个自定义列表项以<dt>开始。每个自定义列表项的定义以<dd>开始。
  • 用于对术语或名词进行解释和描述,自定义列表的列表项前没有任何项目符号。
    基本语法:
<dl>
<dt>名词1</dt>
<dd>名词1解释1</dd>
<dd>名词1解释2</dd>

<dt>名词2</dt>
<dd>名词2解释1</dd>
<dd>名词2解释2</dd>
</dl>

<dl>即“definition list(定义列表)”,
<dt>即“definition term(定义名词)”,
而<dd>即“definition description(定义描述)”。

示例代码:

<dl>
<dt>计算机</dt>
<dd>用来计算的仪器</dd>

<dt>显示器</dt>
<dd>以视觉方式显示信息的装置</dd>
</dl>

运行结果:


以上就是HTML中常用的布局标签及其使用方法。在实际开发中,还可以结合CSS和JavaScript来实现更复杂的布局和交互效果。

掌握了这些HTML常用布局标签,你已经拥有了构建网页的基础工具。记住,好的布局不仅需要技术,更需要创意和对细节的关注。现在,打开你的代码编辑器,开始你的布局设计之旅吧!

SS 网格布局(Grid Layout) 是CSS中最强大的布局系统。 这是一个二维系统,这意味着它可以同时处理列和行,不像 flexbox 那样主要是一维系统。 你可以通过将CSS规则应用于父元素(成为网格容器)和该元素的子元素(网格元素),来使用网格布局。

引言

CSS网格布局(又名“网格”)是一个二维的基于网格的布局系统,其目的只在于完全改变我们设计基于网格的用户界面的方式。 CSS一直用来布局网页,但一直都不完美。 一开始我们使用table 做布局,然后转向浮动、定位以及inline-block,但所有这些方法本质上都是 Hack 的方式,并且遗漏了很多重要的功能(例如垂直居中)。 Flexbox的出现在一定程度上解决了这个问题,但是它的目的是为了更简单的一维布局,而不是复杂的二维布局(Flexbox和Grid实际上一起工作得很好)。 只要我们一直在制作网站,我们就一直在为解决布局问题不断探索, 而Grid是第一个专门为解决布局问题而生的CSS模块。

有两个东西,启发我写这篇指南。 第一个是雷切尔·安德鲁(Rachel Andrew)的书为CSS Grid布局准备。 这本书对网格布局做了彻底、清晰的介绍,也是是整篇文章的基础,我强烈建议你购买并阅读他的书。 我的另一个重要灵感是Chris Coyier的Flexbox完全指南,当需要查阅 flexbox 的一切资料时我就会找这篇文章。 这篇文章帮助了很多人学习 Flex 布局,也是 Google 上搜索“flexbox”关键字排名第一的文章。你会发现他的文章和我的很多相似之处,有最好的范例在那放着为什么咱不偷师学着写呢?

本指南的目的是介绍网格概念,因为它们存在于最新版本的规范中。 因此我不会覆盖过时的IE语法,而且随着规范的成熟,我会尽最大努力保存更新本指南。

基础知识以及浏览器支持情况

一开始你需要使用display:grid把容器元素定义为一个网格,使用grid-template-columns和grid-template-rows设置列和行大小,然后使用grid-column 和 grid-row把它的子元素放入网格。 与flexbox类似,网格子元素的原始顺序不重要。 你的可以在 CSS 里以任意顺序放置它们,这使得使用媒体查询重新排列网格变得非常容易。 想象一下,我们需要定义整个页面的布局,然后为了适应不同的屏幕宽度完全重新排列,我们只需要几行CSS就能实现这个需求。 网格是有史以来最强大的CSS模块之一。

截至2017年3月,许多浏览器都提供了原生的、不加前缀的对CSS Grid的支持,比如 Chrome(包括Android),Firefox,Safari(包括iOS)和Opera。 另一方面,Internet Explorer 10和11支持它,但需要使用过时的语法。 Edge浏览器已经宣布将支持标准的Grid语法,但暂未支持。

浏览器支持的详细数据可在Caniuse查看。其中里面的数字表示该版本以上的浏览器支持Grid。

桌面浏览器

移动端 / 平板

除了微软之外,浏览器制造商在 Grid 规范完全落地以前似乎并没有放手让 Gird 野生也长的打算。 这是一件好事,这意味着我们不需要再去学习各种浏览器兼容版本的旧语法。

在生产环境中使用Grid只是时间问题,但现在是我们该学习的时候了。

重要术语

在深入了解网格的概念之前,理解术语是很重要的。 由于这里所涉及的术语在概念上都是相似的,如果不先记住它们在网格规范中定义的含义,则很容易将它们彼此混淆。 但是不用太担心,这些术语并不多。

Grid Container

设置了 display: gird 的元素。 这是所有 grid item 的直接父项。 在下面的例子中,.container 就是是 grid container。

<div class="container">
 <div class="item item-1"></div>
 <div class="item item-2"></div>
 <div class="item item-3"></div>
</div> 

Grid Item

Grid 容器的孩子(直接子元素)。下面的 .item 元素就是 grid item,但 .sub-item不是。

<div class="container">
 <div class="item"></div> 
 <div class="item">
 <p class="sub-item"></p>
 </div>
 <div class="item"></div>
</div>

Grid Line

这个分界线组成网格结构。 它们既可以是垂直的(“column grid lines”),也可以是水平的(“row grid lines”),并位于行或列的任一侧。 下面例中的黄线就是一个列网格线。

Grid Track

两个相邻网格线之间的空间。 你可以把它们想象成网格的列或行。 下面是第二行和第三行网格线之间的网格轨道。

Grid Cell

两个相邻的行和两个相邻的列网格线之间的空间。它是网格的一个“单元”。 下面是行网格线1和2之间以及列网格线2和3的网格单元。

Grid Area

四个网格线包围的总空间。 网格区域可以由任意数量的网格单元组成。 下面是行网格线1和3以及列网格线1和3之间的网格区域。

Grid 属性列表

Grid Container 的全部属性

  • display
  • grid-template-columns
  • grid-template-rows
  • grid-template-areas
  • grid-template
  • grid-column-gap
  • grid-row-gap
  • grid-gap
  • justify-items
  • align-items
  • justify-content
  • align-content
  • grid-auto-columns
  • grid-auto-rows
  • grid-auto-flowgrid

Grid Items 的全部属性

  • grid-column-start
  • grid-column-end
  • grid-row-start
  • grid-row-end
  • grid-column
  • grid-row
  • grid-area
  • justify-self
  • align-self

父容器(Grid Container)的属性

display

将元素定义为 grid contaienr,并为其内容建立新的网格格式化上下文(grid formatting context)。

值:

  • grid - 生成一个块级(block-level)网格inline-grid - 生成一个行级(inline-level)网格subgrid - 如果你的 grid container 本身就是一个 grid item(即,嵌套网格),你可以使用这个属性来表示你想从它的父节点获取它的行/列的大小,而不是指定它自己的大小。
.container {
 display: grid | inline-grid | subgrid;
}

注意:column, float, clear, 以及 vertical-align 对一个 grid container 没有影响

grid-template-columns / grid-template-rows

使用以空格分隔的多个值来定义网格的列和行。这些值表示轨道大小(track size),它们之间的空格代表表格线(grid line)。

.container {
 grid-template-columns: <track-size> ... | <line-name> <track-size> ...;
 grid-template-rows: <track-size> ... | <line-name> <track-size> ...;
}

例子:

(如果未显示的给网格线命名),轨道值之间仅仅有空格时,网格线会被自动分配数字名称:

.container {
 grid-template-columns: 40px 50px auto 50px 40px;
 grid-template-rows: 25% 100px auto;
}

但你可以给网格线指定确切的命名。 注意中括号里的网格线命名语法:

.container {
 grid-template-columns: [first] 40px [line2] 50px [line3] auto [col4-start] 50px [five] 40px [end];
 grid-template-rows: [row1-start] 25% [row1-end] 100px [third-line] auto [last-line];
}

需要注意的是,一个网格线可以有不止一个名字。例如,这里第2条网格线有两个名字:row1-end 和 row2-start:

.container {
 grid-template-rows: [row1-start] 25% [row1-end row2-start] 25% [row2-end];
}

如果你的定义中包含重复的部分,则可以使用repeat() 符号来简化写法:

.container {
 grid-template-columns: repeat(3, 20px [col-start]) 5%;
}

上面的写法和下面等价:

.container {
 grid-template-columns: 20px [col-start] 20px [col-start] 20px [col-start] 5%;
}

“fr”单位允许您将轨道大小设置为网格容器自由空间的一部分。 例如,下面的代码会将每个 grid item 为 grid container 宽度的三分之一:

.container {
 grid-template-columns: 1fr 1fr 1fr;
}

自由空间是在排除所有不可伸缩的 grid item 之后计算得到的。 在下面的示例中,fr单位可用的自由空间总量不包括50px:

.container {
 grid-template-columns: 1fr 50px 1fr 1fr;
}

grid-template-areas

通过引用 grid-area属性指定的网格区域的名称来定义网格模板。 重复网格区域的名称导致内容扩展到这些单元格。 点号表示一个空单元格。 语法本身提供了网格结构的可视化。

值:

  • <grid-area-name> - 使用 grid-area 属性设置的网格区域的名称. - 点号代表一个空网格单元none - 没有定义网格区域

举例:

.item-a {
 grid-area: header;
}
.item-b {
 grid-area: main;
}
.item-c {
 grid-area: sidebar;
}
.item-d {
 grid-area: footer;
}
.container {
 grid-template-columns: 50px 50px 50px 50px;
 grid-template-rows: auto;
 grid-template-areas: 
 "header header header header"
 "main main . sidebar"
 "footer footer footer footer";
}

这将创建一个四列宽三行高的网格。 整个第一行将由 header 区域组成。 中间一行将由两个 main 区域、一个空单元格和一个 sidebar 区域组成。 最后一行是footer区域组成。

你的声明中的每一行都需要有相同数量的单元格。

您可以使用任意数量的相邻的.来声明单个空单元格。 只要这些点号之间没有空格,他们就代表了一个单一的单元格。

需要注意的是你不是在用这个语法命名网格线,而是在命名区域。 当你使用这种语法时,区域两端的网格线实际上是自动命名的。 比如,如果网格区域的名称是foo,那么区域的起始的行网格线和列网格线名称是 foo-start,并且区域终点的行网格线和列网格线名称是 foo-end。 这意味着某些网格线可能有多个名称,比如上面的例子中最左边的一条网格线有三个名字:header-start,main-start 和 footer-start。

grid-template

在单个声明中定义 grid-template-rows、grid-template-columns、grid-template-areas 的简写。

值:

  • none - 将三个属性都设置为其初始值subgrid - 把 grid-template-rows 和 grid-template-columns 设置为 subgrid, 并且 grid-template-areas 设置为初始值grid-template-rows / <grid-template-columns - 把 grid-template-columns 和 grid-template-rows 设置为指定值, 与此同时, 设置 grid-template-areas 为 none
.container {
 grid-template: none | subgrid | <grid-template-rows> / <grid-template-columns>;
}

它也可以使用一个更复杂但相当方便的语法来指定这三个值。 一个例子:

.container {
 grid-template:
 [row1-start] "header header header" 25px [row1-end]
 [row2-start] "footer footer footer" 25px [row2-end]
 / auto 50px auto;
}

以上等价于:

.container {
 grid-template-rows: [row1-start] 25px [row1-end row2-start] 25px [row2-end];
 grid-template-columns: auto 50px auto;
 grid-template-areas: 
 "header header header" 
 "footer footer footer";
}

由于 grid-template 不会重置隐式网格属性(grid-auto-columns,grid-auto-rows和grid-auto-flow),而这可能是大多数情况下你想要做的。因此建议使用grid属性来代替grid-template。

grid-column-gap / grid-row-gap

指定网格线的大小,你可以把它想象为设置列/行之间的间距的宽度。

值:

  • line-size - 一个长度值
.container {
 grid-column-gap: <line-size>;
 grid-row-gap: <line-size>;
}

举例:

.container {
 grid-template-columns: 100px 50px 100px;
 grid-template-rows: 80px auto 80px; 
 grid-column-gap: 10px;
 grid-row-gap: 15px;
}

只能在列/行之间创建缝隙,而不是在外部边缘创建。

grid-gap

grid-row-gap 和 grid-column-gap 的缩写

.container {
 grid-gap: <grid-row-gap> <grid-column-gap>;
}

Example:

.container {
 grid-template-columns: 100px 50px 100px;
 grid-template-rows: 80px auto 80px; 
 grid-gap: 10px 15px;
}

如果没有指定 grid-row-gap,则会被设置为与 grid-column-gap 相同的值。

justify-items

沿着行轴对齐网格内的内容(与之对应的是 align-items, 即沿着列轴对齐),该值适用于容器内的所有的 grid items。

值:

  • start: 内容与网格区域的左端对齐end: 内容与网格区域的右端对齐center: 内容位于网格区域的中间位置stretch: 内容宽度占据整个网格区域空间(这是默认值)
.container {
 justify-items: start | end | center | stretch;
}

举例:

.container {
 justify-items: start;
}

.container{
 justify-items: end;
}

.container {
 justify-items: center;
}

.container {
 justify-items: stretch;
}

也可以通过给单个 grid item 设置justify-self属性来达到上述效果。

align-items

沿着列轴对齐grid item 里的内容(与之对应的是使用 justify-items 设置沿着行轴对齐),该值适用于容器内的所有 grid items。

值:

  • start: 内容与网格区域的顶端对齐end: 内容与网格区域的底部对齐center: 内容位于网格区域的垂直中心位置stretch: 内容高度占据整个网格区域空间(这是默认值)
.container {
 align-items: start | end | center | stretch;
}

举例:

.container {
 align-items: start;
}

.container {
 align-items: end;
}

.container {
 align-items: center;
}

.container {
 align-items: stretch;
}

也可以通过给单个 grid item 设置align-self属性来达到上述效果。

justify-content

有时,网格的总大小可能小于其网格容器的大小。如果你的所有 grid items 都使用像px这样的非弹性单位来设置大小,则可能发生这种情况。此时,你可以设置网格容器内的网格的对齐方式。 此属性沿着行轴对齐网格(与之对应的是 align-content, 沿着列轴对齐)。

值:

  • start - 网格与网格容器的左边对齐end - 网格与网格容器的右边对齐center - 网格与网格容器的中间对齐stretch - 调整g rid item 的大小,让宽度填充整个网格容器space-around - 在 grid item 之间设置均等宽度的空白间隙,其外边缘间隙大小为中间空白间隙宽度的一半space-between - 在 grid item 之间设置均等宽度空白间隙,其外边缘无间隙space-evenly - 在每个 grid item 之间设置均等宽度的空白间隙,包括外边缘
.container {
 justify-content: start | end | center | stretch | space-around | space-between | space-evenly; 
}

举例:

.container {
 justify-content: start;
}

.container {
 justify-content: end; 
}

.container {
 justify-content: center; 
}

.container {
 justify-content: stretch; 
}

.container {
 justify-content: space-around; 
}

.container {
 justify-content: space-between; 
}

.container {
 justify-content: space-evenly; 
}

align-content

有时,网格的总大小可能小于其网格容器的大小。如果你的所有 grid items 都使用像px这样的非弹性单位来设置大小,则可能发生这种情况。此时,你可以设置网格容器内的网格的对齐方式。 此属性沿着列轴对齐网格(与之对应的是 justify-content, 即沿着行轴对齐)。

值:

  • start - 网格与网格容器的顶部对齐end - 网格与网格容器的底部对齐center - 网格与网格容器的中间对齐stretch - 调整 grid item 的大小,让高度填充整个网格容器space-around - 在 grid item 之间设置均等宽度的空白间隙,其外边缘间隙大小为中间空白间隙宽度的一半space-between - 在 grid item 之间设置均等宽度空白间隙,其外边缘无间隙space-evenly - 在每个 grid item 之间设置均等宽度的空白间隙,包括外边缘
.container {
 align-content: start | end | center | stretch | space-around | space-between | space-evenly; 
}

举例:

.container {
 align-content: start; 
}

.container {
 align-content: end; 
}

.container {
 align-content: center; 
}

.container {
 align-content: stretch; 
}

.container {
 align-content: space-around; 
}

.container {
 align-content: space-between; 
}

.container {
 align-content: space-evenly; 
}

grid-auto-columns / grid-auto-rows

指定自动生成的网格轨道(又名隐式网格轨道)的大小。 隐式网格轨道在你显式的定位超出指定网格范围的行或列(使用 grid-template-rows/grid-template-columns)时被创建。

值:

  • <track-size> - 可以是一个长度值,一个百分比值,或者一个自由空间的一部分(使用 fr 单位)
.container {
 grid-auto-columns: <track-size> ...;
 grid-auto-rows: <track-size> ...;
}

为了说明如何创建隐式网格轨道,思考如下代码:

.container {
 grid-template-columns: 60px 60px;
 grid-template-rows: 90px 90px
}

这里创建了一个 2x2的网格。

但是,现在想象一下,使用 grid-column 和 grid-row 来定位你的网格项目,如下所示:

.item-a {
 grid-column: 1 / 2;
 grid-row: 2 / 3;
}
.item-b {
 grid-column: 5 / 6;
 grid-row: 2 / 3;
}

这里我们指定 .item-b开始于列网格线 5 并结束于在列网格线 6,但我们并未定义列网格线 5 或 6。因为我们引用不存在的网格线,宽度为0的隐式轨道的就会被创建用与填补间隙。我们可以使用 grid-auto-columns 和 grid-auto-rows属性来指定这些隐式轨道的宽度:

.container {
 grid-auto-columns: 60px;
}

grid-auto-flow

如果你存在没有显示指明放置在网格上的 grid item,则自动放置算法会自动放置这些项目。 而该属性则用于控制自动布局算法的工作方式。

值:

  • row - 告诉自动布局算法依次填充每行,根据需要添加新行column - 告诉自动布局算法依次填充每列,根据需要添加新列dense - 告诉自动布局算法,如果后面出现较小的 grid item,则尝试在网格中填充空洞
.container {
 grid-auto-flow: row | column | row dense | column dense
}

需要注意的是,dense 可能导致您的 grid item 乱序。

举例, 考虑如下 HTML:

<section class="container">
 <div class="item-a">item-a</div>
 <div class="item-b">item-b</div>
 <div class="item-c">item-c</div>
 <div class="item-d">item-d</div>
 <div class="item-e">item-e</div>
</section>

你定义一个有5列和2行的网格,并将 grid-auto-flow 设置为 row(这也是默认值):

.container {
 display: grid;
 grid-template-columns: 60px 60px 60px 60px 60px;
 grid-template-rows: 30px 30px;
 grid-auto-flow: row;
}

当把 grid item 放在网格上时,你只把其中两个设置了固定的位置:

.item-a {
 grid-column: 1;
 grid-row: 1 / 3;
}
.item-e {
 grid-column: 5;
 grid-row: 1 / 3;
}

因为我们将 grid-auto-flow 设置为row,所以我们的grid就像这样。 注意观察我们没有做设置的三个项目(item-b,item-c和item-d)是如何在剩余的行水平摆放位置的:

如果我们将 grid-auto-flow 设置为 column,则 item-b,item-c 和 item-d 以列的顺序上下摆放:

.container {
 display: grid;
 grid-template-columns: 60px 60px 60px 60px 60px;
 grid-template-rows: 30px 30px;
 grid-auto-flow: column;
}

grid

在单个属性中设置所有以下属性的简写:grid-template-rows,grid-template-columns,grid-template-areas,grid-auto-rows,grid-auto-columns和grid-auto-flow。 它同时也将 sets grid-column-gap 和 grid-row-gap 设置为它们的初始值,即使它们不能被此属性显示设置。

值:

  • none - 将所有子属性设置为其初始值<grid-template-rows> / <grid-template-columns> - 将 grid-template-rows 和 grid-template-columns 分别设置为指定值,将所有其他子属性设置为其初始值<grid-auto-flow> [<grid-auto-rows> [ / <grid-auto-columns>] ] - 接受所有与grid-auto-flow,grid-auto-rows和grid-auto-columns相同的值。 如果省略grid-auto-columns,则将其设置为为grid-auto-rows指定的值。 如果两者都被省略,则它们被设置为它们的初始值
.container {
 grid: none | <grid-template-rows> / <grid-template-columns> | <grid-auto-flow> [<grid-auto-rows> [/ <grid-auto-columns>]];
}

举例:

以下代码写法等价

.container {
 grid: 200px auto / 1fr auto 1fr;
}
.container {
 grid-template-rows: 200px auto;
 grid-template-columns: 1fr auto 1fr;
 grid-template-areas: none;
}

以下代码写法等价

.container {
 grid: column 1fr / auto;
}
.container {
 grid-auto-flow: column;
 grid-auto-rows: 1fr;
 grid-auto-columns: auto;
}

它也可用使用一个更复杂但相当方便的语法来一次设置所有内容。 你可以指定 grid-template-areas、grid-template-rows 以及 grid-template-columns,并将所有其他子属性设置为其初始值。 你现在所做的是在其网格区域内,指定网格线名称和内联轨道大小。 可以看下面的例子:

.container {
 grid: [row1-start] "header header header" 1fr [row1-end]
 [row2-start] "footer footer footer" 25px [row2-end]
 / auto 50px auto;
}

上述代码等价于

.container {
 grid-template-areas: 
 "header header header"
 "footer footer footer";
 grid-template-rows: [row1-start] 1fr [row1-end row2-start] 25px [row2-end];
 grid-template-columns: auto 50px auto; 
}

孩子(Grid Items)的属性

grid-column-start / grid-column-end / grid-row-start /grid-row-end

使用特定的网格线确定 grid item 在网格内的位置。grid-column-start/grid-row-start 属性表示grid item的网格线的起始位置,grid-column-end/grid-row-end属性表示网格项的网格线的终止位置。

值:

  • <line>: 可以是一个数字来指代相应编号的网格线,也可使用名称指代相应命名的网格线span <number>: 网格项将跨越指定数量的网格轨道span <name>: 网格项将跨越一些轨道,直到碰到指定命名的网格线auto: 自动布局, 或者自动跨越, 或者跨越一个默认的轨道
.item {
 grid-column-start: <number> | <name> | span <number> | span <name> | auto
 grid-column-end: <number> | <name> | span <number> | span <name> | auto
 grid-row-start: <number> | <name> | span <number> | span <name> | auto
 grid-row-end: <number> | <name> | span <number> | span <name> | auto
}

举例:

.item-a {
 grid-column-start: 2;
 grid-column-end: five;
 grid-row-start: row1-start
 grid-row-end: 3
}

.item-b {
 grid-column-start: 1;
 grid-column-end: span col4-start;
 grid-row-start: 2
 grid-row-end: span 2
}

如果没有声明 grid-column-end / grid-row-end,默认情况下,该网格项将跨越1个轨道。

网格项可以相互重叠。 您可以使用z-index来控制它们的堆叠顺序。

grid-column / grid-row

grid-column-start + grid-column-end, 和 grid-row-start + grid-row-end 的简写形式。

值:

  • <start-line> / <end-line> - 每个值的用法都和属性分开写时的用法一样
.item {
 grid-column: <start-line> / <end-line> | <start-line> / span <value>;
 grid-row: <start-line> / <end-line> | <start-line> / span <value>;
}

举例:

.item-c {
 grid-column: 3 / span 2;
 grid-row: third-line / 4;
}

如果没有指定结束行值,则该网格项默认跨越1个轨道。

grid-area

给 grid item 进行命名以便于使用 grid-template-areas 属性创建模板时来进行引用。另外也可以做为 grid-row-start + grid-column-start + grid-row-end + grid-column-end 的简写形式。

值:

  • <name> - 你的命名<row-start> / <column-start> / <row-end> / <column-end> - 可以是数字,也可以是网格线的名字
.item {
 grid-area: <name> | <row-start> / <column-start> / <row-end> / <column-end>;
}

举例:

给一个网格项命名

.item-d {
 grid-area: header
}

作为 grid-row-start + grid-column-start + grid-row-end + grid-column-end 的简写:

.item-d {
 grid-area: 1 / col4-start / last-line / 6
}

justify-self

沿着行轴对齐grid item 里的内容(与之对应的是 align-self, 即沿列轴对齐)。 此属性对单个网格项内的内容生效。

值:

  • start - 将内容对齐到网格区域的左端end - 将内容对齐到网格区域的右端center - 将内容对齐到网格区域的中间stretch - 填充网格区域的宽度 (这是默认值)

举例:

.item-a {
 justify-self: start;
}

.item-a {
 justify-self: end;
}

.item-a {
 justify-self: center;
}

.item-a {
 justify-self: stretch;
}

要为网格中的所有grid items 设置对齐方式,也可以通过 justify-items 属性在网格容器上设置此行为。

align-self

沿着列轴对齐grid item 里的内容(与之对应的是 justify-self, 即沿行轴对齐)。 此属性对单个网格项内的内容生效。

值:

  • start - 将内容对齐到网格区域的顶部end - 将内容对齐到网格区域的底部center - 将内容对齐到网格区域的中间stretch - 填充网格区域的高度 (这是默认值)
.item {
 align-self: start | end | center | stretch;
}

举例:

.item-a {
 align-self: start;
}

.item-a {
 align-self: end;
}

.item-a {
 align-self: center;
}

.item-a {
 align-self: stretch;
}

要为网格中的所有grid items 统一设置对齐方式,也可以通过 align-items 属性在网格容器上设置此行为。

译者:若愚老师

原文链接:https://zhuanlan.zhihu.com/p/33030746