我有一个奇怪的 CSS 问题,我无法解决。 我的页面布局相当复杂,具有固定的页眉和页脚以及灵活的内容区域。
现在在此内容区域内,我想创建可滚动的 div,它默认使用外部容器的整个高度,但不能超过。
一切正常,可滚动的 div 是页眉和页脚之间的唯一元素。
不幸的是,一旦我将可滚动的 div 放入一个简单的固定宽度容器中以使其在页面上居中,浏览器就会根据其内部内容调整其大小,因此不再可滚动,但下方的内容消失了因为外部 div 的溢出:隐藏。
非常感谢任何帮助,我真的花了最后几天时间试图解决这个问题。
谢谢,
帕斯卡
解决方案
很遗憾,我不能回答我自己的问题,这里是对我有用的解决方案:
我能够通过为 #container
指定 100%
的 height
来解决问题。
结果可以在这里看到:www.hacksrus.net/files/demo_working.html(我也不允许发布另一个超链接..;-))
如果有人能解释原因,我将不胜感激!
感谢您的帮助!
最佳答案
好的,试试这个,在 div 'scrollableContent' 之后,创建一个 div 并命名为 'centerAlign' 并将这段代码添加到你的 css 文件中
好的,这是你的代码,我测试过。
<!doctype html>
<html>
<head>
<title>Test</title>
<style>
* {
width: 100%;
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-size: 20px;
overflow: hidden;
}
header {
position: absolute;
height: 100px;
border: 1px solid #c00;
}
#content {
position: absolute;
top: 100px;
bottom: 100px;
border: 1px solid #c00;
max-height: 100%;
overflow: hidden;
}
#scrollableContent {
max-height: 100%;
overflow: auto;
}
footer {
position: absolute;
bottom: 0;
height: 100px;
border: 1px solid #c00;
}
#centerAlign {
width:745px;
margin:0 auto;
background-color:#999;
}
</style>
</head>
<body>
<header>header</header>
<div id="content">
<div id="scrollableContent">
<div id='centerAlign'>
<div>
<p>BEGIN</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>END</p>
</div>
</div>
</div>
</div>
<footer>footer</footer>
</body>
</html>
关于html - 最大高度 : 100% div within complex layout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14374509/