Div Float问题:似乎div自动clear:left

发布于 2024-12-08 14:49:40 字数 542 浏览 0 评论 0原文

我有一系列图像,所有图像的宽度均为 width:400px,但高度各不相同。它们位于以下容器中:

#content {width:808px;margin:0 auto;margin-top:100px}

所有 ODD 图像都具有以下代码:

{float:left;clear :left;margin:2px}

所有偶数图像都有以下代码:

{float:right;clear:right;margin:2px}

理论上不应该每个奇数图像都在这左侧,彼此堆叠,每个偶数图像应位于右侧,彼此堆叠。

相反,我得到的是:

Chaos

就好像右侧的一些图像是 clear:leftclear:both,对吧?

有什么想法吗?

谢谢

I have a series of images, all of which have width:400px, but have varying heights. They are in the following container:

#content {width:808px;margin:0 auto;margin-top:100px}

All the ODD images have the following code:

{float:left;clear:left;margin:2px}

All the EVEN images have the following code:

{float:right;clear:right;margin:2px}

In theory shouldn't every ODD image be on the left, stacked ontop of each other and every EVEN image should be on the right stacked on top of each other.

Instead I get this:

Chaos

It's as though some of the images on the right are clear:left or clear:both, right?

Any thoughts?

Thanks

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

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

发布评论

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

评论(1

腹黑女流氓 2024-12-15 14:49:40

以这段代码为例:

<div>Airplane</div>
<div style="float: left;">Symba</div><div style="float:right;">Chewbacca(?)</div>

您不会期望 Chewbacca(?) 的顶部与 Airplane 的顶部对齐!它将与 Symba 的顶部对齐。

Symba 具有向左浮动,因此被迫位于飞机下方。飞机是否漂浮已经不重要了。

我见过一些布局可以满足您的要求,即对所有元素应用 float:left,然后在页面加载完成后使用一些 Javascript 来整理空白区域(float: left 本身无法做到这一点) ,正如您可能已经注意到的那样)。我自己不喜欢这个解决方案,但我能想到的唯一其他选择是将图像预先排序为两列,并相应地分割它们:

<div style="float:left">
 <div>Myst(?)</div>
 <div>Airplane</div>
 <div>Symba</div>
</div>
<div style="float:right">
 <div>Coastline</div>
 <div>Painter</div>
 <div>Chewbacca(?)</div>
</div>

但如果您希望列始终具有相等的高度,那可能不会为您工作,除非您提前知道图像的高度,并且可以根据该高度将内容放入列中。 (循环,记录当前高度,并始终添加到较短的列;类似的事情。)

take, for example, this code:

<div>Airplane</div>
<div style="float: left;">Symba</div><div style="float:right;">Chewbacca(?)</div>

you wouldn't expect the top of Chewbacca(?) to line up with the top of Airplane! it will line up with the top of Symba.

Symba has float: left, so is forced below the Airplane. whether Airplane happens to be floating or not doesn't matter anymore.

I've seen layouts that do what you want, by applying float:left to ALL the elements, and then using some Javascript to tidy up the white space once the page is done loading (float: left on its own won't do it, as you may have already noticed). I don't like that solution myself, but the only other alternative I can think of is to pre-sort the images in to two columns, and split them accordingly:

<div style="float:left">
 <div>Myst(?)</div>
 <div>Airplane</div>
 <div>Symba</div>
</div>
<div style="float:right">
 <div>Coastline</div>
 <div>Painter</div>
 <div>Chewbacca(?)</div>
</div>

but if you want the columns to always be equal height, that may not work for you, unless you know the heights of the images in advance, and can put things into columns based on that. (loop over, keeping a tally of the current height, and always adding to the shorter column; something like that.)

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