Masonry-style Layout ONLY with CSS

2019-01-01 13:24发布

As you can see in the image every box has a different height and there are some boxes with double width.

Layout

Is it possible to create a masonry-style layout only with CSS?

标签: css html5 css3
2条回答
忆尘夕之涩
2楼-- · 2019-01-01 13:56

I'm working on a site right now with the same kind of layout, two columns with the occasional double-wide box. What I do is just separate the double-wide from the rest of the content. For example:

<div class="two-columns">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>
<div class="double-wide">
</div>
<div class="two-columns">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>

Then you can apply the CSS3 column solution to just the two-columns class. If you need to support IE9 you'll sadly need to add a JavaScript fallback.

查看更多
零度萤火
3楼-- · 2019-01-01 14:22

With css3 support you could do this:

http://jsfiddle.net/huAxS/2/

.container {
    -moz-column-count: 2;
    -moz-column-gap: 10px;
    -webkit-column-count: 2;
    -webkit-column-gap: 10px;
    column-count: 2;
    column-gap: 10px;
    width: 360px;
}

.container div {
    display: inline-block;
    width: 100%;
    background-color: red;
}​

With no css3 support, you have to resort to js unfortunately.

查看更多
登录 后发表回答