Home  >  Q&A  >  body text

css - IE 和 Chrome 中的定位和居中表现不一致问题

浏览器版本是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 中粘贴打开,你会发现表现不一样,

先行谢谢

伊谢尔伦伊谢尔伦2717 days ago590

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-04-17 13:17:02

    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

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 13:17:02

    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

    reply
    0
  • Cancelreply