水平对齐在 IE 中不起作用
我正在尝试水平对齐 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你有文档类型吗?如果没有它,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).使用下面的CSS
Use below CSS
试试这个
.ie ul.menu{
宽度:500px;
左边距:自动;
右边距:自动;
显示:块;}
Try this
.ie ul.menu{
width: 500px;
margin-left: auto;
margin-right:auto;
display:block;}
实际上,如果您在菜单 div 周围放置边框,您将在 IE 中看到该 DIV 居中对齐,但是您的 ul 内容却并非如此。请将其弹出到您的 CSS 中,看看您会得到什么结果:
应该在 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:
Should work in IE.