使中央div填充剩余的可用空间

时间:2012-10-18 21:26:36

标签: css html5 css3 html

我目前正在开发一个HTML5和CSS项目,并且在让容器正确显示方面遇到了问题。

我想要的是顶部的标题栏,包含2个其他div的包装,然后是底部的页脚,它始终位于窗口底部或内容的底部,以较低者为准。

这是一个片段:

html, body
{
	padding: 0;
	margin: 0;
}

#wrapper
{
	position: absolute;
	background-color: purple;
	height: 100%;
	width: 100%;
	margin-top: 0px;
}

header
{
	position: absolute;
	width: 100%;
	height: 80px;
	background-color: black;
	color: white;
}


#articleContainer
{
	background-color: blue;
	width: 75%;
	margin-left: auto;
	margin-right: auto;
	height: auto;
	margin-top: 80px;
}

#articleContent
{
	width: 70%;
	background-color: yellow;
	float: left;
}

#articleSideBar
{
	position: relative;
	width: 28%;
	background-color: green;
	float: right;
	margin-left: 2px;
	margin-right: 2px;
	display: inline;
	margin-top: 0px;
	float: right;
	height: auto;
}
<html>
	<head>
		<title>index</title>
		<link href="ArticleStyleSheet.css" rel="stylesheet" type="text/css" />
	</head>
	<body>
		<div id="wrapper">
			<header>
				Header
			</header>
			<div id="articleContainer">
				Article Container
				
				<div id="articleContent">
					The quick brown fox jumped over the lazy dogs back. All good men must come to the aid of the party
				</div>
				
				<div id="articleSidebar">
					Article Sidebar
				</div>
			</div>
		</div>
	</body>
</html>

目前,articleContainer只有很多行的高度。我想要的是formContainer来填充屏幕的其余部分,我已经尝试添加高度:100%;属性,但然后这感觉形式容器超过屏幕大小。即出现一个垂直滚动条,其高度与标题大致相同。如何在没有滚动条的情况下让formContainer填充可用的屏幕空间。但是,如果内容大于表单容器应扩展以填充额外空间。

感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:6)

如果您真的想要一个css3解决方案,那么您正在height: calc(100% - 80px);设置#articleContainer,如 this fiddle 所示,但这不会适用于所有浏览器。

使用old flexbox model css:

的示例
html, body
{
    padding: 0;
    margin: 0;
    height: 100%;
}

#wrapper
{
    display: -webkit-box;
    -webkit-box-orient: vertical;
    min-height: 100%;
    background-color: purple;
    width: 100%;
    margin-top: 0px;
}

header
{
    width: 100%;
    height: 80px;
    background-color: black;
    color: white;
}

#articleContainer {
    width: 75%;
    margin: auto;
    background-color: blue;
    display: -webkit-box;
    -webkit-box-flex: 1;
}

#articleContent
{
    width: 70%;
    background-color: yellow;
}

#articleSideBar
{
    position: relative;
    width: 28%;
    background-color: green;
    margin-left: 2px;
    margin-right: 2px;
    display: inline;
    margin-top: 0px;
    height: auto;
}​

同样的事情,但这一次使用new flexbox model css

html, body
{
    padding: 0;
    margin: 0;
    height: 100%;
}

#wrapper
{
    display: -webkit-flex;
    -webkit-flex-direction: column;
    min-height: 100%;
    background-color: purple;
    width: 100%;
    margin-top: 0px;
}

header
{
    width: 100%;
    height: 80px;
    background-color: black;
    color: white;
}

#articleContainer {
    width: 75%;
    margin: auto;
    background-color: blue;
    display: -webkit-flex;
    -webkit-flex: 1;
}

#articleContent
{
    width: 70%;
    background-color: yellow;
}

#articleSideBar
{
    position: relative;
    width: 28%;
    background-color: green;
    margin-left: 2px;
    margin-right: 2px;
    display: inline;
    margin-top: 0px;
    height: auto;
}​

version with only the paragraph in yellow

答案 1 :(得分:1)

之前我使用过这种方法,棘手的部分是将页眉和页脚放在正确的位置。一旦你有了,其余部分应该落实到位:

的jsfiddle:

http://jsfiddle.net/Ug5JR/

的CSS:

html, body { margin: 0; padding: 0; height: 100%; }

header {
  position: relative;
  display: block;
  background: red;
  height: 100px;
  z-index: 1;
}

article {
  display: block;
  background: yellow;
  min-height: 100%;
  margin-top: -100px;
  margin-bottom: -100px;
}

article section {
  display: block;
  padding-top: 100px;
  padding-bottom: 100px;
  overflow: hidden;
}

footer{
  display: block;
  background: blue;
  height: 100px;
}

p:hover {
  height: 4000px;
}

标记:

<header></header>
<article>
  <section>
     <p>Hover me and I'll push the content larger than the page</p>
  </section>
</article>
<footer></footer>

诀窍是获得负边距以吸收页眉和页脚使用的空间,这会导致100%计算自我纠正。然后,您可以使用任何内部元素来抵消负边距,并在顶部和底部填充边距或边距。因此,虽然您的文章元素几乎是页面的100%高度,但您的文章&gt; section元素将显示正确的高度并正确地将它们放在外面。

相关问题