解决css头部导航栏固定,主体内容铺满屏幕,左侧侧边栏固定高度,右侧内容高度自适应

头部固定高度,主体内容高度设置100%,是根据父元素实际高度决定的,这样会多出一部分高度,解决代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
    // 根标签高度铺满屏幕
        html,body {
            height: 100%;
            margin: 0;
            padding: 0;
        }
        .container {
            height: 100%;  
        }
        header {
            width: 100%;
            height: 60px;
            background-color: red;
        }
        // 主体绝对定位
        .contont {
            position: absolute;
            left: 0;
            top: 60px;
            bottom: 0;
            width: 100%;
            
        }
        //侧边栏浮动
        .aside {
            float: left;
            width: 200px;
            height: 100%;
            background-color: green;
        }
        // 右侧overflow: auto滚动条
        .main {
            margin-left: 200px;
            height: 100%;
            background-color: yellow;
            overflow: auto;
            
        }
        h3 {
            margin-top: 1200px;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>头部</header>
        <div class="contont">
            <div class="aside">侧边栏</div>
            <div class="main">
                <h3>主体</h3>
            </div>
        </div>
    </div>
</body>
</html>
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的侧边抽屉式导航栏的HTML和CSS代码示例: HTML代码: ``` <div class="wrapper"> <div class="sidebar"> <a href="#" class="close-btn">×</a> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </div> <div class="main-content"> <button class="open-btn">☰ Open Sidebar</button> <h1>Welcome to my website</h1> <p>This is the main content of my website.</p> </div> </div> ``` CSS代码: ``` * { margin: 0; padding: 0; box-sizing: border-box; } .wrapper { display: flex; height: 100vh; } .sidebar { position: fixed; top: 0; left: -300px; width: 300px; height: 100%; background-color: #333; color: #fff; transition: all 0.3s ease-in-out; } .sidebar ul { list-style: none; padding: 20px; } .sidebar ul li { margin-bottom: 10px; } .sidebar ul li a { color: #fff; text-decoration: none; } .sidebar ul li a:hover { text-decoration: underline; } .close-btn { position: absolute; top: 10px; right: 10px; font-size: 30px; color: #fff; text-decoration: none; } .open-btn { position: fixed; top: 20px; left: 20px; font-size: 30px; background-color: #333; color: #fff; border: none; padding: 10px; cursor: pointer; } .main-content { flex: 1; padding: 20px; } ``` 在这个示例中,侧边栏初始状态是隐藏的,点击“Open Sidebar”按钮后会滑出一个300像素宽的侧边栏,显示导航链接。点击“X”按钮或者页面其他区域,侧边栏会滑回隐藏状态。在主内容区域,我们使用了flex布局内容区域自适应填充剩余空间。 需要注意的是,这个示例是一个基础的模板,实际项目中需要根据需求进行适当的调整和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值