我使用了响应式的三个网格布局。可以说每个都是33%的宽度,如果一个网格/格是空的,我希望另两个网格扩展到50%的宽度,如果两个网格/格是空的,我想剩下一个为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%; }
}

最佳答案

您可以使用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/

09-18 22:57