CSS浮动问题,不同高度的div

发布于 2024-11-01 20:59:16 字数 760 浏览 0 评论 0原文

我想知道是否有办法让许多不同高度的 div 彼此相邻浮动而不产生下推效果,

例如:!

|||||||  ||2||| ||||||||
||||||| |||||||| ||||3||
||1||        ||||||||
|||||||       ?||||||||
|||||||||4||||  
    |||||||||

问题是 div 2 下的空白,因为 div3 比 div2 高,所以它 推下 div4 留下丑陋的空白

代码:

<div class=container>

<div class=box>1</div>
<div class=box>2</div>
<div class=box>3</div>
<div class=box>4</div>

</div>

我试图让 div .container 动态浮动任意数量的 div,并且所有边框相互接触,没有这种下推效果,

有什么方法可以做到吗? 我尝试了 jQuery Masonry 插件,但也能弄清楚:(..

非常需要帮助

i was wondering if there is anyway to float many divs with different heights next to each others without pushdown effect,

eg:!

|||||||  ||2|||  ||||||||
|||||||  ||||||||  ||||3||
||1||          ||||||||
|||||||        ?||||||||
||||||| ||4||||  
      |||||||||

the problem is the white space under div 2 which is because div3 is taller than div2 so it
pushed down div4 leaving ugly whitespace

code:

<div class=container>

<div class=box>1</div>
<div class=box>2</div>
<div class=box>3</div>
<div class=box>4</div>

</div>

im trying to make div .container float any number of divs inside dynamically and all borders touch each other w/o this pushdown effect,

is there any way to do it ?
i tryed jQuery Masonry plugin but could figure it out also :(..

ty very much for help

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

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

发布评论

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

评论(3

没有你我更好 2024-11-08 20:59:16

这是浮动元素的 CSS 标准行为。如果你不想要 (2) 和 (4) 之间有空格,那么你可能需要浮动 (2),然后为 (2) 的内容放置一个 div,为 (4) 放置另一个 div,这样它们就是“在一起”(垂直)。

我想这取决于你想要做什么和想要实现什么。您可能还想让所有 div 具有相同的高度并具有背景或边框,以便它们浮动而不会出现视觉空白。 (但是如果里面的内容没有填满div,就会有空间。

另外,你可能想浮动1个div,然后在其内部浮动(1),(2)和(3),然后浮动(4) 分开......但这实际上取决于你想做什么。

This is the standard behavior for CSS for floating elements. If you don't want the space between (2) and (4), then you may need to float (2) and then put a div for content of (2) and another div for (4), so that they are "together" (vertically).

And I guess it depends what you want to do and achieve. You might also want to make all divs the same height with a background or border, so that they float without the visual empty space. (but there will be space if the content inside doesn't fill up the divs.

Also, you might want to float 1 div, and then float (1), (2), and (3) inside of it, and then float (4) separately... but really it depends what you want to do.

溺孤伤于心 2024-11-08 20:59:16

看看这个。这可能会解决您的问题:

<div id="container" style="height:300px;width:308px;background:#ddd;border:1px solid red;float:right"">
  <div class="box" style="height:175px;width:100px;background:#CF9;border:1px solid #C0C;float:right">3</div>
  <div class="box" style="height:150px;width:100px;background:orange;border:1px solid #404040;float:right">2</div>
  <div class="box" style="height:300px;width:100px;background:green;border:1px solid yellow;float:left">1</div>
  <div class="box" style="height:145px;width:100px;background:blue;border:1px solid #F03;float:right">4</div>
</div>

这里我使用颜色和边框进行视觉辅助。

Check this out. This may solve your problem:

<div id="container" style="height:300px;width:308px;background:#ddd;border:1px solid red;float:right"">
  <div class="box" style="height:175px;width:100px;background:#CF9;border:1px solid #C0C;float:right">3</div>
  <div class="box" style="height:150px;width:100px;background:orange;border:1px solid #404040;float:right">2</div>
  <div class="box" style="height:300px;width:100px;background:green;border:1px solid yellow;float:left">1</div>
  <div class="box" style="height:145px;width:100px;background:blue;border:1px solid #F03;float:right">4</div>
</div>

Here I used colors and borders for visual aid.

天气好吗我好吗 2024-11-08 20:59:16

我写了一个简单的 jQuery 脚本来实现这个目的。它甚至适用于可变宽度容器/页面,因此您不需要将行放入单独的容器中。让我们看看并让我知道这是否是您所需要的:

http://demo.petiar.sk/smartfloat/

谢谢,
皮蒂亚尔。

I wrote a simple jQuery script which does the trick. It even work for variable width container/page, so you dont need to enclose rows into separate containers. Lets have a look and let me know if that's what you need:

http://demo.petiar.sk/smartfloat/

Thanks,
Petiar.

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