【问题标题】:Vertically align absolute positioned DIV with left value inside TD element in IE11将绝对定位的 DIV 与 IE11 中 TD 元素内的左值垂直对齐
【发布时间】:2018-12-17 11:32:05
【问题描述】:

我已经使用 position: absolute 和 left 值在 TD 元素内定位了一些 div 元素,但我需要在中间垂直对齐 div 没有固定边距和填充值,因为我的TD高度可以是任何东西。我添加了示例供参考,有帮助吗?

.content {
  position: absolute;
  left: 20px;
  background: grey;
  width: 100px;
  height: 20px;
}

td {
  border: 1px solid grey;
  border-collapse: collapse;
  /* Works in chrome, Firefox, Edge but not in IE 11*/
  display: flex;
  align-items: center;
}

.content2 {
  position: absolute;
  left: 140px;
  background: grey;
  width: 100px;
  height: 20px;
}
<table>
  <tr>
    <td style='width:300px;height:30px;'>
      <div class='content'>
      </div>
      <div class='content2'>
      </div>
    </td>
  </tr>
</table>

谢谢,

【问题讨论】:

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


    【解决方案1】:

    您可以将display: inline-block 用于 div,将vertical-align:middle 用于单元格。然后无论表格行有多高,它都会将 div 的垂直居中对齐。

      .content {
         display: inline-block;
         background: grey;
         width: 100px;
         height: 20px;
         vertical-align: middle;
       }
      tr{
        height: 90px;
      }
       td {
         border: 1px solid grey;
         border-collapse: collapse;
       }
    
       .div1{
         margin-left: 20px;
       }
       .div2{
         margin-left: 40px;
       }
    
    <table>
      <tr>
        <td style='width:300px;height:30px;'>
          <div class='content div1'>
          </div>
          <div class='content div2'>
          </div>
        </td>
      </tr>
    </table>
    

    Fiddle

    注意,与float 不同,div 之间会显示inline-block 空格!因此,除非您将它们编码为 &lt;div&gt;...&lt;/div&gt;&lt;div&gt;...&lt;/div&gt;,否则您会看到一些不需要的额外空间。

    【讨论】:

    • 是的,感谢您的回答,但在我的情况下,我想将 div 放在表格中的特定位置(左),所以我必须将位置设置为绝对位置。
    • 为什么?只需添加一个margin-left。
    • 附注:表格单元格上的height 属性不起作用,它适用于表格行或内容。
    • 感谢您的帮助,我们可以放置,但是在我的应用程序中,这些 div 可以落在同一位置,这些放置是从表格的起点开始进行一些计算的。
    猜你喜欢
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    相关资源
    最近更新 更多