状况一:html
内容不足一屏时使父级背景铺满一屏,内容太高时,父级背景被内容撑开:布局
HTML: <body> <div class="container"> <div class="content"> <!-- 内容 --> </div> </div>
</body>
CSS: html, body { height: 100%; background-color: #ccc; } .container { height: calc(100% - 20px); padding: 10px; padding-bottom: 0; } .content { background-color: #fff; height: auto; margin-bottom: 10px; min-height: 100%;
}
状况二: spa
若是只须要一屏展现,假如里面内容每块高度须要占比50%,则按照下面布局和样式:code
HTML: <body> <div class="container"> <div class="content"> <!-- 内容 --> <div class="box1"></div> <div class="box2"></div> </div> </div> </body>
CSS: html, body { height: 100%; background-color: #ccc; } .container { height: calc(100% - 20px); padding: 10px; padding-bottom: 0; } .content { background-color: #fff; height: 100%; margin-bottom: 10px; } .box1 { height: 50%; background-color: #eee; }
状况三: htm
实际项目开发中,一般在公共common里写第一种,若有一屏展现的特殊状况,可用样式冲掉。blog
效果图:开发