如何创建跨 2 列和 2 行的 CSS 网格布局框?

问题描述 投票:0回答:3

我已经按照最新的 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>

理想情况下,我希望能够在网格父级中设置所有网格大小调整属性,然后仅定义网格项 A 中的属性以跨越 2 列和行。

当前指定每个网格区域并附加一个独特的类,如下所示:

.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 }
    
html css grid-layout css-grid
3个回答
59
投票
如果您不想为每个网格项定义网格区域,则不要使用

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>

codepen 演示


26
投票
而不是像 @Michael Benjamin 的答案那样告诉你的元素应该在哪一列/哪一行开始/结束

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;
    

3
投票
按照你的计划去做;删除无用的类。

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>

© www.soinside.com 2019 - 2024. All rights reserved.