html
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Grid 布局示例</title>
<style>
.grid-container {
display: grid;
grid-template-columns: 200px 1fr; /* 两列 200px 1fr自适应*/
grid-template-rows: 100px 1fr 100px; /* 三行 100px 1fr自适应 100px*/
gap: 10px; /*网格间距10px*/
height: 100vh;
background-color: #f4f4f4;
}
.item {
padding: 20px;
background-color: lightblue;
border: 1px solid #333;
text-align: center;
}
/* 针对子元素的行列设置,可以先根据这里绘制出一个简化版表格,然后根据表格的线来理解 2/3的含义 ,-1代表从当前开始到结束 */
/* 如例:
1 2 3 4
| | | |
| 列1 | 列2 | 列3 |
| | | |
*/
.header {
grid-column: 1 / -1;
}
.sidebar {
grid-row: 2 / 3;
}
.main {
grid-row: 2; /* 简写方式 2/3*/
grid-column: 2;
}
.footer {
grid-column: 1 / -1;
}
</style>
</head>
<body style="margin: 0;">
<div class="grid-container">
<div class="item header">Header</div>
<div class="item sidebar">Sidebar</div>
<div class="item main">Main Content</div>
<div class="item footer">Footer</div>
</div>
</body>
</html>