css - 如何解决 IE 错误,它无法在没有固定大小的父元素中正确地绝对居中元素?

标签 css internet-explorer

我遇到了 IE11(也许其他 IE 也有)的问题,如果父元素没有固定大小,则居中元素(使用绝对位置方法)定位不正确。大小由内容决定。

是否有任何已知的解决方法?

例子:

.box{
    position: absolute;
    top:0;
    right: 0;   
    left: 0;
    bottom:0;
    margin:auto;
    height:20px;
    width:20px;  
    background-color:#000;
}

.container{
    position: relative;
    background:red;
    padding-left:100px;
 }
<table>
  <tr>
    <td class="container">
      <div class="box">
      </div>
    </td>
    <td>
      Some content Some content Some content Some content Some content <br>
      Some content Some content Some content Some content Some content <br>
      Some content Some content Some content Some content Some content <br>
      Some content Some content Some content Some content Some content <br>
    </td>
  </tr>
</table>

最佳答案

你应该使用<div>作为 parent ,因为 <td> 的相对位置不能在 IE 上运行

检查更新的片段

.bg {
  background: red;
}

.box {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  margin: auto;
  height: 20px;
  width: 20px;
  background-color: #000;
}

.container {
  position: relative;
  padding-left: 100px;
}
<table>
  <tr>
    <td class="bg">
      <div class="container">
        <div class="box">
        </div>
      </div>
    </td>
    <td>
      Some content Some content Some content Some content Some content <br> Some content Some content Some content Some content Some content <br> Some content Some content Some content Some content Some content <br> Some content Some content Some content
      Some content Some content <br>
    </td>
  </tr>
</table>

关于css - 如何解决 IE 错误,它无法在没有固定大小的父元素中正确地绝对居中元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43169882/

相关文章:

javascript - IE9 兼容模式下的 XMLSerializer 不起作用

html - Internet Explorer 8 规模问题

c++ - 如何在 MFC 中获取嵌入式 Web 浏览器控件的 HWND

CSS水平菜单: Text Align Bottom

css - Chrome 和谷歌地图的 z-index 堆叠问题

html - 使用 ':nth-of-type' 一起选择所有子元素,而不是单独选择

javascript - 针对 :hover DOM with querySelectorAll 的问题

html - 不包括 IE 6/7 流量

javascript - 屏幕缩小时试图隐藏 Logo

html - 选择选项的 IE 样式问题同时具有 selected 和 disabled 属性