CSS之height: 100%和height: auto的区别
程序员文章站
2022-05-01 12:29:23
...
height: auto —— 是指根据块内内容自动调节高度(高度自然撑开)
height:100% —— 是指其相对父块高度而定义的高度,也就是按照离它最近且有定义高度的父层的高度来定义高度
例:随着窗口宽度的改变,导航栏的位置发生变化(大于700时为侧边导航栏,700到400之间为顶部水平导航栏,小于400为顶部垂直导航栏)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body {
margin: 0;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: auto;
background: rgb(208, 208, 208);
position: fixed;
width: 25%;
height: 100%;
top: 0;
}
ul li {
display: block;
padding: 14px 16px;
text-align: center;
}
ul li:hover {
background: rgb(173, 173, 173);
cursor: pointer;
color: white;
}
div {
margin-left: 25%;
padding: 1px 16px;
height: 1000px;
}
@media screen and (max-width:700px) {
ul {
width: 100%;
height: auto;
position: relative;
}
ul li {
float: left;
}
div {
margin-left: 0;
}
}
@media screen and (max-width: 400px) {
ul li {
float: none;
}
}
</style>
</head>
<body>
<ul>
<li>主页</li>
<li>推荐</li>
<li>摄影</li>
<li>娱乐</li>
</ul>
<div>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
<p>Some text some text some text some text..</p>
</div>
</body>
</html>
推荐阅读
-
H5 canvas中width、height和style的宽高区别详解
-
关于CSS和JS中用到的各种Height和Width的问题
-
关于height:100%和height:100vh的区别了解吗,进来看看
-
CSS中width和height的默认值auto与%案例详解
-
css之Display、Visibility、Opacity、rgba和z-index: -1的区别
-
css中z-index: 0和z-index: auto的区别
-
CSS布局之浮动(float)和定位(position)属性的区别
-
line-height属性值为百分比和数字乘积因子的区别_html/css_WEB-ITnose
-
line-height和font-size的缩写形式_html/css_WEB-ITnose
-
div+css布局之padding和margin的使用,及自适应100%的宽度_html/css_WEB-ITnose