CSS - 最小高度为 100% 的内容创建垂直滚动条
我正在尝试创建一个带有标题的页面,其中包含导航菜单和填充页面其余部分的内容区域,内容的最小高度为 100%,但即使它是空的,由于标题的原因,它也会显示垂直滚动条尺寸。
HTML 相关部分:
<body>
<div id="header">Davi Andrade</div>
<nav>
<ul>
<li><a href="/">About</a></li>
<li><a href="/">Portfolio</a></li>
<li><a href="/">Downloads</a></li>
<li><a href="index.html">Home</a></li>
</ul>
</nav>
<div id="content">
Text
</div>
</body>
和 CSS:
html, body {
background: #333333;
font-family: "Segoe UI", "Lucida Grande", sans-serif;
height: 100%;
margin: 0;
padding: 0;
}
#header {
color: #b6b6b6;
float: left;
font-family: Megrim;
font-size: 36px;
margin: 18px 52px 18px 52px;
text-shadow: 0 0 3px rgba(0, 18, 255, 0.8), 0 1px 2px rgba(0, 0, 0, 0.5);
text-transform: uppercase;
}
nav li a {
color: white;
display: block;
float: right;
font-size: 1.3em;
list-style: none;
padding: 24px 48px 24px 0;
text-decoration: none;
}
#content {
clear: both;
background: #3e3e3e;
box-shadow: 0 -2px 3px rgba(0,0,0,0.35), 0 -1px 0 rgba(255,255,255,0.3);
min-height: 100%;
}
有什么方法可以解决这个问题吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据您想要实现的目标,可以通过两种方式之一来实现此布局。
首先,如果您不需要页面底部的任何内容,那么您应该删除
#content
中的min-height
和background-color
并将background-color
放在body
中。我还会稍微更改标题 HTML 结构,使其在语义上更正确且更易于使用:HTML
CSS
其次,如果您确实需要在底部添加页脚那么您需要进行上述更改,然后将 HTML 包装在带有
min-height: 100%
的容器元素中。然后,您可以使用position:absolute
将页脚元素放置在容器元素的底部。还有一些其他的细节,我在 这个 Stackoverflow 答案 中进行了更详细的解释。带页脚:http://jsfiddle.net/cNfWZ/1/
没有页脚:http://jsfiddle.net/cNfWZ/
Depending on what you are trying to achieve, this layout can be achieved in one of two ways.
Firstly, if you do not need anything at the foot of the page then you should just remove
min-height
andbackground-color
off#content
and place thebackground-color
in thebody
instead. I would also change the header HTML structure slightly to make it a little more semantically correct and easier to work with:HTML
CSS
Secondly, if you do need a footer at the bottom of the page then you will need to make the changes above and then wrap your HTML inside a container element with
min-height: 100%
. You can then useposition: absolute
to place the footer element at the bottom of the container element. There are a few other bits and pieces to it which I have explained in more detail within this Stackoverflow answer.With footer: http://jsfiddle.net/cNfWZ/1/
Without footer: http://jsfiddle.net/cNfWZ/
看这里:
让 div 填充剩余屏幕空间的高度
使用普通的 CSS 和 HTML 确实不可能做到这一点。
如果您想要的只是背景为浅灰色,我建议您将正文的背景设置为该颜色,然后将标题和导航设置为深灰色。内容看起来像是填满了其余部分,但实际上并非如此。
编辑:
抱歉,我应该说使用 div 和 body 元素是不可能的。有了桌子,您就可以实现您想要的目标。
Look here:
Make a div fill the height of the remaining screen space
This really isn't all that possible using normal CSS and HTML.
If all you want is for the background to be that lighter gray, I would suggest that you make the background of the body be that color and then make the header and nav be the darker grey. The content will look like it fills the rest, but won't actually.
Edit:
Sorry, I should say not possible using a div and the body element. With a table you can achieve something like what you are going for.
Steve Sanderson 在这里对 CSS 高度问题做了很好的处理:http://blog.stevensanderson.com/2011/10/05/full-height-app-layouts-a-css-trick-to-make-it-easier/
Steve Sanderson has a great treatment of height CSS issues here: http://blog.stevensanderson.com/2011/10/05/full-height-app-layouts-a-css-trick-to-make-it-easier/
是的。失去 min-height 属性。
Yea. Lose the min-height property.