Ayo*_*uan 13 html css flexbox css-grid
我尝试使用 css 和如下标记的列布局来实现砌体风格。
我想知道是否有可能使.green
一列采用两列而不是一列?
先感谢您!
.parent{
column-gap: 1rem;
column-count: 2;
}
.element{
display:inline-block;
background:red;
width:100%;
height:100px;
}
.green{
background:green;
}
Run Code Online (Sandbox Code Playgroud)
<div class="parent">
<div class="element green">
</div>
<div class="element">
</div>
<div class="element">
</div>
<div class="element">
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
bor*_*kur 21
使用 CSS 网格,您可以使用grid-column: span 2
:
.wrapper {
display: grid;
grid-gap: 1rem;
grid-template-columns: 1fr 1fr 1fr;
grid-auto-flow: dense;
background-color: #fff;
color: #444;
padding: 1rem;
}
.box {
background-color: red;
color: #fff;
border-radius: 0.5rem;
padding: 1rem;
font-size: 200%;
}
.a,
.d,
.e,
.f {
background-color: green;
grid-column: span 2; /* <-- here is the trick */
}
Run Code Online (Sandbox Code Playgroud)
<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 class="box h">H</div>
</div>
Run Code Online (Sandbox Code Playgroud)
在此处了解更多信息:https : //developer.mozilla.org/en-US/docs/Web/CSS/grid-column
关于砌体样式:在撰写本文时,CSS Grid Layout 规范的第 3 级包含一个masonry
值grid-template-columns
和grid-template-rows
布局,尽管浏览器支持非常不存在:https : //caniuse.com/mdn-css_properties_masonry
在此处了解它:https : //developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Masonry_Layout
所以我grid-auto-flow: dense;
在网格上使用了,这使得网格项 G 出现在网格项 F 之前。这不是真正的砖石风格(根据可用的垂直空间将元素放置在最佳位置),但它通过使网格密集填充所有可用来接近水平空间与适合该空间的下一个网格项。
“密集”填充算法尝试填充网格中较早的孔
在此处了解它:https : //developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-flow
哦,如果您是 CSS 网格的新手,我建议您观看Wes Bos的演讲“45 分钟内的 CSS 网格!”:https : //www.youtube.com/watch?v=DCZdCKjnBCs
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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="parent">
<div class="element green"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
更多信息:
我想说,不,你不能让单个 .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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)