设置网格项高度时网格列端不起作用

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

我正在尝试创建一个像这样的布局,但是当我尝试创建它时,如果我为网格元素提供高度,我将无法正确获得布局。

根据我的理解,网格元素默认会拉伸以填充它们分配到的网格区域。有人能指出我为什么会出现这种奇怪行为的正确方向吗?

在开发工具中我看到消息

display: block 属性可以防止 grid-column-end 出现 效果。

但是“网格”容器的所有子项不都应该是网格项,并且它们的显示属性并不重要吗?

    * {
      margin: 0;
      padding: 0;
    }

    body {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }

    .container {
      width: 600px;
      height: 400px;
      background-color: darkslategray;
      display: grid;
      grid-template-rows: 1fr 1fr 1fr;
      grid-template-columns: 1fr 1fr 1fr 1fr;
      gap: 10px;
    }

    .item {
      width: 100%;
      height: 50px; /* remove this line and it works */
    }

    .item1 {
      background-color: #0c9e9d;
      grid-area: 1 / 1 / 4 / 2;
    }

    .item2 {
      background-color: #2ecc71;
      grid-area: 1 / 2 / 2 / 4;
    }

    .item3 {
      background-color: #9b59b6;
      grid-area: 1 / 4 / 2 / 5;
    }

    .item4 {
      background-color: #3498db;
      grid-area: 2 / 4 / 4 / 5;
    }

    .item5 {
      background-color: #f1c40f;
    }

    .item6 {
      background-color: #e67e22;
    }

    .item7 {
      background-color: #e74c3c;
    }

    .item8 {
      background-color: lightskyblue;
    }
'''
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
  <div class="container">
    <div class="item item1"></div>
    <div class="item item2"></div>
    <div class="item item3"></div>
    <div class="item item4"></div>
    <div class="item item5"></div>
    <div class="item item6"></div>
    <div class="item item7"></div>
    <div class="item item8"></div>
  </div>

</body>
</html>
'''

css css-grid
1个回答
0
投票

这就是解决方案。

body {
  width: 100vw;
  height: 100vh;
  margin: 0;
}

.container {
  display: grid;
  grid-template-columns: auto auto auto auto;
  grid-gap: 10px;
  background-color: darkslategray;
  padding: 0;
  height: 100%;
}

.item {
    background-color: rgba(255, 255, 255, 0.8);
    text-align: center;
    padding: 20px 0;
    font-size: 30px;
}

.item1 {
  background-color: #0c9e9d;
  grid-area: 1 / 1 / span 3 / span 1;
}

.item2 {
  background-color: #2ecc71;
  grid-area: 1 / 2 / span 1 / span 2;
}

.item3 {
  background-color: #9b59b6;
}

.item4 {
  background-color: #f1c40f;
}

.item5 {
  background-color: #e67e22;
}

.item6 {
  background-color: #3498db;
  grid-area: 2 / 4 / span 2 / span 1;
}

.item7 {
  background-color: #e74c3c;
}

.item8 {
  background-color: lightskyblue;
}
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
  <div class="container">
    <div class="item item1"></div>
    <div class="item item2"></div>
    <div class="item item3"></div>
    <div class="item item4"></div>
    <div class="item item5"></div>
    <div class="item item6"></div>
    <div class="item item7"></div>
    <div class="item item8"></div>
  </div>
</body>

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