欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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>
相关标签: CSS