我尝试使用css来实现砌体风格,其列布局如下图所示。
我想知道是否可以将.green
设为一列而不是两列吗?
提前谢谢!
.parent{
column-gap: 1rem;
column-count: 2;
}
.element{
display:inline-block;
background:red;
width:100%;
height:100px;
}
.green{
background:green;
}
<div class="parent">
<div class="element green">
</div>
<div class="element">
</div>
<div class="element">
</div>
<div class="element">
</div>
</div>
答案 0 :(得分:1)
我会说,不,您不能让单个.green元素占用两列,因为您是专门告诉浏览器使用两列。如果您需要它跨越两列,那么我建议使用一个单独的元素。也许更合适的解决方案是使用CSS网格布局。下面的代码段包含了这两种解决方案的示例:
.parent {
column-gap: 1rem;
column-count: 2;
}
.element {
display: inline-block;
background: red;
width: 100%;
height: 100px;
}
.green {
background: green;
width: 100%;
height: 100px;
margin-bottom: 1rem;
}
.grid-container {
margin-top: 20px;
display: grid;
grid-template-columns: auto auto;
grid-gap: 1rem;
}
.greenGrid {
background: green;
height: 100px;
grid-column-start: 1;
grid-column-end: 3;
}
.redGrid {
background: red;
height: 100px;
}
<div class="green">
</div>
<div class="parent">
<div class="element">
</div>
<div class="element">
</div>
<div class="element">
</div>
</div>
<div class='grid-container'>
<div class='greenGrid'></div>
<div class='redGrid'></div>
<div class='redGrid'></div>
<div class='redGrid'></div>
</div>
答案 1 :(得分:0)
CSS网格布局提供了一种简单,便捷和有效的解决方案。
.parent {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 100px;
grid-gap: 1rem;
}
.element.green {
grid-column: 1 / -1;
background: green;
}
.element {
background: red;
}
<div class="parent">
<div class="element green"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
</div>
更多信息:
答案 2 :(得分:0)
以下是使用grid-column: auto / span 2
的灵活方法:
.wrapper {
display: grid;
grid-gap: 10px;
grid-template-columns: 100px 100px 100px;
background-color: #fff;
color: #444;
padding: 20px;
}
.box {
background-color: red;
color: #fff;
border-radius: 5px;
padding: 20px;
font-size: 150%;
}
.a,
.g {
background-color: green;
grid-column: auto / span 2;
}
<div class="wrapper">
<div class="box a">A</div>
<div class="box b">B</div>
<div class="box c">C</div>
<div class="box d">D</div>
<div class="box e">E</div>
<div class="box f">F</div>
<div class="box g">G</div>
</div>