CSS之height: 100%和height: auto的区别

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>
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值