7

我目前正在开发一个 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,我尝试添加 height: 100%; 属性,但随后感觉表单容器超过了屏幕大小。即出现一个垂直滚动条,它与标题的高度大致相同。如何在没有滚动条的情况下让 formContainer 填充可用的屏幕空间。但是,如果内容大于表单容器,则应展开以填充额外的空间。

感谢您的任何帮助,您可以提供。

4

2 回答 2

7

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

使用旧的 flexbox 模型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;
}​

同样的事情,但这次使用新的 flexbox 模型 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;
}​

只有黄色段落的版本

于 2012-10-18T21:34:14.137 回答
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% 高度,但您的文章 > 部分元素将显示正确的高度并正确放置它的子元素。

于 2012-10-18T21:45:25.093 回答