元素宽高自适应大家第一时间想到的肯定是百分比,但是百分比对于不熟悉的人都不太友好,元素的百分比取决与父级元素的宽高,宽度百分比很好实现,但是高度一般用px表示,因为很少有人给元素设置固定的值,想要实现高度自适应其实只需要一行代码,不多bb了上代码
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
text-align: center;
}
body,
html {
width: 100%;
height: 100%;
/* 给body加上高度之后他的子元素就可以使用百分比了 */
}
header {
width: 100%;
height: 20%;
background: pink;
}
aside:nth-child(2) {
width: 200px;
height: 70%;
background: #5f5;
float: left;
}
aside:nth-child(3) {
width: 200px;
height: 70%;
background: #5f5;
float: right;
}
main {
width: clac(100% - 400px);
height: 70%;
background: #ccc;
}
footer {
width: 100%;
height: 10%;
background: #911;
}
</style>
</head>
<body>
<header>头</header>
<aside>左</aside>
<aside>右</aside>
<main>身体</main>
<footer>脚</footer>
</body>
这里是一个很实用的圣杯布局,当然对于部分设备还是很不友好的,
建议搭配min使用,具体可以自己调试一下