子元素設置了絕對定位,父元素沒有設置相對定位,因此子元素脫離了文檔流,父元素高度就變成沒有了,如何讓父元素高度自適應子元素高度。
子元素的高度是不確定的
可以JS設置父元素高度
<body>
<<span class="<<span class=" hljs-keyword" style="box-sizing: border-box; font-weight: bold;">divid="father"> <<span
class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">div id="son"></<span
class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">div> </<span class="hljs-keyword"
style="box-sizing: border-box; font-weight: bold;">div>hljs-keyword" style="box-sizing: border-box; font-weight: bold;">divid="father"> <<span
class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">div id="son"></<span
class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">div> </<span class="hljs-keyword"
style="box-sizing: border-box; font-weight: bold;">div>
</body>
JS設置父元素高度
var father = document.getElementById('father'); var son = document.getElementById('son'); father.style.height=son.offsetHeight+'px';
css給父元素設置overflow:hidden也可以包裹子元素
在子元素最后一個元素后面:添加一個空元素,設置style:clear:both
