--- title: CSS格線布局 slug: Web/CSS/CSS_Grid_Layout tags: - CSS - 參考 - 布局 - 柵格 - 示列 - 網格布局 translation_of: Web/CSS/CSS_Grid_Layout ---
CSS 格線佈局長於把頁面的主要區域分離、或是在 HTML 原生語法構建的區域間,定義大小、位置、層次等方面的關聯。
格線佈局使作者能夠對齊元素為行和列,就像表格一樣,。然而,格線佈局可以更輕易的達成比一般表格更多元化的排版。例如,一個網格容器的子元素可以定位自己和層重疊,類似於CSS定位元素。
以下示例展示了一個三列軌道格線,其中創建的行數最小為 100 像素,最大為 auto。Items 已經使用基於線放置在網格的位置上。
* {box-sizing: border-box;}
.wrapper {
max-width: 940px;
margin: 0 auto;
}
.wrapper > div {
border: 2px solid rgb(233,171,88);
border-radius: 5px;
background-color: rgba(233,171,88,.5);
padding: 1em;
color: #d9480f;
}
<div class="wrapper"> <div class="one">One</div> <div class="two">Two</div> <div class="three">Three</div> <div class="four">Four</div> <div class="five">Five</div> <div class="six">Six</div> </div>
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 10px;
grid-auto-rows: minmax(100px, auto);
}
.one {
grid-column: 1 / 3;
grid-row: 1;
}
.two {
grid-column: 2 / 4;
grid-row: 1 / 3;
}
.three {
grid-row: 2 / 5;
grid-column: 1;
}
.four {
grid-column: 3;
grid-row: 3;
}
.five {
grid-column: 2;
grid-row: 4;
}
.six {
grid-column: 3;
grid-row: 4;
}
{{ EmbedLiveSample('example', '500', '440') }}
| 格式 | 狀態 | 意見 |
|---|---|---|
| {{ SpecName('CSS3 Grid') }} | {{ Spec2('CSS3 Grid') }} | 初始定義 |