IE7 与我的浮动侧边栏配合不佳

发布于 2024-07-23 09:56:53 字数 950 浏览 2 评论 0原文

我试图让一个动态大小的侧边栏浮动在网页的右上部分(但在标题和导航下方),并使页面上的主要内容围绕它流动(有点像“L”形状,除了“L”的底部非常厚)。 侧边栏的宽度和高度因页面而异,因此我无法使用任何硬值。

我的 css 看起来像:(

#main {
    width: 850px;
    height: auto;
}

#sidebar {
    width: auto;
    float: right;
}

加上一些我认为无关紧要的填充、边距和背景颜色代码)

我的 html 看起来像:

<div id="wrapper">  
    <div id="header">    /* header stuff */    </div>  
    <div id="nav">       /* nav stuff */       </div>  

    <div id="sidebar">  
        /* my sidebar content, really just an h3 and a ul */
    </div>

    <div id="main">
        /* lots of content here */
    </div>
</div>

我不完全明白为什么我必须先有侧边栏 div,但这段代码在 FF 中工作正常、Chrome、Safari (Windows) 和 IE8。 但是在IE7(和IE6,我不关心)上,主要内容被推到侧边栏底部下方,就好像侧边栏div上有一个“clear:left”(但没有) 。

我有一种感觉,这是那些邪恶的 IE7 不合规错误之一,特别是因为 IE8 的行为与其他浏览器完全相同。 但我不知道如何解决它。

有任何想法吗? TIA。

I'm trying to get a dynamically sized sidebar to float in the upper right portion of my web pages (but below the header and nav) and have the main content on the page flow around it (sort of in an "L" shape except with the bottom part of the "L" really thick). The width and height of the sidebar will vary from page to page so I can't use any hard values.

My css looks like:

#main {
    width: 850px;
    height: auto;
}

#sidebar {
    width: auto;
    float: right;
}

(plus some padding, margin, and background color code I think is inconsequential)

My html looks like:

<div id="wrapper">  
    <div id="header">    /* header stuff */    </div>  
    <div id="nav">       /* nav stuff */       </div>  

    <div id="sidebar">  
        /* my sidebar content, really just an h3 and a ul */
    </div>

    <div id="main">
        /* lots of content here */
    </div>
</div>

I don't completely understand why I have to have the sidebar div first, but it this code works fine in FF, Chrome, Safari (Windows), and IE8. But on IE7 (and IE6, which I don't care about), the main content gets pushed down below the bottom of the sidebar, as if there was a "clear: left" on the sidebar div (but there isn't).

I have a feeling this is one of those evil IE7 non-compliance bugs, especially because IE8 behaves exactly like the other browsers. But I have no idea how to fix it.

Any ideas? TIA.

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

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

发布评论

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

评论(3

影子是时光的心 2024-07-30 09:56:53

首先,确保您使用的文档类型会将 IE7 置于严格模式(请参阅 http://hsivonen.iki .fi/doctype/ 进行解释)。 如果这不起作用,则可能需要调整页边距宽度。

之所以必须首先拥有侧边栏 div,是因为 div 是显示为块元素,其后的任何内容都将位于其下方(除非您浮动主 div)。

通过浮动侧边栏 div 并将其放在第一位,浏览器知道它可以将主 div 显示在侧边栏的右侧。 您可以通过向主 div 添加左侧浮动并从侧边栏 div 中删除浮动并将其移动到主 div 之后来获得类似的效果。

First, make sure you are using a doctype that will put IE7 into strict mode (see http://hsivonen.iki.fi/doctype/ for an explaination). if that doesn't do it, it may be that you need some play in your margin widths.

The reason why you have to have the sidebar div first, is since div is a displayed as a block element anything after it, will be below it (unless you float the main div).

By floating the sidebar div and putting it first, the browser knows it can display the main div to the right of the sidebar. You could get a similar effect by adding float left to the main div and removing the float from the sidebar div and moving it after the main div.

咆哮 2024-07-30 09:56:53

根据您的描述,听起来您的侧边栏的行为就像是一个块元素。 也许尝试一些不同的显示选项,例如内联块。 我还尝试尝试宽度 min-width 属性。 不过很难说。

From what you describe, it sounds like your sidebar is behaving as if it was a block element. Maybe try some different display options like inline-block. I'd also try experimenting with the width min-width attributes. Hard to say though.

时光病人 2024-07-30 09:56:53

答对了! 固定的! 那些提到玩弄宽度和边距的人今晚将获得金星。 事实证明,我所要做的就是删除主 div 上的固定宽度,然后在右侧添加一些填充来为文本和图像创建装订线。 在 FF3、Chrome、Safari(Win)以及最重要的 IE6 和 IE6 中进行了测试和确认。 IE7(尽管我仍然讨厌IE)。

我猜 IE 渲染引擎是在说,“我看到你希望你的主 div 宽 850px,但是侧边栏卡在那里,我没有空间,所以我必须将它推到侧边栏下面” 。 当然,其他浏览器的渲染引擎都会说:“伙计,我完全明白你想要做什么!没问题,我会按照你想要的方式布置所有内容。”

Bingo! Fixed! The guys who mentioned playing around with widths and margins get the gold stars tonight. It turned out that all I had to do was remove the fixed width on the main div, then add some padding on the right to create a gutter for text and images. Tested and confirmed in FF3, Chrome, Safari (Win), and most importantly, IE6 & IE7 (even though I still hate IE).

I guess the IE rendering engine was saying, "I see that you want your main div to be 850px wide, but with that sidebar you stuck up there, I don't have room so I'll have to shove it underneath the sidebar". Of course, every other browser's rendering engine said, "Dude, I totally get what you're trying to do! No problem, I'll lay out everything exactly as you'd like it."

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