如何让网站上的框堆叠成两列?

发布于 2024-11-26 01:56:46 字数 703 浏览 0 评论 0原文

我的网站上,我有几个块,其中包含动态生成的来自不同网站的信息,导致我无法/想要控制这些块出现的顺序。现在我希望它们堆叠在左列或右列中,具体取决于哪里有空间。

为了澄清一下,我画出了它的样子以及我想要的样子,但是该网站不允许我发布它们,所以我希望我的描述足够清晰(如果不是,图像是

78R5d https://i.sstatic.net/78R5d.png

T51aP https://i.sstatic.net/T51aP.png

现在就是这样,当右列中的框总体略高于左侧的框,左侧将出现一个新框,但其上方有一些空白以补偿高度差。如果右侧的盒子不会挡住左侧的盒子,那就太好了。

现在,恐怕如果不向每个盒子添加一些特定的类,这是不可能的,但你永远不知道人们可能会想出什么聪明的解决方案:)

On my website I have a few blocks with info from different sites being generated dynamically, resulting in me not being able/wanting to control the order in which those blocks appear. Now I'd like them to stack up in either the left or the right column, depending on where there is space.

To clarify, I drew up what it looks like and what I'd like it to look like, but the website won't let me post them, so I hope my description is clear enough (if not, the images are

78R5d
https://i.sstatic.net/78R5d.png

and

T51aP
https://i.sstatic.net/T51aP.png

the way it is now, when the boxes in the right column are slightly higher in total than the boxes on the left, a new box will appear on the left, but with some whitespace above it to compensate for the height difference. It'd be great if boxes on the right wouldn't block those on the left.

Now, I'm afraid this won't be possible without adding some specific classes to each box, but you never know what clever solutions people might come up with :)

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

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

发布评论

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

评论(3

魂ガ小子 2024-12-03 01:56:46

对于这个问题,有几种使用 Javascript 进行布局的解决方案。其中之一是 Masonry,它是一个 jQuery 插件。您只需要做一些基本的 CSS:

.item {
    float: left;
    width: 200px;
}

然后像这样:

$('#container').masonry({
    itemSelector : '.item',
    columnWidth : 240
});

当然您将使用的选项可能会有所不同。

There are several solutions to this problem that use Javascript for layout. One of them is Masonry, which is a jQuery plugin. You just need to do some basic CSS:

.item {
    float: left;
    width: 200px;
}

And then something like this:

$('#container').masonry({
    itemSelector : '.item',
    columnWidth : 240
});

Of course the options you would use might be different.

浅黛梨妆こ 2024-12-03 01:56:46

你应该将文章分成两个 div,否则你会得到空白。并排浮动两个 div,并用动态块交替填充它们。

<div class="column1">
    <article id="1">
    <article id="3">
</div>
<div class="column2">
    <article id="2">
    <article id="4">
</div>

You should separate the articles into two divs, otherwise you will get the white space. Float two divs side-by-side and alternate filling them with the dynamic blocks.

<div class="column1">
    <article id="1">
    <article id="3">
</div>
<div class="column2">
    <article id="2">
    <article id="4">
</div>
月棠 2024-12-03 01:56:46

经过更多研究,我发现了一个名为 column-width 或 column-count< 的 CSS 3 属性/a> 可以解决这个问题。目前大多数非 IE 浏览器都支持此功能,但基于 Gecko 和 WebKit 的浏览器需要分别使用前缀 -moz- 和 -webkit-。

我还没想出如何让它完美显示。在 Chromium 中,框可以在左列中被剪切,并在右列中继续,这对我的网站来说不太好。不幸的是,WebKit 还不支持break-inside,而且 -webkit-column-break-inside 也不能解决这个问题。在 Firefox 中,我可以通过设置固定宽度并将框向左浮动来解决此问题,但这确实破坏了 Chromium 中的功能。

所以,这不是一个完美的解决方案,但至少你可以进行浏览器嗅探,或者在某些情况下甚至可以进行优雅的降级,至少使它在 Gecko 中看起来更好一些。

After some more research, I found a CSS 3 property called column-width or column-count that can solve this. This is currently supported in most browsers that are not IE, though Gecko- and WebKit-based browsers need to use the prefixes -moz- and -webkit- respectively.

I haven't figured out yet how to get it to display perfectly. In Chromium boxes can be cut halfway in the left column, and continued in the right, which isn't great for my site. Unfortunately, WebKit doesn't support break-inside yet, and -webkit-column-break-inside doesn't do the trick either. In Firefox, I can fix this by setting a fixed width and floating the boxes to the left, but this really breaks things in Chromium.

So, not a perfect solution, but at least you could do browser sniffing or in some cases even graceful degradation to at least make it look a bit better in Gecko.

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