CSS 网格布局(Grid)
Header
Menu
Main
Right
Footer
代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
.item1 { grid-area: header; }
.item2 { grid-area: menu; }
.item3 { grid-area: main; }
.item4 { grid-area: right; }
.item5 { grid-area: footer; }
.grid-container {
display: grid;
grid-template-areas:
'header header header header header header'
'menu main main main right right'
'menu footer footer footer footer footer';
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<h2>Grid 布局</h2>
<p>此网格布局包含六列和三行:</p>
<div class="grid-container">
<div class="item1">Header</div>
<div class="item2">Menu</div>
<div class="item3">Main</div>
<div class="item4">Right</div>
<div class="item5">Footer</div>
</div>
</body>
</html>
网格布局
CSS 网格布局模块(CSS Grid Layout Module)提供了带有行和列的基于网格的布局系统,它使网页设计变得更加容易,而无需使用浮动和定位。
浏览器支持
所有现代浏览器均支持网格属性。
57.0 | 16.0 | 52.0 | 10 | 44 |
网格元素
网格布局由一个父元素以及一个或多个子元素组成:
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
<div class="grid-item">7</div>
<div class="grid-item">8</div>
<div class="grid-item">9</div>
</div>
1
2
3
4
5
6
7
8
9
Display 属性
当 HTML 元素的 display
属性设置为 grid
或 inline-grid
时,它就会成为网格容器。
实例1
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
background-color: #2196F3;
padding: 10px;
}
.grid-item {
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid rgba(0, 0, 0, 0.8);
padding: 20px;
font-size: 30px;
text-align: center;
}
</style>
</head>
<body>
<h2>display 属性:</h2>
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
<div class="grid-item">7</div>
<div class="grid-item">8</div>
<div class="grid-item">9</div>
</div>
<p>请将 <em>display</em> 属性设置为 <em>grid</em>,以生成块级的网格容器。</p>
</body>
</html>
实例2
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: inline-grid;
grid-template-columns: auto auto auto;
background-color: #2196F3;
padding: 10px;
}
.grid-item {
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid rgba(0, 0, 0, 0.8);
padding: 20px;
font-size: 30px;
text-align: center;
}
</style>
</head>
<body>
<h2>display 属性:</h2>
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
<div class="grid-item">7</div>
<div class="grid-item">8</div>
<div class="grid-item">9</div>
</div>
<p>请将 <em>display</em> 属性设置为 <em>inline-grid</em>,以生成行内的网格容器。</p>
</body>
</html>
网格容器的所有直接子元素将自动成为网格项目。
网格列(Grid Columns)
网格项的垂直线被称为列。
网隔行(Grid Rows)
网格项的水平线被称为行。
网格间隙(Grid Gaps)
每列/行之间的间隔称为间隙。
您可以通过使用以下属性之一来调整间隙大小:
- grid-column-gap
- grid-row-gap
- grid-gap
实例1
grid-column-gap
属性设置列之间的间隙:
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-column-gap: 50px;
grid-template-columns: auto auto auto;
background-color: #2196F3;
padding: 10px;
}
.grid-item {
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid rgba(0, 0, 0, 0.8);
padding: 20px;
font-size: 30px;
text-align: center;
}
</style>
</head>
<body>
<h2>grid-column-gap 属性:</h2>
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
<div class="grid-item">7</div>
<div class="grid-item">8</div>
<div class="grid-item">9</div>
</div>
<p>请使用 <em>grid-column-gap</em> 属性调整列之间的空间。</p>
</body>
</html>
实例2
grid-row-gap
属性设置行之间的间隙:
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-row-gap: 50px;
grid-template-columns: auto auto auto;
background-color: #2196F3;
padding: 10px;
}
.grid-item {
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid rgba(0, 0, 0, 0.8);
padding: 20px;
font-size: 30px;
text-align: center;
}
</style>
</head>
<body>
<h2>grid-row-gap 属性:</h2>
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
<div class="grid-item">7</div>
<div class="grid-item">8</div>
<div class="grid-item">9</div>
</div>
<p>请使用 <em>grid-row-gap</em> 属性来调整行间距。</p>
</body>
</html>
实例3
grid-gap
属性是 grid-row-gap 和 grid-column-gap 属性的简写属性:
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-gap: 50px 100px;
grid-template-columns: auto auto auto;
background-color: #2196F3;
padding: 10px;
}
.grid-item {
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid rgba(0, 0, 0, 0.8);
padding: 20px;
font-size: 30px;
text-align: center;
}
</style>
</head>
<body>
<h2>grid-gap 属性:</h2>
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
<div class="grid-item">7</div>
<div class="grid-item">8</div>
<div class="grid-item">9</div>
</div>
<p>请使用 <em>grid-gap</em> 属性来调整列<em>和</em>行之间的空间。</p>
</body>
</html>
实例4
grid-gap
属性还可用于将行间隙和列间隙设置为一个值:
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-gap: 50px;
grid-template-columns: auto auto auto;
background-color: #2196F3;
padding: 10px;
}
.grid-item {
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid rgba(0, 0, 0, 0.8);
padding: 20px;
font-size: 30px;
text-align: center;
}
</style>
</head>
<body>
<h2>grid-gap 属性:</h2>
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
<div class="grid-item">7</div>
<div class="grid-item">8</div>
<div class="grid-item">9</div>
</div>
<p>请使用 <em>grid-gap</em> 属性来调整列<em>和</em>行之间的空间。</p>
</body>
</html>
网格行(Grid Lines)
列之间的线称为列线(column lines)。行之间的线称为行线(row lines)。
当把网格项目放在网格容器中时,请引用行号:
实例1
把网格项目放在列线 1,并在列线 3 结束它:
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
.item1 {
grid-column-start: 1;
grid-column-end: 3;
}
</style>
</head>
<body>
<h2>网格行</h2>
<div class="grid-container">
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
<div class="item6">6</div>
<div class="item7">7</div>
<div class="item8">8</div>
</div>
<p>您可以在放置网格项时引用行号。</p>
</body>
</html>
实例2
把网格项目放在行线 1,并在行线 3 结束它:
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
.item1 {
grid-row-start: 1;
grid-row-end: 3;
}
</style>
</head>
<body>
<h2>网格行</h2>
<div class="grid-container">
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
<div class="item6">6</div>
<div class="item7">7</div>
<div class="item8">8</div>
</div>
<p>您可以在放置网格项时引用行号。</p>
</body>
</html>
所有 CSS Grid 属性
属性 | 描述 |
---|---|
column-gap | 指定列之间的间距 |
gap | row-gap 与 column-gap 的简写属性 |
grid | grid-template-rows,grid-template-columns, grid-template-areas, grid-auto-rows, grid-auto-columns, 和 grid-auto-flow 的简写属性 |
grid-area | 为网格项目指定一个名称, 或者是 grid-row-start, grid-column-start, grid-row-end, 与 grid-column-end 的简写属性 |
grid-auto-columns | 指定默认列大小 |
grid-auto-flow | 指定如何在网格中插入自动放置的项 |
grid-auto-rows | 指定默认行大小 |
grid-column | 指定 grid-column-start 与 grid-column-end 的简写属性 |
grid-column-end | 指定网格项的结束位置 |
grid-column-gap | 指定列之间间隙的大小 |
grid-column-start | 指定网格项从何处开始 |
grid-gap | A shorthand property for the grid-row-gap and grid-column-gap properties |
grid-row | 指定 grid-row-start 与 grid-row-end 的简写属性 |
grid-row-end | 指定网格项的结束位置 |
grid-row-gap | 指定行间距的大小 |
grid-row-start | 指定网格项从何处开始 |
grid-template | 指定 grid-template-rows, grid-template-columns 与 grid-areas 的简写属性 |
grid-template-areas | 指定如何使用命名网格项显示列和行 |
grid-template-columns | 指定列的大小,以及网格布局中的列数 |
grid-template-rows | 指定网格布局中行的大小 |
row-gap | 指定网格行之间的间距 |