微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

html – div在td中绝对定位时留空间

我试图理解绝对的位置.我有以下代码
div#div1 {
  position: absolute;
  left: 0;
  right: 0;
  height: 100px;
  border: 1px solid green;
  background-color: green;
  width: 100%;
}

td {
  position: relative;
  border: 1px solid blue;
  height: 18px;
  width: 100%;
}

table {
  width: 100%;
}
<table>
  <tr>
    <td>
      <div id="div1">
        This is a heading with an absolute position
      </div>
    </td>
  </tr>
</table>

由于定位绝对,我在顶部获得了一些额外的空白区域.
当我指定top:0px时,它工作正常.

有人可以解释为什么只使用左右定位时会留下一些空间.

解决方法

表格单元格的认垂直对齐是基线.在第一个<表>中可以看到这种效果.下面.

这导致表格内容,文本或< div>,被放置在垂直中心周围.

如果你想移动< div>到顶部,vertical-align:top;会做的伎俩.或顶部:0;.

div#div1 {
  position: absolute;
  left: 0;
  right: 0;
  height: 100px;
  border: 1px solid green;
  background-color: green;
  width: 100%;
  Box-sizing: border-Box;
}

td {
  position: relative;
  border: 1px solid blue;
  height: 100px;
  width: 100%;
}

table {
  width: 100%;
}
<!DOCTYPE html>
<html>

<body>
  <table>
    <tr>
      <td>
        Some text
      </td>
    </tr>
  </table>

  <table>
    <tr>
      <td>
        <div id="div1">This is a heading with an absolute position</div>
      </td>
    </tr>
  </table>
</body>

</html>

原文地址:https://www.jb51.cc/html/226118.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐