【发布时间】:2018-05-13 20:57:01
【问题描述】:
有没有办法让宽度基于 fr 单位的网格单元格动态调整它们的高度以保持正方形?
另外,我希望做到这一点没有 JS
下面的小提琴有一些示例代码。具有“sqaure”类的 div 是我想要动态调整它们的高度以匹配它们的宽度(这是 1fr 所以它会改变)
https://jsfiddle.net/bpk0sLvL/403/
.holder {
display: grid;
grid-template-columns: 1fr 1fr;
column-gap: 4px;
row-gap: 4px;
}
.box {
background-color: #ccc;
}
.wide {
grid-column-start: 1;
grid-column-end: 3;
}
<div class="holder">
<div class="box wide">
<p>This can be any height</p>
<p>All these divs are sized based on fr, so I want to have the two square divs stay square as they dynamically resize </p>
</div>
<div class="box sqaure">
This needs to be a sqaure
</div>
<div class="box sqaure">
This needs to be a square as well
</div>
</div>
【问题讨论】:
-
你可以试试填充技巧
-
填充技巧是什么?
-
尝试
padding-top:75%在你的广场课上看看 -
@GeorgeKendros 也许check this post 关于如何保持 div 的纵横比