如何创建一个CSS网格没有相同数量的列项目[关闭]

qnyhuwrf  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(90)

已关闭,此问题需要更focused。目前不接受答复。
**想改善这个问题吗?**更新问题,使其仅通过editing this post关注一个问题。

5天前关闭。
Improve this question
我试图将这个模式构建到css网格中,但无法找到如何实现它。我不确定这是否可能在100%的css。
Layout image
如果有人能帮我,我会非常高兴。。谢谢帮助:)

eqfvzcg8

eqfvzcg81#

联系我们

<div class="grid-container">
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
</div>

css:

.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 10px;
  justify-items: center;
}

.grid-item {
  background-color: #ddd;
  padding: 80px;
  position: relative;
}

.grid-item:nth-child(1) {
  grid-column: 2;
  grid-row: 1;
}

.grid-item:nth-child(2) {
  grid-column: 1;
  grid-row: 2;
}

.grid-item:nth-child(3) {
  grid-column: 2;
  grid-row: 2;
}

.grid-item:nth-child(4) {
  grid-column: 3;
  grid-row: 2;
}

.grid-item:nth-child(5) {
  grid-column: 1;
  grid-row: 3;
}

.grid-item:nth-child(6) {
  grid-column: 2;
  grid-row: 3;
}

.grid-item:nth-child(7) {
  grid-column: 3;
  grid-row: 3;
}
.grid-item:nth-child(2), .grid-item:nth-child(4), .grid-item:nth-child(5), .grid-item:nth-child(7) {
  margin-top: -80px;
  height: 0;
}
bogh5gae

bogh5gae2#

您可以创建一个3列网格,并在每列中嵌套一个Flex容器或另一个网格。

.grid {
  width: fit-content;
  margin: auto;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.flex {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 10px;
}

.box {
  height: 100px; /* for presentation */
  width: 100px; /* for presentation */
  background-color: #DDD; /* for presentation */
  border: 1px solid #999; /* for presentation */
}
<div class="grid">
  <div class="flex">
    <div class="box"></div>
    <div class="box"></div>
  </div>
  <div class="flex">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
  </div>
  <div class="flex">
    <div class="box"></div>
    <div class="box"></div>
  </div>
</div>

但如果你只使用flexbox,它会更简单(样式更少):

.flex {
  display: flex;
  justify-content: center;
  gap: 10px;
}

.flex-column {
  flex-direction: column;
}

.box {
  height: 100px; /* for presentation */
  width: 100px; /* for presentation */
  background-color: #DDD; /* for presentation */
  border: 1px solid #999; /* for presentation */
}
<div class="flex flex-row">
  <div class="flex flex-column">
    <div class="box"></div>
    <div class="box"></div>
  </div>
  <div class="flex flex-column">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
  </div>
  <div class="flex flex-column">
    <div class="box"></div>
    <div class="box"></div>
  </div>
</div>

相关问题