【发布时间】:2013-01-02 13:15:34
【问题描述】:
我想创建一个 HTML 页面:
- 水平居中显示
- 整个窗口的高度都是白色背景
- 包含固定的标题和可滚动的内容
我有两个与 {width: 100%} 和 {height: 100%} 有关的问题。
我的页眉是页面宽度的 100%,而我希望它是其父宽度的 100%。 背景出现在窗口高度的 100% 处,但随后会随着内容向上滚动。
如果能帮助我理解 CSS 在这两种情况下如何处理 100% 值,我将不胜感激。我不是在寻求解决方法:我已经有了。我要求深入了解 CSS 的思维方式。
非常感谢,
詹姆斯
这是基本的 HTML:
<!DOCTYPE html>
<head>
<title>Width & Height 100%</title>
<style>
html {
height:100%;
}
body {
background: #666;
height: 100%;
margin: 0;
}
#container {
position: relative;
height:100%;
background: white;
width: 400px;
margin: 0 auto;
padding: 0 0;
}
#header {
position:fixed;
z-index:100;
background:#ffe;
/* width:760px; */
width:100%;
height:64px;
}
#content {
position: absolute;
left:20px;
width:360px;
height:360px;
margin:64px 0 0 0;
background:#efe;
}
</style>
</head>
<body>
<div id="container">
<div id="header">
Fixed header
</div>
<div id="content">
Scrollable content
</div>
</div>
</body>
</html>
【问题讨论】:
-
你的情况不是对width和height属性的理解有问题,你的问题是元素的定位问题