保证金自动失败 IE11

Margin auto fails IE11

如果你 check my website 使用浏览器 Chrome 你可以看到 div 调用 .bg-content 如何与这种风格一起工作:

.bg-content {
    max-width: 605px;
    height: 149px;
    position: absolute;
    top: 62px;
    font-family: sans-serif;
    margin-left: auto;
    margin-right: auto;
    left: 0;
    right: 0;
}

但是如果你用 Internet Explorer 11 打开我的网站,div .bg-content 不居中,他向左浮动。

我该如何解决这个问题?

它不起作用,因为最大宽度;

试试这个:

.bg-content {
    width: 605px;
    height: 149px;
    position: absolute;
    top: 62px;
    font-family: sans-serif;
    margin-left: auto;
    margin-right: auto;
    left: 0;
    right: 0;
}

我经常用于 Internet Explorer(虽然主要用于旧版本)的技巧是设置父容器 text-align: center; 和容器本身 text-align: left;

我看过你的网站,父容器似乎没有默认宽度。请尝试将其设置为 width: 100%;。我在 运行 时间环境中尝试过,它似乎有效。

你应该使用

width: 100%;

在您的 .bg-content 元素中

示例: IE11 max-width & margin auto fix

对于固定位置元素:

left: 0;
right: 0:
width: 100%;
margin: 0 auto; 

对于绝对位置元素你可以使用这个

 position: absolute;
 margin: 0 auto;
 left: 0;
 right: 0;
 top: 200px; // change this as you want
 width: 450px; // change this as you want
 margin-left: auto;
 margin-right: auto;