<div class="wrapper"> <div>Item One</div> <div>Item Two</div> <div>Item Three</div> <div>Item Four</div> <div>Item Five</div> </div>
.wrapper { display: grid; }
.容器列的划分;单位可以是绝对的或相对的;可以混合使用
grid-template-columns: px | % | fr | auto | repeat() | minmax() | max-content | min-content;
grid-template-columns: 100px 200px 300px;
grid-template-columns: 10% 70% 20%;
grid-template-columns: 1fr 2rem 1fr;
grid-template-columns: 1fr 2fr 1fr;
grid-template-columns: auto 1fr auto;
grid-template-columns: 1fr auto 1fr;
grid-template-columns: 2rem auto 1fr;
grid-template-columns: repeat(3, 1fr);
grid-template-columns: repeat(12, 1fr);
grid-template-columns: repeat(3, 100px 200px);
.如果想 自适应 划分,可以使用:auto-fill、auto-fit;其中,minmax():格子最小和最大尺寸
.auto-fill:当容器有 剩余闲置空间,生成 空轨道;尽可能多地填充列,即使这些列是空的
grid-template-columns: repeat(auto-fill, minmax(50px, 1fr));
auto-fill
.auto-fit:不会生成 轨道,剩余空间被分配到各个子项中;根据实际内容调整列的数量和宽度,收缩多余的列
grid-template-columns: repeat(auto-fit, minmax(50px, 1fr));
auto-fit
.列宽由最大宽度子项绝对 - maximal content contribution will be defined by the size of the largest sentence
.让列尽可能宽以适应最长的内容,保证内容不会被截断
.列宽由子项中的某个最大单词宽度决定 - minimal content contribution will be defined by the size of the largest word
.让列尽可能窄,仅适应最窄的内容,可能导致较多剩余空间;紧凑的列布局
grid-template-rows: px | % | fr | auto | repeat() | minmax();
grid-auto-rows: <length> | <percentage> | min-content | max-content | auto;
grid-auto-columns: <length> | <percentage> | min-content | max-content | auto;
grid-auto-flow: row | column | dense | row dense | column dense;
默认 row 排列 - 划分列的行优先
column 排列 - 划分行的列优先
column 排列 - 未划分行的列优先
默认拉伸填充
水平居中
垂直居中
默认开始 normal/start/flex-start
水平居中 center
水平结束 end/flex-end
.box div:nth-child(1) { grid-column-start: 1; grid-column-end: 3; }
.box div:nth-child(1) { grid-column: 1/3; }
.box div:nth-child(1) { grid-column: span 2; }
.box div:nth-child(2) { grid-row: 1/3; }
.box div:nth-child(2) { grid-row: 1/3; grid-column: 3/4; }
.box div:nth-child(2) { grid-row: span 2; }
.aside { display: grid; grid-template-columns: 8rem 1fr; gap: 1rem; }
.aside { display: grid; grid-template-columns: minmax(150px, 25%) 1fr; gap: 1rem; }
.cup { display: grid; grid-template-columns: 8rem 1fr 4rem; gap: 1rem; }
.grid { display: grid; grid-template-columns: repeat(12, 1fr); gap: 1rem; }
.grid { display: grid; grid-template-columns: 1fr 2fr 1fr; gap: 1rem; }
.grid { width: 300px; height: 200px; display: grid; grid-template-rows: auto 1fr auto; gap: 1rem; }
.page { display: grid; grid-template-columns: auto 1fr auto; grid-template-rows: auto auto 1fr auto; grid-template-areas: 'header header header' 'nav nav nav' 'aside article article' 'footer footer footer'; min-height: 100vh; gap: 1rem; } header { grid-area: header; } nav { grid-area: nav; } aside { grid-area: aside; } article { grid-area: article; } footer { grid-area: footer; }
.sudoku { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; }
.item:nth-child(1), .item:nth-child(5) { grid-column: span 2 } .item:nth-child(2), .item:nth-child(3) { grid-row: span 2; }
.item:nth-child(1) { grid-column: 1/3; } .item:nth-child(2) { grid-column: 3/4; grid-row: 1/3; } .item:nth-child(3) { grid-row: 2/4; } .item:nth-child(5) { grid-column: 2/4; }
<div class="wrap"> <ul> <li>Lorem.</li> <li>Sapiente.</li> <li>Placeat.</li> <li>Rem.</li> <li>Magnam.</li> </ul> </div>
.wrap { display: grid; grid-template-rows: 0fr;//MUST be 0fr, NOT 0 transition: .5s; } .wrap.active { grid-template-rows: 1fr; } .wrap ul { overflow: hidden; }
.ale { display: grid; grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr)); gap: 1rem; }
.case0 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.case2 { display: grid; grid-template-columns: repeat(3, 80px); grid-template-rows: repeat(3, 80px); gap: 10px; margin: 1rem auto; width: 100%; height: 320px; background-color: #eee; }
.self { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; width: 320px; height: 320px; margin: 1rem auto; background-color: #eee; } .self div:nth-child(1) { justify-self: start; align-self: start; } .self div:nth-child(2) { justify-self: center; align-self: start; } .self div:nth-child(3) { justify-self: end; align-self: start; } .self div:nth-child(4) { justify-self: start; align-self: center; } .self div:nth-child(5) { justify-self: center; align-self: center; } .self div:nth-child(6) { justify-self: end; align-self: center; } .self div:nth-child(7) { justify-self: start; align-self: end; } .self div:nth-child(8) { justify-self: center; align-self: end; } .self div:nth-child(9) { justify-self: end; align-self: end; }