如何在杂志/报纸等砖石布局中浮动元素?

发布于 2024-12-18 14:49:54 字数 534 浏览 3 评论 0原文

我正在尝试实现一种布局,其中项目将像报纸/杂志文章部分一样浮动。它类似于 jQuery 的 Masonry 所做的事情。但我试图仅使用 CSS3 来实现这一点。我想也许 box 显示属性可以做到这一点。尽管尝试了几次,我还是无法使项目在满足父列宽度后向下滑动。

有没有办法只使用CSS来实现这种布局?

标记将是这样的:

<article>
    <section>...</section>
    <section>...</section>
    <section>...</section>
    <section>...</section>
</article>

这里一个部分将向左浮动并在更适合的列队列上调整自身(而不是像简单浮动那样低于前一个的基线)。

I am trying to achieve a layout where items will float like newspaper/magazine article sections. It is something similar as what jQuery's Masonry does. But I was trying to achieve that only using CSS3. I thought perhaps the box display property could do it. Although after trying for few times, I wasn't able to make the items slide down after the parent column width as fulfilled.

Is there any way to achieve this layout only using CSS?

The markup would be something like this:

<article>
    <section>...</section>
    <section>...</section>
    <section>...</section>
    <section>...</section>
</article>

Here a section would float left and adjust itself on the columns queue where better fit (and not bellow the baseline of the previous one, as simple float does).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

濫情▎り 2024-12-25 14:49:54

可以使用 CSS 列。 这里是一个很好的解释。

CSS:

div{
-moz-column-count: 3;
-moz-column-gap: 10px;
-webkit-column-count: 3;
-webkit-column-gap: 10px;
column-count: 3;
column-gap: 10px;
width: 480px; }

div a{
display: inline-block; /* Display inline-block, and absolutely NO FLOATS! */
margin-bottom: 20px;
width: 100%; }

HTML:

<div>
<a href="#">Whatever stuff you want to put in here. Images, text, movies, what have you. No, really, anything!</a>
...and so on and so forth ad nauseum.
</div>

另外,我通过在 Google 上搜索“CSS Masonry”找到了这个网站。这是第二个结果。

It's possible using CSS columns. Here is a good explanation.

CSS:

div{
-moz-column-count: 3;
-moz-column-gap: 10px;
-webkit-column-count: 3;
-webkit-column-gap: 10px;
column-count: 3;
column-gap: 10px;
width: 480px; }

div a{
display: inline-block; /* Display inline-block, and absolutely NO FLOATS! */
margin-bottom: 20px;
width: 100%; }

HTML:

<div>
<a href="#">Whatever stuff you want to put in here. Images, text, movies, what have you. No, really, anything!</a>
...and so on and so forth ad nauseum.
</div>

Also, I found this site by searching "CSS Masonry" on Google. It was the second result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文