我有以下代码段:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body style='overflow: hidden;'>
<style>
*, :after, :before {
box-sizing: border-box;
}
.example {
display: flex;
flex-direction: column;
justify-content: flex-end;
height: 100%;
max-height: 100%;
}
.bottom {
overflow: auto;
height: 140px;
max-height: 100%;
flex-shrink: 0;
white-space: pre-wrap;
}
.top {
position: relative;
display: flex;
}
.scroll {
overflow-y: scroll;
width: 100%;
}
.elements {
position: relative;
}
.test {
margin-top: 20px;
}
</style>
<article class="example">
<div class="top">
<div class="scroll">
<article class="elements">
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
</article>
</div>
</div>
<article class="bottom">
Bottom
</article>
</article>
</body>
</html>
答案 0 :(得分:1)
您没有为文章容器设置高度(也可以使用overflow:auto;)。
.elements {
position: relative;
overflow: auto;
height: 150px;
}
答案 1 :(得分:1)
对您的代码进行位更改
*,
:after,
:before {
box-sizing: border-box;
}
/* some normalization to document */
html,
body {
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
.example {
display: flex;
flex-direction: column;
justify-content: flex-end;
height: 100%;
max-height: 100%;
}
.bottom {
overflow: auto;
height: 140px;
max-height: 100%;
flex-shrink: 0;
white-space: pre-wrap;
}
/* handling overflow here */
.top {
position: relative;
display: flex;
overflow: auto;
}
/* removed overflow here */
.scroll {
width: 100%;
}
.elements {
position: relative;
}
.test {
margin-top: 20px;
}
<article class="example">
<div class="top">
<div class="scroll">
<article class="elements">
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
<article class="test">
TEST
</article>
</article>
</div>
</div>
<article class="bottom">
Bottom
</article>
</article>