在實際使用中,我們布局時會遇到,父級下面有多個元素,上面的子元素為固定高度,后面的子元素自動充滿父級的剩余空間,如圖:
容器元素500*500,父級元素100%占滿,黃色區域高100px,讓紅色區域自動充滿父級的剩余空間
使用定位實現
HTML
<div class="container"> <div class="father"> <div class="header"></div> <div class="content"></div> </div> </div>
CSS
.container{ width: 500px; height: 500px; background-color: #0bb8b2; } .father { width: 100%; height: 100%; position: relative; background-color:green; } .header{ height: 100px; background-color:yellow; } .content{ position: absolute; left: 0; right: 0; top: 100px; bottom: 0; background-color:red; }