小编Gra*_*ith的帖子

让 CSS 网格区域在移动设备上堆叠(单列布局)

我试图让 CSS 网格块在小屏幕上查看时彼此堆叠。我知道我可以编写媒体查询将两列更改为一列。但我认为网格可以在没有它们的情况下处理这个问题?

我想我可以通过自动调整列来实现这一点。但是,我想我可能误解了这是如何工作的?

.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  grid-template-rows: 1fr 1fr;
  grid-template-areas: "leftCol rightTop" "leftCol rightBottom";
  height: 100vh;
}

.leftCol {
  grid-area: leftCol;
  background-color: pink;
  width: 100%;
  height: 100;
}

.rightBottom {
  grid-area: rightBottom;
  background-color: yellow;
  width: 100%;
  height: 100;
}

.rightTop {
  grid-area: rightTop;
  background-color: blue;
  width: 100%;
  height: 100;
}
Run Code Online (Sandbox Code Playgroud)
<div class="grid-container">
  <div class="leftCol"></div>
  <div class="rightBottom"></div>
  <div class="rightTop"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

当屏幕低于 400 像素时,右侧栏就会消失。我原以为它们会叠在一起。

例如:

网格模型

CodePen 示例

css css-grid

5
推荐指数
1
解决办法
2481
查看次数

标签 统计

css ×1

css-grid ×1