IE7 中右浮动元素向下推下一个元素
我正在尝试使用 header+content+footer+sidebar 创建一个简单的标记。侧边栏必须浮动在标题和内容元素上方,如果它比内容高,则必须将页脚向下推(如下所示: http://jsfiddle.net/gWLFN/7/)。
HTML:
<div id="wrapper">
<div id="sidebar">sidebar</div>
<div id="header">header</div>
<div id="content">content</div>
<div style="clear:both"></div>
<div id="footer">footer</div>
</div>
CSS:
#wrapper { width:500px }
#header { width:500px; height:100px; background-color:red }
#content { width:500px; height:300px; background-color:green }
#footer { width:500px; height:100px; background-color:blue }
#sidebar {
float:right;
margin-top:50px;
width:100px;
height:500px;
background-color: yellow;
border:1px solid white;
}
问题是在 IE7 中,侧边栏会向下推其他元素。我认为这是因为标题+侧边栏的总宽度大于包装宽度。我发现了很多关于 IE7 中 float:right 问题的帖子,但它们都是针对不超过包装器的宽度的。
我选择了 float:right 而不是绝对定位,因为页脚的位置必须取决于侧边栏的高度(如果有人知道如何使用绝对定位来做到这一点,那就完美了!)。
我将不胜感激任何解决这个问题的想法。
I'm trying to create a simple markup with header+content+footer+sidebar. The sidebar must float above the header and content element, and if it's taller than the content, it must push the footer down (like this: http://jsfiddle.net/gWLFN/7/).
The HTML:
<div id="wrapper">
<div id="sidebar">sidebar</div>
<div id="header">header</div>
<div id="content">content</div>
<div style="clear:both"></div>
<div id="footer">footer</div>
</div>
The CSS:
#wrapper { width:500px }
#header { width:500px; height:100px; background-color:red }
#content { width:500px; height:300px; background-color:green }
#footer { width:500px; height:100px; background-color:blue }
#sidebar {
float:right;
margin-top:50px;
width:100px;
height:500px;
background-color: yellow;
border:1px solid white;
}
The problem is that in IE7, the sidebar pushes down the other elements. I think it's because the total widths of header+sidebar is greater than wrapper width. I have found a lot of posts about float:right problem in IE7, but all of them are for widths that doesn't exceede the wrapper.
I have choosen float:right instead of absolute positioning because the position of the footer must depend on sidebar height (if someone knows how to do this with absolute positioning, perfect!).
I would appreciate any idea to solve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经快到了,HTML 结构的顺序有点混乱,并且您强制使用 CSS 宽度,而不是让浏览器找到最适合的宽度。
您可以从嵌套 CSS 类(
#sidebar
除外)中删除width
值,因为默认情况下,除非指定了宽度,否则它们会占用任何剩余宽度。然后你只需要在 HTML 结构中交换#header
和#sidebar
就可以了。请注意,由于我们交换了
#header
和#sidebar
,因此#sidebar
中的margin-top
已被改变了。CSS
HTML
工作示例:http://jsfiddle.网/gnx2z/
You are almost there, the order of the HTML structure is slightly muddled and you are forcing CSS widths rather than letting the browser work out the best fit.
You can remove the
width
values from the nested CSS classes (except#sidebar
) as, by default, they take up any remaining width unless they have one specified. Then you just need to swap#header
and#sidebar
round in the HTML structure and you are pretty much sorted.Please note, since we have swapped round
#header
and#sidebar
, themargin-top
within#sidebar
has been changed.CSS
HTML
Working example: http://jsfiddle.net/gnx2z/