0

像许多设计师一样,我用来margin: 0 auto;使元素居中。在尝试在http://www.caniuse.com上检查浏览器对此功能的支持时,我找不到任何相关内容。

此功能是否有浏览器兼容性矩阵?

4

2 回答 2

2

尽管您可能不想调整代码以在不支持的古董浏览器中工作margin: 0 auto;,但是既然您问了:

支持仅从 IE6 开始。如果要支持早期的浏览器,可以添加text-align: center;到父元素。这是可行的,因为旧浏览器也错误地适用text-align于块元素。同时,margin: 0 auto;为现代浏览器保留。如果你也想text-align: center在现代浏览器中工作,你也可以设置display: inline-block;- 那么你就不需要margin: 0 auto;.

因此,假设这是您的 HTML:

<div id="outer">
    <div id="inner"></div>
</div>

您有以下选择:

选项1

#outer {
    background: pink;
    width: 100%;
    text-align: center; /* for very old browsers */
}

#inner {
    background: green;
    display: inline-block;
    width: 50px;
    height: 50px;
    margin: 0 auto; /* for >99% of browsers */
}

选项 2

#outer {
    background: pink;
    width: 100%;
    text-align: center;
    height: 50px; /* height of child - necessary for IE8 and IE9,
    otherwise the height is slightly larger than that of the child */
}

#inner {
    background: green;
    display: inline-block; /* necessary for modern browsers, IE8+ */
    width: 50px;
    height: 50px;
}

但正如我所说,在支持这些古老的浏览器之前,您可能真的会考虑额外的努力是否值得,或者是否最好放弃对它们的支持。

于 2017-03-08T06:57:19.403 回答
1

https://developer.mozilla.org/en/docs/Web/CSS/margin,所有浏览器都margin: 0 auto;完全支持。

于 2016-04-10T03:07:52.617 回答