CSS 网格 | 如何自动调整不同大小的列?

Vin*_*oft 2 html css css-grid

我正在尝试使用 CSS 网格(第一次),我正在努力解决一些基本问题。

我正在构建一个包含三个组件的页脚。

  #footer-content {
    position: relative;
    margin: 0 auto;
    padding: 0 20px;
    max-width: $max-width;
    width: 100%;
    height: 100%;
    color: white;

    display: grid;
    grid-template-columns: 12% 12% 76%;
}
Run Code Online (Sandbox Code Playgroud)

对于手机:

@media only screen and (max-width: 800px) {
  #footer-content {
      grid-template-columns: 50% 50% 100%;
    }
}
Run Code Online (Sandbox Code Playgroud)

上面代码的问题:第三个元素(宽度 100%)没有被推到下一行,而是出现在屏幕之外。

使用grid-template-columns: repeat(auto-fit, 50% 50% 100%);不起作用。

使用grid-template-columns: repeat(auto-fit, 50%);作品:第三个元素被向下推,只有 50% 的宽度而不是 100%。

问题:如何使用 CSS 网格自动适应不同大小的三列?

Pau*_*e_D 5

网格列不换行,因此这grid-template-columns: 50% 50% 100%;列没有意义。您将定义两列并告诉第三个元素在下一行跨越它们。

.box {
  height: 150px;
  background: lightblue;
  border: 1px solid grey;
}

#footer-content {
  display: grid;
  grid-template-columns: 50% 50%;
  grid-gap: .25em;
}

.box:nth-child(3) {
  grid-column: span 2;
}
Run Code Online (Sandbox Code Playgroud)
<div id="footer-content">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>
Run Code Online (Sandbox Code Playgroud)