javascript - 绝对位置时div高度为0

标签 javascript html css

不知道为什么2号线在1号线之上

我想在第 1 行之后添加第 2 行。我不想删除绝对位置,因为这与某些不同的网页有关。

Codepen Link of that Web page

 console.log('a', document.getElementById('a').getBoundingClientRect().height);
 console.log('b', document.getElementById('b').getBoundingClientRect().height);
 console.log('c', document.getElementById('c').getBoundingClientRect().height);
#a{
  position:realative;
  background:#ef0000;
}
#b{
  position:absolute;
}
#c{
  
}
<div id="a">
  <div id="b">
    <h2>Line 1 :</h2>
  </div>
  <div id="c">
    <h2>Line 2 :</h2>
  </div>
</div>

最佳答案

     <div id="a">
          <div id="b">
            <h2>Line 1 :</h2>
            <div id="c">
            <h2>Line 2 :</h2>
          </div>
          </div>

        </div>

    place div c inside b.

    Make id  'b' as ancestor to 'c'.so,since 'b'  is positioned absolute its child will be  positioned relative to 'b'. 

LINE1
LINE2

关于javascript - 绝对位置时div高度为0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50225272/

相关文章:

javascript - 去除字符串中的空格

jquery - 父级到子级悬停

css - 媒体查询——我们是否应该始终在原始/父样式表上设置默认值?

javascript - 表单仍然提交,尝试使用 e.preventDefault();

javascript - 谁能告诉我,为什么我的 js 代码没有在 jsfiddle 上运行?

javascript - 搜索时如何突出显示表格行而不仅仅是文本?

javascript - 如何使用 Jquery 每 2 秒更改一次图像

javascript - 如何使用 Javascript 编辑 XML 节点的数据

html - 仅使用 CSS 的渐变按钮

jquery - 动态页面导航元素对齐