水平对齐在 IE 中不起作用

发布于 2025-01-08 00:09:19 字数 758 浏览 3 评论 0原文

我正在尝试水平对齐 div,但由于某种原因它在 IE 中不起作用...我做错了什么?

HTML

<body>
    <div id="container">
        <div id="header">
            <img src="logo.png">
        </div>

        <div id="top-nav">
            <ul class="menu">
                <li class="first leaf menu-mlid-471"><a href="">Home</a></li>
            </ul>
        </div>
    </div>
</body>

CSS

body{
   background-color: #fff;
   margin: 0;
   padding: 0;
 }
ul.menu{
   width: 500px;
   margin: auto;
 }
 #header{
   height: 150px;
 }

对于 Firefox、Chrome、Safari...没问题。这一切都恰好发生在中间。但 IE 拒绝正确对齐它......

I'm trying to align a div horizontally, but for some reason it's not working in IE... What am I doing wrong?

HTML

<body>
    <div id="container">
        <div id="header">
            <img src="logo.png">
        </div>

        <div id="top-nav">
            <ul class="menu">
                <li class="first leaf menu-mlid-471"><a href="">Home</a></li>
            </ul>
        </div>
    </div>
</body>

CSS

body{
   background-color: #fff;
   margin: 0;
   padding: 0;
 }
ul.menu{
   width: 500px;
   margin: auto;
 }
 #header{
   height: 150px;
 }

For Firefox, Chrome, Safari,... no problem. It all comes exactly in the middle. But IE refuses to align it properly...

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

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

发布评论

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

评论(4

陈年往事 2025-01-15 00:09:19

你有文档类型吗?如果没有它,IE 将恢复到 Quirks 模式,该模式不支持 margin: auto; 居中。

另外,IE < 6 根本不支持 margin: auto; 居中(以防古老的 IE 浏览器合规性对您特别重要)。

Do you have a doctype? Without it, IE reverts to Quirks Mode, which does not support margin: auto; centering.

Also, IE < 6 does not support margin: auto; centering at all (in case ancient IE browser compliance is particularly important you).

看春风乍起 2025-01-15 00:09:19

使用下面的CSS

.ie ul.menu{
           width: 500px;
           margin: auto;
           display:block;
 }

Use below CSS

.ie ul.menu{
           width: 500px;
           margin: auto;
           display:block;
 }
鹤仙姿 2025-01-15 00:09:19

试试这个 .ie ul.menu{
宽度:500px;
左边距:自动;
右边距:自动;
显示:块;}

Try this .ie ul.menu{
width: 500px;
margin-left: auto;
margin-right:auto;
display:block;}

第七度阳光i 2025-01-15 00:09:19

实际上,如果您在菜单 div 周围放置边框,您将在 IE 中看到该 DIV 居中对齐,但是您的 ul 内容却并非如此。请将其弹出到您的 CSS 中,看看您会得到什么结果:

ul.menu{
width: 500px;
margin: auto;
border:solid black thin;
text-align:center;
}

应该在 IE 中工作。

Actually, if you place a border around your menu div, you will see in IE that the DIV is aligned in the center, however, your ul content is not. Please pop this into your CSS and see what results you get:

ul.menu{
width: 500px;
margin: auto;
border:solid black thin;
text-align:center;
}

Should work in IE.

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