在我的页面中,我有一个基于其父级的高度为 100% 的元素;但是,我希望这个元素在必要时自动垂直滚动。这是我的例子。
我做不到。
我添加了以下CSS:
height:100%;
max-height:100%;
事实上,我想要的是确保.content
始终在.container
. 像这样:
(顺便说一句,在上图中,我使用max-height:400px
应该避免的,因为它的最大高度应该基于 的高度.container
)
有任何想法吗?
这接近您正在寻找的解决方案,尽管您可能需要调整填充等以获得更美观的外观:http: //jsfiddle.net/GJ5yM/
新的 CSS:
.content{
height:100%;
position: absolute;
}
.windowcontent{
background-color: white;
padding: 10px;
max-height:100%;
position:relative;
overflow-y:auto;
}
希望这是您正在寻找的:
CSS:
#scrollFrameWrap {
width: 400px;
margin: 20px auto;
background: #000;
border-radius: 8px;
box-shadow: 0px 1px 15px 0 #fff inset;
padding: 3px 10px 10px;
}
.title {
text-align: center;
font-size: 12px;
color: #aaa;
}
#parentElement {
height: 150px;
padding-bottom: 10px;
margin-bottom:10px;
}
#scrollFrame {
width: 380px;
height: 100%;
overflow: scroll;
overflow-x: hidden;
background: #fff;
border-radius: 4px;
padding: 10px;
}
的HTML:
<div id="scrollFrameWrap">
<p class="title">Title</p>
<div id="parentElement">
<div id="scrollFrame">
<p>This is a sentence, hope you like it.</p>
<p>This is a sentence, hope you like it.</p>
<p>This is a sentence, hope you like it.</p>
<p>This is a sentence, hope you like it.</p>
<p>This is a sentence, hope you like it.</p>
<p>This is a sentence, hope you like it.</p>
<p>This is a sentence, hope you like it.</p>
<p>This is a sentence, hope you like it.</p>
<p>This is a sentence, hope you like it.</p>
<p>This is a sentence, hope you like it.</p>
</div>
</div>
</div>
看看有没有帮助
只设置
overflow-y: scroll;
并设置任何你想要的高度。