我正在使用响应式三网格布局。让我们说每个都是33%宽度,如果一个网格/ div是空的,我希望其他两个扩展到50%宽度,如果2个网格/ div是空的,我希望剩下一个是100%宽度。 它可以纯粹由CSS完成吗?
<div class="section group">
<div class="col span_1_of_3">
This is column 1
</div>
<div class="col span_1_of_3">
This is column 2
</div>
<div class="col span_1_of_3">
This is column 3
</div>
</div>
/* SECTIONS */
.section {
clear: both;
padding: 0px;
margin: 0px;
}
/* COLUMN SETUP */
.col {
display: block;
float:left;
margin: 1% 0 1% 1.6%;
}
.col:first-child { margin-left: 0; }
/* GROUPING */
.group:before,
.group:after { content:""; display:table; }
.group:after { clear:both;}
.group { zoom:1; /* For IE 6/7 */ }
/* GRID OF THREE */
.span_3_of_3 { width: 100%; }
.span_2_of_3 { width: 66.13%; }
.span_1_of_3 { width: 32.26%; }
/* GO FULL WIDTH BELOW 480 PIXELS */
@media only screen and (max-width: 480px) {
.col { margin: 1% 0 1% 0%; }
.span_3_of_3, .span_2_of_3, .span_1_of_3 { width: 100%; }
}
答案 0 :(得分:1)
您可以使用flex
轻松实现此目的。这是通过CSS实现这一点的简单 -
.section {
display: flex;
}
.section .col {
flex-grow: 100;
}
.section .col:empty {
display: none;
}
这是一个在行动中看到这一点的小提琴 - https://jsfiddle.net/schikara/oeb235gu/6/
另外,要了解有关Flex的更多信息,请点击链接 - https://css-tricks.com/snippets/css/a-guide-to-flexbox/