子元素絕對定位,父元素高度自適應子元素高度


   子元素設置了絕對定位,父元素沒有設置相對定位,因此子元素脫離了文檔流,父元素高度就變成沒有了,如何讓父元素高度自適應子元素高度。

   子元素的高度是不確定的

   可以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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM