我已经按照最新的 CSS 网格规范创建了一个网格布局,但还不完全熟悉它。我正在尝试创建以下布局,而不必为每个网格子项定义网格区域。
body {
max-width: 1024px;
margin: 10px auto;
}
.grid {
display: grid;
grid-gap: 10px;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas: "a a b" "a a c" "d e f";
}
.grid__thing {
background-color: rebeccapurple;
}
.a {
grid-area: a;
}
.b {
grid-area: b;
}
.c {
grid-area: c;
}
.d {
grid-area: d;
}
.e {
grid-area: e;
}
.f {
grid-area: f;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="grid">
<div class="grid__thing a">
<img src="https://placehold.it/1360x880" alt="" />
</div>
<div class="grid__thing b">
<img src="https://placehold.it/660x405" alt="" />
</div>
<div class="grid__thing c">
<img src="https://placehold.it/660x405" alt="" />
</div>
<div class="grid__thing d">
<img src="https://placehold.it/660x405" alt="" />
</div>
<div class="grid__thing e">
<img src="https://placehold.it/1327x817" alt="" />
</div>
<div class="grid__thing f">
<img src="https://placehold.it/1327x817" alt="" />
</div>
</div>
当前指定每个网格区域并附加一个独特的类,如下所示:
.grid {
display: grid;
grid-gap: 10px;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas: "a a b"
"a a c"
"d e f";
.a {
grid-area: a;
}
.b {
grid-area: b;
}
.c {
grid-area: c;
}
.d {
grid-area: d;
}
.e {
grid-area: e;
}
.f {
grid-area: f;
}
想做这样的事情,这样我就不必为每个网格项创建唯一的 CSS 类:
.grid {
display: grid;
grid-gap: 10px;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas: "a a b"
"a a c"
"d e f";
}
.a {
// The only unique selector, so this is the only thing that
// should be given unique styling
}
grid-template-areas
属性,该属性要求您为每个网格项定义名称。相反,只需在容器上使用
grid-template-columns
和
grid-template-rows
即可。然后使用
grid-column
和
grid-row
将 2x2 尺寸应用到第一个网格项。
grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 100px);
grid-gap: 10px;
}
grid-item:first-child {
grid-column: 1 / 3; /* span from grid column line 1 to 3 (i.e., span 2 columns) */
grid-row: 1 / 3; /* same concept, but for rows */
}
/* non-essential decorative styles */
grid-item {
background-color: aqua;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.5em;
}
<grid-container>
<grid-item>A</grid-item>
<grid-item>B</grid-item>
<grid-item>C</grid-item>
<grid-item>D</grid-item>
<grid-item>E</grid-item>
<grid-item>F</grid-item>
</grid-container>
grid-column: 1 / 3;
您还可以告诉您的元素应该有多少行/列span
grid-column: span 2;
/*same rule as*/
grid-column-start: span 2;
grid-column-end: auto;
这样,元素的放置方式就像放置在文档流中一样,但它会跨越多个列/行。您仍然可以将此规则与起点 ala 结合起来
grid-column: 1 / span 2;
/*same rule as*/
grid-column-start: 1;
grid-column-end: span 2;
当然你可以用 column
切换
row
使元素跨越多行
grid-row: span 2;
body {
max-width: 1024px;
margin: 10px auto;
}
.grid {
display: grid;
grid-gap: 10px;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas: "a a" "a a";
}
.grid__thing {
background-color: rebeccapurple;
}
.a {
grid-area: a;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="grid">
<div class="grid__thing a">
<img src="https://placehold.it/1360x880" alt="" />
</div>
<div class="grid__thing">
<img src="https://placehold.it/660x405" alt="" />
</div>
<div class="grid__thing">
<img src="https://placehold.it/660x405" alt="" />
</div>
<div class="grid__thing">
<img src="https://placehold.it/660x405" alt="" />
</div>
<div class="grid__thing">
<img src="https://placehold.it/1327x817" alt="" />
</div>
<div class="grid__thing">
<img src="https://placehold.it/1327x817" alt="" />
</div>
</div>