加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

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

发布时间:2020-12-14 18:31:56 所属栏目:资源 来源:网络整理
导读:我试图理解绝对的位置.我有以下代码段 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%;
我试图理解绝对的位置.我有以下代码段
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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读