当内容换行为多行时,浮动宽度不再缩小以适应

发布于 2024-08-07 22:04:27 字数 326 浏览 2 评论 0原文

这是一个测试文件

调整窗口大小,使其足够宽以容纳所有四个盒子。请注意,按照预期,容器不比盒子宽。

将窗口大小调整到足够小,以使框位于多行上。请注意,容器是页面的整个宽度(这是无意的)。

为什么?是否可以通过不依赖盒子大小的方式来防止这种情况发生?

(在 Firefox 3.5 和 Chrome 4.0.221.8 上看到。如果解决方案在 IE6 中不起作用,那也没关系。)

Here is a test file.

Resize the window to be wide enough to hold all four boxes. Notice the container is no wider than the boxes, as intended.

Resize the window to be small enough that the boxes are on more than one line. Notice the container is the full width of the page (this is unintended).

Why? Is it possible to prevent this in a way that does not depend on the size of the boxes?

(Seen on Firefox 3.5 and Chrome 4.0.221.8. If a solution doesn't work in IE6, that's fine.)

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

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

发布评论

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

评论(2

长伴 2024-08-14 22:04:27

CSS 2.1 第 10.3.5 节浮动、不可替换元素 (http:// www.w3.org/TR/CSS21/visudet.html#float-width) 表示:

width = min(max(首选最小宽度, 可用宽度), 首选宽度)

  • 首选最小宽度 = 其中之一的宽度内盒,因为它们的尺寸都相同。
  • 可用宽度 = 页面宽度减去边距/边框。
  • 首选宽度 = 所有内盒并排的宽度。

对于文本换行的情况来说,这是完全合理的(想象一下宽度是否根据行端与可用空间边缘的接近程度而变化),但不是您想要的。不过,我看不出有什么办法可以避免这种情况。

CSS 2.1 section 10.3.5 Floating, non-replaced elements (http://www.w3.org/TR/CSS21/visudet.html#float-width) says that:

width = min(max(preferred minimum width, available width), preferred width)

  • preferred minimum width = width of one of inner boxes, as they're all the same size.
  • available width = width of the page minus margins/borders.
  • preferred width = width of all inner boxes side-by-side.

This is completely sane for cases of text wrapping (imagine if the width changed depending on how close the line ends got to the edge of the available space) but not what you want here. I can't see a way to avoid this, though.

逆光飞翔i 2024-08-14 22:04:27

我遇到了这个问题,因为父 div 的 max-wdith 导致文本范围被换行。右侧的边界并没有缩小。

这是一个 JS 解决方案:

删除所有内容上的浮动并使照片内联,然后将背景移动到包装 div 并添加 jquery 单行线来修复:

这是代码:

http ://jsbin.com/odeya3

I encountered this problem with a span of text that's wrapping because of a parent div's max-wdith. The border wasn't shrinking down on the right.

Here's a JS solution:

Remove the float on everything and make the photos inline, then move the background to a wrapper div and add a jquery one-liner to fix:

<script>$('#galleryWrapper').width($('.gallery').width());</script>

Here's the code:

http://jsbin.com/odeya3

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