我正在尝试使用div创建一个2x2网格。某些div可能包含图像,但可能会将其设置为背景,选项为background-size: cover
。
这是我创建的笔:http://codepen.io/qarlo/pen/vLEprq
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
margin: auto;
max-width: 960px;
width: 80%;
}
.container__item {
align-content: center;
border: 1px solid #333;
display: flex;
flex-basis: 1;
font-size: 3em;
justify-content: center;
height: 100%;
margin-bottom: 1em;
min-height: 300px;
width: 47%;
}
<div class="container">
<div class="container__item">?</div>
<div class="container__item">?</div>
<div class="container__item">?</div>
<div class="container__item">?</div>
</div>
我想强制div为正方形并在调整大小时保持纵横比。我错误地希望这对Flexbox来说很简单,但除非我遗漏了什么,否则我错了。
答案 0 :(得分:11)
要保持商品的宽高比,一种非常简单的方法是使用CSS Viewport units
我修改了你的笔,看看这些单位是如何运作的:http://codepen.io/vladbicu/pen/wMBmOb
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
margin: auto;
max-width: 960px;
width: 80%;
}
.container__item {
align-content: center;
border: 1px solid #333;
display: flex;
flex-basis: 1;
font-size: 3em;
justify-content: center;
margin-bottom: 1em;
// maintain aspect ratio
width: 30vw;
height: 30vw;
}
希望它有所帮助。
答案 1 :(得分:4)
使用old "padding-bottom" trick获得固定宽高比。尽管如此,还是需要额外的div:
.container {
margin: auto;
width: 80%;
max-width: 960px;
}
.container__square {
float: left;
position: relative;
padding-bottom: 50%;
width: 50%;
background: linear-gradient(45deg, #CCC, #000, #CCC);
}
.container__square__item {
position: absolute;
top: 1em;
bottom: 1em;
left: 1em;
right: 1em;
border: 1px solid #333;
background: #FFF;
}
/* clearfix */
.container::after {
content: "";
display: block;
clear: both;
}
&#13;
<div class="container">
<div class="container__square">
<div class="container__square__item">?</div>
</div>
<div class="container__square">
<div class="container__square__item">?</div>
</div>
<div class="container__square">
<div class="container__square__item">?</div>
</div>
<div class="container__square">
<div class="container__square__item">?</div>
</div>
</div>
&#13;
答案 2 :(得分:1)
如果您想使用弹性框布局,请猜测您必须至少设置min-height
以维持重新调整大小的宽高比。
这是一个快速而又肮脏的例子。
function setCellsMinHeight (parentSelector, cellsMargin, aspect) {
var winWidth = window.innerWidth,
containerList = document.querySelectorAll(parentSelector),
containerArray = Array.prototype.slice.call(containerList),
childMargin = cellsMargin;
containerArray.forEach(function(elem) {
var containerWidth = elem.offsetWidth,
childCount = elem.children.length,
childWidth = (containerWidth - ((childMargin * 2) * childCount)) / childCount,
childMinHeight = (childWidth / 100) * aspect;
for (i = 0; i < childCount; i++) {
elem.children[i].style.margin = childMargin + "px";
elem.children[i].style.minHeight = childMinHeight + "px";
}
});
}
window.onresize = function(event) {
setCellsMinHeight('.container', 4, 100);
};
setCellsMinHeight('.container', 4, 100);
body {
margin: 0;
}
.container {
display: flex;
flex-flow: row wrap;
align-items: stretch;
max-width: 100%;
margin: 0 auto;
}
.content-cell {
flex: 1;
background-color: #ccc;
}
@media (min-width: 801px) {
.container {
max-width: 800px;
}
}
<div class="container">
<div class="content-cell"></div>
<div class="content-cell"></div>
</div>
<div class="container">
<div class="content-cell"></div>
<div class="content-cell"></div>
</div>
希望它有所帮助。
答案 3 :(得分:-1)
据我所知,我认为你总是想要灵活的盒子,所以你可以使用JavaScript来执行这个动作。
这就是你如何维护灵活的盒子
或者您可以使用外部库