头部和底部固定,中间内容可滚动

本文介绍了一种使用HTML和CSS实现的网页布局方案,该方案可以让页面的顶部和底部保持固定位置,而中间的内容区域则可以滚动。通过简单的代码示例展示了如何设置各个部分的位置和高度,以及如何控制溢出内容的显示。
html+css的代码的展示
 
 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>上下固定,中间内容可以滚动</title>
<style type="text/css">
html,body,.page{width:100%; height:100%;overflow:hidden;}
.top,.con,.bottom{position:absolute; left:0; right:0;}
.top{top:0;z-index:1; height:30px;background:#ff0;}
.con{top:30px; bottom:40px; overflow:auto;}
.bottom{bottom:0;z-index:1; height:40px;background:#00f;}
 
</style>
</head>
<body>
<div class="wrap">
<div class="top">头部</div>
<div class="con">中间内容<br/>
中间内容<br/>
中间内容<br/>
中间内容<br/>
中间内容<br/>
中间内容<br/>
中间内容<br/>
 
</div>
<div class="bottom">底部内容</div>
</div>
</body>
</html>
### CSS实现头部底部固定中间内容滚动的布局示例 以下是通过CSS实现头部底部固定中间内容滚动的布局代码示例: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>头部底部固定中间内容滚动</title> <style type="text/css"> * { margin: 0; padding: 0; box-sizing: border-box; } html, body { height: 100%; width: 100%; overflow: hidden; } .container { display: flex; flex-direction: column; height: 100vh; } .header { background-color: #ff0; height: 50px; display: flex; align-items: center; justify-content: center; } .content { flex: 1; overflow-y: auto; background-color: #f0f0f0; padding: 10px; } .footer { background-color: #00f; height: 40px; display: flex; align-items: center; justify-content: center; color: white; } </style> </head> <body> <div class="container"> <div class="header">头部内容</div> <div class="content"> <p>中间内容滚动区域</p> <p>中间内容滚动区域</p> <p>中间内容滚动区域</p> <p>中间内容滚动区域</p> <p>中间内容滚动区域</p> <p>中间内容滚动区域</p> <p>中间内容滚动区域</p> <p>中间内容滚动区域</p> <p>中间内容滚动区域</p> <p>中间内容滚动区域</p> <p>中间内容滚动区域</p> </div> <div class="footer">底部内容</div> </div> </body> </html> ``` 上述代码中使用了`flexbox`布局[^2]。`.container`作为父容器,设置了`display: flex``flex-direction: column`,以确保子元素按照垂直方向排列。`.header``.footer`分别设置固定高度,而`.content`通过`flex: 1`占据剩余空间,并且设置了`overflow-y: auto`以实现滚动效果。 #### 注意事项 - `height: 100vh`确保整个页面的高度为视口高度[^2]。 - `overflow: hidden`用于防止页面整体出现滚动条[^1]。 - 使用`box-sizing: border-box`可以更方便地控制元素尺寸[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值