共计 2638 个字符,预计需要花费 7 分钟才能阅读完成。
HTML – 容器布局,使容器充满屏幕高度,自适应剩余高度
- 一. 实现方法(flex 布局)
- 二. 功能实例
一. 实现方法(flex 布局)
容器使用使用
display:flex
布局,自适应容器使用flex:1
充满屏幕
div class="container">
div class="header">div>
div class="content">内容div>
div class="footer">div>
div>
.container{
width: 100%;
height:100vh;
display:flex;
flex-direction: column;
}
.header{
width: 100%;
height:50px;
}
.content{
width: 100%;
flex:1;
}
.footer{
width: 100%;
height:50px;
}
优化:为防止出现内容超出容器情况,自适应容器中多嵌套一层
定位容器
,保证内容在容器中显示
div class="container">
div class="header">div>
div class="content">
div class="postion">内容div>
div>
div class="footer">div>
div>
.container{
width: 100%;
height:100vh;
display:flex;
flex-direction: column;
}
.header{
width: 100%;
height:50px;
}
.content{
width: 100%;
flex:1;
position: relative;
}
.postion {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
.footer{
width: 100%;
height:50px;
}
二. 功能实例
DOCTYPE html>
html lang="en">
head>
meta charset="UTF-8">
meta http-equiv="X-UA-Compatible" content="IE=edge">
meta name="viewport" content="width=device-width, initial-scale=1.0">
title>容器充满屏幕,自适应剩余的高度title>
style>
* {
margin: 0;
padding: 0;
}
html,
body {
width: 100%;
height: 100%;
display: flex;
background: #e2e2e2;
}
.menus {
width: 160px;
height: 100%;
border-right: 1px solid #bbb;
box-sizing: border-box;
display: flex;
flex-direction: column;
align-items: center;
}
.menuItem {
width: 130px;
height: 40px;
margin: 5px 0;
background: #fff;
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
.container {
width: calc(100% - 160px);
height: 100%;
display: flex;
flex-direction: column;
padding: 10px;
box-sizing: border-box;
}
.header {
width: 100%;
min-height: 50px;
border-bottom: 1px solid #bbb;
box-sizing: border-box;
background: #f0d4b9;
}
.content {
width: 100%;
flex: 1;
position: relative;
}
.postion {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
.info1 {
width: 100%;
height: calc(100% - 80px);
background: #b9e5f0;
display: flex;
align-items: center;
justify-content: center;
}
.info2 {
width: 100%;
height: 80px;
background: #c4b9f0;
display: flex;
align-items: center;
justify-content: center;
}
.footer{
width: 100%;
height: 50px;
border-top: 1px solid #bbb;
box-sizing: border-box;
display: flex;
justify-content: center;
align-items: center;
}
style>
head>
body>
div class="menus">
div class="menuItem" onclick="menuClick(this)">目录一div>
div class="menuItem" onclick="menuClick(this)">目录二div>
div>
div class="container">
div id="header" class="header">div>
div class="content">
div class="postion">
div class="info1">内容一div>
div class="info2">内容二div>
div>
div>
div class="footer">Footer 部分div>
div>
script>
function menuClick(evt) {
const title = evt.innerHTML
let inner = ''
switch (title) {
case "目录一":
for (let i = 0; i 2; i++) {
inner += `${title} 标题${i + 1}`
}
break;
case "目录二":
for (let i = 0; i 4; i++) {
inner += `${title} 标题${i + 1}`
}
break;
}
document.getElementById('header').innerHTML = inner
}
script>
body>
html>
原文地址: HTML – 容器布局,使容器充满屏幕高度,自适应剩余高度
正文完