Internet Explorer 6 和 7:当浮动元素包含向右浮动的子元素时,浮动元素会扩展至 100% 宽度。 有解决方法吗?
我有一个父 div
向左浮动,有两个子 div
需要向右浮动。
父 div
应该(如果我正确地理解了规范)应包含子 div
所需的宽度,这就是它在 Firefox 等中的行为方式。
在 IE 中,父 div
扩展至 100% 宽度。 这似乎是子元素向右浮动的浮动元素的问题。 测试页:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Float test</title>
</head>
<body>
<div style="border-top:solid 10px #0c0;float:left;">
<div style="border-top:solid 10px #00c;float:right;">Tester 1</div>
<div style="border-top:solid 10px #c0c;float:right;">Tester 2</div>
</div>
</body>
</html>
不幸的是,我无法修复子 div
的宽度,因此我无法在父级上设置固定宽度。
是否有仅 CSS 的解决方法可以使父 div
与子 div
一样宽?
I've got a parent div
floated left, with two child div
s that I need to float right.
The parent div
should (if I understand the spec correctly) be as wide as needed to contain the child div
s, and this is how it behaves in Firefox et al.
In IE, the parent div
expands to 100% width. This seems to be an issue with floated elements that have children floated right. Test page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Float test</title>
</head>
<body>
<div style="border-top:solid 10px #0c0;float:left;">
<div style="border-top:solid 10px #00c;float:right;">Tester 1</div>
<div style="border-top:solid 10px #c0c;float:right;">Tester 2</div>
</div>
</body>
</html>
Unfortunately I can't fix the width of the child div
s, so I can't set a fixed width on the parent.
Is there a CSS-only workaround to make the parent div
as wide as the child div
s?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是一个使
inline-block
在 IE6 上工作的解决方案,如 http://foohack.com/2007/11/cross-browser-support-for-inline-block-styling/ 使元素的行为更像右浮动s
:Here's a solution which makes
inline-block
work on IE6 as at http://foohack.com/2007/11/cross-browser-support-for-inline-block-styling/ to make the elements behave more like right-floated<div>s
:我想出了一个使用
text-align: right
和display: inline
的解决方案。试试这个:
注意,我必须切换标记中“测试器”框的顺序,以与您的示例相同的方式显示。 我认为新容器上有一个替代方案,即 margin-top,但我现在没有时间研究。
如果您想为所有其他浏览器提供更清晰的样式,请尝试以下操作:
当您想要在这些框周围布局内容时,可能会出现一些不同的问题。 不过,我认为这些问题比这个问题更容易解决。
希望这对您有帮助。
I came up with a solution using
text-align: right
anddisplay: inline
.Try this:
Notice I had to switch the order of the "tester" boxes in the markup to show up in the same way as your example. I think there is an alternative that margin-top on the new container, but I don't have time looking into right now.
If you want cleaner styling for all other browsers try this:
There are some different issues that can come up when you want to layout stuff around those boxes. However, I think those issues will be much easier to solve than this one.
Hope this was helpful for you.
我无法提出符合您要求的纯 CSS 解决方案,但如果您想使用 JavaScript 解决方案,也许下面的代码可以提供帮助? 我没有改变 div 的样式,只是将 ID
main
、sub1
和sub2
添加到您的 div 中。I couldn't come up with a CSS-only solution that fits your requirements, but if you want to use a JavaScript solution, maybe the following code can help? I did not alter the style of the divs, I only added the IDs
main
,sub1
, andsub2
to your divs.我遇到了同样的问题,并通过使用position:absolute和right:0定位子元素(我想向右浮动)来解决。 我给了父元素足够的正确填充,为子元素腾出空间...值得一试,可能不适用于所有应用程序!
I had the same problem and solved in by positioning the child element (which I wanted to float right) with position:absolute and right:0. I gave the parent element enough right padding to make room for the child element... Worth a shot, might not work for all applications!
使用单单元格表格而不是外部 div 怎么样? 可能需要更多的工作才能使所有内容正确对齐,但表格不会扩展。
What about using a single-cell table instead of the outer div? It may need some more work to have everything aligned properly, but the table doesn't expand.
您可以从以下开始:
结果似乎至少在您正在寻找的范围内。 显然,您需要根据需要对其进行调整,并且可能添加一些 css 逻辑以仅将其应用于浏览器 < IE 7。
如果这不是您想要的,请尝试使用一些负边距。
Something you could start with is this:
The result of that seems to be at least in the ballpark of what you're looking for. Obviously, you'd want to tweak it for your needs, and probably add some css logic to only apply it to browser < IE 7.
If that's not exactly what you're looking for, try playing with some negative margins.
首先,为什么不使用内联样式?
我会使用它来使用单独的 css 文件来定位 IE:
我知道这不是一个直接的问题,但 IE 总是很难处理! 我认识的大多数设计师/开发人员都会为 IE 制作一个全新的样式表。
Firstly, why aren't you using inline styles?
I'd use this to target IE with a separate css file:
I know this isn't a direct question, but IE is ALWAYS a pain to deal with! Most designers/developers that I know will make a totally new stylesheet for IE.
一个非常 hacky 但仅 CSS 的解决方案,在 IE、chrome 和 FF 中工作正常。
我采用了 kaba 的解决方案 - 但问题是,它在 IE 中工作正常,但所有其他浏览器在 2 个子 div 之间显示 4px 空间。 即使两个 div 上的内容都展开,空间仍保持为 4px。 为了解决这个问题,我使用了 IE 条件语句。 我看起来不像世界上最好的代码,但它完成了工作。
A very hacky but a CSS only solution that works okay in IE, chrome and FF.
I took kaba's solution - but the problem was, it works okay in IE but all the other browsers show a 4px space between the 2 child divs. The space remains as 4px even if the content on both the divs expand. So to fix that I've used IE conditional statements. I doesn't look like the best code in the world but it gets the job done.