【问题标题】:Does text-align center and absolute positioning not work simultaneously in IE-11?文本对齐中心和绝对定位在 IE-11 中不能同时工作吗?
【发布时间】:2016-09-02 10:13:44
【问题描述】:

以下脚本(https://jsfiddle.net/u9z76r6q/2/) 不在 IE-11 中居中:

.container {
  display: block;
  width: 400px;
  height: 200px;
  text-align: center;
  border: 1px solid black;
}
span {
  position: absolute;
 
}
<a href="#" class="container">
  <span>
    <img src="https://redarrowmedia.files.wordpress.com/2012/03/arrow-logo1.png" alt="" width="40px" height="40px">    
  </span>
  </a>

在所有其他现代浏览器中,箭头图像居中。这是 Internet Explorer 中的错误吗?

【问题讨论】:

  • 在我看来 Chrome/FF 和 IE 是错误的。 IE 的行为就像将 left 设置为 0 和 Chrome/FF 一样,就好像元素没有宽度一样。老实说,我在规范中找不到关于text-align: centerleft: auto 组合的信息,但在CSS absolute and fixed positioning 中的描述之外,我会说IE 肯定是错误的,Chrome/FF 稍微有点。
  • 很有趣,曾经只有 IE 以这种方式表现......其他浏览器不介意文本对齐......我不明白为什么他们应该将元素排除在流之外。很久以前,我养成了重新定义左坐标以在任何地方都有相同行为的习惯。就像 CSS 中的任何其他安全重置一样
  • @GCyrillus 其他浏览器应该注意文本对齐,因为这里leftauto

标签: html css internet-explorer css-position internet-explorer-11


【解决方案1】:

绝对定位元素不占用父元素的宽度,它的宽度取决于内容。不同的结果是因为不同的浏览器定位跨度不同。将 width: 100%; left: 0; 添加到 span。

UPD:Chrome 中的屏幕截图:

IE 中的截图:

您可以在 IE 中看到 span 位于左侧,而在 Chrome 中位于居中。 span 的内容仍然与中心对齐,但 span 宽度太小而看不到它!将宽度添加到 span 并查看里面的内容将对齐到中心。 小提琴:https://jsfiddle.net/u9z76r6q/3/ 在 IE 中观看

UPD2:实际上在 Chrome 中 span 也不在中心。它的行为类似于left: 50%;。它的左边框在中间。

【讨论】:

  • 他们对它的定位有何不同? left:0 始终是默认值。而绝对位置和text-align: center有什么关系?
  • 在 chrome 和 ie 中打开你的小提琴并打开开发工具。你会看到 span 的位置不同。
  • Text-align: center 不会影响绝对定位的元素本身,只会影响其内容。
  • 在开发工具中,span 绝对定位在 IE-11、chrome 和 firefox 中。另外,为什么绝对位置元素是否采用它的父级宽度很重要?我没有为 span 分配任何宽度。
  • 不,它在 IE 中的工作方式相同。但我认为translateX(-50%) 在旧版浏览器中可能会出现问题。
猜你喜欢
  • 2016-10-26
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 2016-11-17
  • 2014-10-19
  • 2018-09-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多