我试图解决的问题非常简单,但我很难用CSS技术实现我想要的目标。
我想要的是将某种高度设置为相对值的父div
(例如:100%或30%)。请不要问为什么我需要它,因为这只是一个部分,解释整个布局超出了这个问题。
在此父级内部,我需要一个标题h1
,后跟包含大量文本的子级div
。 最重要的,我只需要在子div中设置滚动条,这样标题将始终保持附加到容器的顶部。
标记:
<div class="wrapper">
<h1>Test</h1>
<div class="text">
Lorem ipsum (... lots of text)
</div>
</div>
(NOT)工作小提琴: http://jsfiddle.net/CodeConstructors/BEVSS/
答案 0 :(得分:10)
你想要达到this的东西吗?
的 HTML 强>
<div class="wrapper">
<div class="header">
HEADER
</div>
<div class="content">
Lorem ipsum (... lots of text)
</div>
</div>
<强> CSS 强>
html, body {
height: 100%;
margin: 0px;
padding: 0px;
}
.wrapper {
width: 400px;
height: 100%;
background-color: #fec;
margin: auto;
position: relative;
}
.header {
height: 40px;
background-color: green;
color: #fff;
}
.content {
position:absolute;
bottom:0px;
top: 40px;
width:100%;
overflow: auto;
background-color: #333;
color: #666;
}
答案 1 :(得分:1)
您可以使用CSS3属性calc(),如下所示:
html, body { height: 100% }
.wrapper {
height: 30%;
overflow: hidden;
width: 400px;
background-color: yellow;
}
h1 {height: 20px}
.text {
overflow: auto;
height:-webkit-calc(100% - 20px);
height: -moz-cale(100% - 20px);
height: -ms-cale(100% - 20px);
height: -o-cale(100% - 20px);
height: cale(100% - 20px);
}