【发布时间】:2021-09-05 16:42:00
【问题描述】:
我正在尝试使用 css 创建一组从三列优雅降级为一列的列。通常我会这样做,但要求是每列中的项目应保持固定大小,并且列之间应始终保持 16px 的间距。
这就是我要找的东西:
但是,当我向列添加宽度属性时,它们之间的间距会变得更大。我对如何在屏幕更改大小时保持列相同感到困惑。我知道不可避免地越小,我将不得不使用媒体查询更改列宽,以使它们变成两列,然后变成一列,所以这不是问题。
哦,当然,这是代码和codepen link。我建议用 SO 编辑器全屏运行它,因为我没有做过任何响应式 css,列只是浓缩成一个大块,很难看到发生了什么。
body {
margin: 0;
padding: 40px;
}
.row {
display: flex;
}
.tile {
width: 300px;
height: 300px;
background-color: #ddd;
margin-right: 8px;
margin-left: 8px;
}
.col {
width: 33.33%;
}
<div class="row">
<div class="col">
<div class="tile"></div>
</div>
<div class="col">
<div class="tile"></div>
</div>
<div class="col">
<div class="tile"></div>
</div>
</div>
【问题讨论】:
-
您不需要
col类中的 width 属性。让它们中的子元素确定(因为它们的大小必须固定)。将列与 justify-center 粘合在一起,并将 16px 的 column-gap 属性添加到row类。您也不需要tile类的边距。
标签: html css responsive-design