我一直在尝试使用 CSS 和 Flexbox 实现以下布局,但没有任何运气。也许这里有人可以帮助我并指出我所犯的错误,甚至建议我最好的做法。
这应该是最终结果(所有项目的高度和宽度都不同,但我开始认为 flexbox 不是最好的选择):
现场示例:https ://jsfiddle.net/bogdaniel/Lzugkva3/5/
<div class="container">
<div class="blog-container">
<div class="blog-item" style="height: 286px;">
<div class="blog-post">
<div class="post-body">
<div class="post-title">
<h2>Item One</h2>
<span>height: 286px;</span>
<p>Item Should Be First In The List On The Left Column</p>
</div>
</div>
</div>
</div>
<div class="blog-item" style=";height: 203px;">
<div class="blog-post">
<div class="post-body">
<div class="post-title">
<h2>Item Two</h2>
<span>height: 203px;</span>
<p>Item Should Go To The right next to the height 286px;</p>
</div>
</div>
</div>
</div>
<div class="blog-item" style="height: 255px;">
<div class="blog-post">
<div class="post-body">
<div class="post-title">
<h2>Item Three</h2>
<span>height: 255px;</span>
<p>Item Should Be Second On the First Row In the List</p>
</div>
</div>
</div>
</div>
<div class="blog-item" style="height: 325px;">
<div class="blog-post">
<div class="post-body">
<div class="post-title">
<h2>Item Four</h2>
<span>height: 325px;</span>
<p>Item Should Go To The right Of Item Three On The Second Column</p>
</div>
</div>
</div>
</div>
<div class="blog-item" style="height: 251px;">
<div class="blog-post">
<div class="post-body">
<div class="post-title">
<h2>Item Five</h2>
<span>height: 251px;</span>
<span>width: 523px;</span> Item Should Start From The Left And Span 2 Columns Almost
</div>
</div>
</div>
</div>
<div class="blog-item" style="height: 282px;">
<div class="blog-post">
<div class="post-body">
<div class="post-title">
<h2>Item Six</h2>
<span>height: 282px;</span>
<span>width: 186px;</span>
<p>Item Should Be Portrait And Span On The Right Column</p>
</div>
</div>
</div>
</div>
<div class="blog-item" style=" width: 100%%; height: 204px;">
<div class="blog-post">
<div class="post-body">
<div class="post-title">
<h2>Item Seven</h2>
<span>height 204px;</span>
<span>with: 523px;</span>
</div>
</div>
</div>
</div>
<div class="blog-item" style="width: 186px; height: 174px;">
<div class="blog-post">
<div class="post-body">
<div class="post-title">
<h2>Item Eight</h2>
<span>height: 174px;</span>
<span>width: 186px;</span>
</div>
</div>
</div>
</div>
</div>
</div>
SCSS代码:
.blog-container {
display: flex;
flex-flow: column wrap;
/* Your container needs a fixed height, and it
* needs to be taller than your tallest column. */
min-height: 1400px;
height: 100vh;
/* Force new columns */
&:before,
&:after {
content: "";
flex-basis: 100%;
width: 0;
order: 2;
}
/* Optional */
background-color: #f7f7f7;
border-radius: 3px;
margin: 15px auto;
counter-reset: items;
}
.blog-item {
width: 50%;
padding: 14px;
.blog-post {
height: 100%;
/* Optional */
position: relative;
border-radius: 3px;
border: 1px solid #4290e2;
box-shadow: 0 2px 2px rgba(0, 90, 250, 0.05),
0 4px 4px rgba(0, 90, 250, 0.05), 0 8px 8px rgba(0, 90, 250, 0.05),
0 16px 16px rgba(0, 90, 250, 0.05);
color: #000;
padding: 15px;
&:before {
counter-increment: items;
content: counter(items);
}
.post-body {
padding: 15px;
}
}
}
.blog-item:nth-child(2n + 1) {
order: 1;
}
.blog-item:nth-child(2n + 2) {
order: 2;
}
.blog-item:nth-child(2n + 3) {
order: 1;
}
.blog-item:nth-child(2n + 4) {
order: 2;
}
