css - IE 和 Chrome 中的定位和居中表现不一致问题
伊谢尔伦
伊谢尔伦 2017-04-17 13:15:02
0
2
588

浏览器版本是IE 11和Chrome 50
html 代码

    <p class="outer">
        <p class="inner"></p>
    </p>

CSS代码

    .outer {
        position: relative;
        width: 400px;
        height: 200px;
        background: #f44336;
        
        /* 在子元素有绝对定位的情况下,在IE中居中无效, Chrome中偏向中间,但是并不完全居中。 IE 11则完全向左*/
        text-align: center;
    }
    .inner {
        /* 去掉绝对定位,在IE 11 和Chrome中,inner居中正常*/
        position: absolute;
        display: inline-block;
        width: 100px;
        height: 100px;
        background-color: #eee;
    }

代码链接

复制链接,分别在IE 和Chrome 中粘贴打开,你会发现表现不一样,

  • .inner元素, 有绝对定位而且display设定为inline-block时,在IE中.outertext-align: center无效,Chrome中也不会正常居中而是会偏一点,不知道为什么 ???

  • 去掉绝对定位就一切正常了

先行谢谢

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
小葫芦

Set text-align: right;

Set text-align: left;

When an inline element has position, text-align: right; will align the element to the right of the parent element;
text-align: left; align the element to its left, text-align: center; and the middle The lines are aligned, so it will not be completely horizontally centered. As for IE, it is different, which is a manifestation of IE being confused

小葫芦
position:absolute;// 这个属性已经使元素脱离了文档流
display:inline-block;// display是基于文档流的属性,自然是不会起到任何作用

To sum up: display:inline-block; is an invalid statement, and statements based on it will not work either

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!