<!DOCTYPE HTML>
<html lang="en" dir="ltr">
<head>
<title>Floating Div Madness upon Window Resize</title>
<style>
* {margin:0; padding:0}
body {margin:20px; font-size:0px; color:#000000}
div.page {margin-right:120px; background-color:#AAAAFF; float:left}
div.wide {width:300px; background-color:#AAFFAA}
div.box {width:100px; margin-left:-100px; background-color:#FFAAAA; float:left}
h1 {font-size:x-large}
p {padding-bottom:5px; font-size:small}
</style>
</head>
<body>
<div class="page">
<h1>MAIN PAGE</h1>
<p>This is the main portion of the page (light blue). It is currently floated left with a right margin of 120px, to account for the box (light red) as well as the white space between it and the box (light red). All may look well on the surface, but...</p>
<p>Resize the window in Firefox (I tested both 3.5 and 4) and the white margin of the body can be cut off, not only on the right side, but on the bottom of the page, too.</p>
<p>Remove enough text and this div will shrink to fit it, no longer taking up the entire width of the page (minus the box).</p>
<div class="wide">
<p>If I nest another, non-floating div with a fixed width (light green), something even stranger happens when I resize the window, this time in Internet Explorer 6 (maybe in other versions, too): The text in the page div (light blue) is squished, I think by the margin of the box (light red). The fixed width div (light green) is unaffected by this.</p>
</div>
</div>
<div class="box">
<h1>BOX</h1>
<p>(this could be navigation, or anything else)</p>
</div>
</body>
</html>
我希望出于语义化的原因,可以在代码后面保留盒子(浅红色),但这是可选的。以下是我已经尝试过的一些更成功的方法,以及它们为什么似乎不起作用:
绝对定位:当浏览器未调整大小时,此方法与我的代码一样美观。它确实在某种程度上解决了Firefox中消失的页面边距问题。然而,当窗口大小变得足够小时,盒子(浅红色)将会覆盖或在主页div(浅蓝色)下方,具体取决于我设置哪个z-index更高或更低。
仅浮动盒子:这涉及更改HTML并将盒子(浅红色)放置在代码的其余内容之前。这会自动扩展主页div(浅蓝色),这是我没有找到使用浮动方法(没有给定数量的内容)的方法。但是,在Firefox中仍然删除了body的边距,在IE中文本仍然被压缩,并且当窗口变得足够小时,盒子(浅红色)仍将覆盖或在主页div(浅蓝色)下方(再次取决于z-index)。
为所有内容分配min-width:这在解决IE问题方面非常成功,但需要在比这更复杂且支持不同媒体类型的页面上进行一些CSS工作。而且,它仍然没有解决Firefox中的body边距问题,也没有给我一个扩展主页div(浅蓝色)的方法,因为它仍然是浮动的。
将body边距更改为边框:这也无法解决Firefox问题;无论是边框还是边距,在使用浮动时都会被削减掉页面的右侧和底部。
将边距添加到盒子中:这对于Firefox也不起作用。我可以让主页内容(浅蓝色)的底部边距保持在原地(这对我来说似乎特别奇怪),但是盒子(浅红色)的右边距仍然被削减。
任何帮助都将不胜感激,因为我找不到任何网站或帖子回答这些问题,更不用说描述这些问题存在了;我肯定花费了大量时间寻找和测试解决方案!