overflow 属性规定当内容溢出元素框时发生的事情。定义当一个元素的内容太大而无法适应 块级格式化上下文时候该做什么。它是 overflow-x 和overflow-y的 简写属性 。
MDN: https://developer.mozilla.org/zh-CN/docs/Web/CSS/overflow
auto:如果内容被修剪,则浏览器会显示滚动条以便查看其余的内容。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* {
margin: 0;
padding: 0;
}
.head {
height: 48px;
background-color: aqua;
}
.c1 {
position: absolute;
background-color: #8aab30;
left: 0;
width: 118px;
}
.c2 {
position: absolute;
background-color: #0000FF;
left: 120px;
right: 0;
top: 48px;
bottom: 0;
overflow: auto;
}
li {
list-style: none;
}
</style>
</head>
<body>
<div class="head"></div>
<div class="body-body">
<div class="c1">
<ul>
<li>241</li>*40个,调试的时候可以多写一点
</ul>
</div>
<div class="c2">
<ul>
<li>241</li>*40个,调试的时候可以多写一点
</ul>
</div>
</div>
</body>
</html>
顶部栏和底部栏固定不动,中间可以滚动
父元素使用 flex
布局,排列方向为垂直。为顶部的 header
和底部的 footer
分别设置宽高。中间的部分设置为 flex: 1
,其 overflow: auto
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.container {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
}
.header {
width: 100%;
height: 100px;
background: #f99;
}
.content {
width: 100%;
height: 100%;
overflow: auto;
background: yellow;
flex: 1;
}
.footer {
width: 100%;
height: 100px;
background: #99f;
}
</style>
</head>
<body>
<div class="container">
<div class="header"></div>
<div class="content">
<ul>
<li>1234567890</li>*40个,调试的时候可以多写一点
</ul>
</div>
<div class="footer"></div>
</div>
</body>
</html>
很多移动端的 APP 都会实现点击左边的分类,右边出现对应的分类内容。
这其中就是使用 overflow: auto
父元素使用 flex
布局,对于超出的部分使用 overflow: hidden;
,左半部分设置 width
,overflow
设置为 auto
,右半部分设置 flex
为 1,overflow
设置为 auto
。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
li {
list-style: none;
height: 30px;
}
.tab-bar {
width: 100%;
height: 50px;
background-color: aqua;
}
.menu-inner {
position: absolute;
right: 0;
bottom: 0;
left: 0;
top: 50px;
display: flex;
overflow: hidden;
}
.left-bar {
width: 100px;
background-color: #efefef;
overflow: auto;
height: 100%;
}
.right-content {
flex: 1;
margin-left: 100px;
height: 100%;
overflow: auto;
}
.shop-bar {
width: 100%;
height: 50px;
background-color: blueviolet;
position: absolute;
bottom: 0;
left: 0;
right: 0;
z-index: 99;
}
</style>
</head>
<body>
<div class="detail">
<div class="tab-bar">导航栏</div>
<div class="menu-inner">
<div class="left-bar">
<li>123456</li>*40个,调试的时候可以多写一点
</div>
<div class="right-content">
<li>123456</li>*40个,调试的时候可以多写一点
</div>
</div>
<div class="shop-bar">
底部栏
</div>
</div>
</body>
</html>
赶快来动手实践一下吧。