高度100%无法正常工作

5
所以我有一个div应该有100%的高度。它唯一的父元素是<body>,所以100%应该是窗口的高度。但是它只像容器元素一样高而不是跨越整个屏幕的高度。有趣的是,如果我使用固定定位,它能做到我想要的效果。不幸的是,在站点布局中我不能使用fixed。以下是我的CSS。如果你想看看现在的网站是什么样子,请点击这个链接:http://ion.comli.com/projects/
body, ul { 
    margin: 0;
    padding: 0;
}

body {
    background: url('/images/background.png') no-repeat fixed;
}


/* CONTENT */

.content{
    margin-left: auto;
    margin-right: auto;
    position: absolute;
    top: 50px;
    left: 15%;
    height: 100%;
    width: 70%;
    background-color: #ffffff;
}

/* END CONTAINER*/

/* CONTAINER */

.container{
    background: #ffffff;
    margin: 5% 10%;
    text-align: center;
}

.container .title a {
    font-family: Franchise, "sans-serif";
    font-size: 48px;
    color: black;
    line-height: 48px;
    text-align: center;
    text-decoration: none;
}

.container .date {
    font-family: Ubuntu, "sans-serif";
    font-size: 12px;
    color: #666666;
    line-height: 12px;
    text-align: center;
    text-decoration: none;
}

.container .body {
    font-family: Ubuntu, "sans-serif";
    font-size: 16px;
    text-align: left;
}

/* END CONTAINER */

/* PROJECT */
.project {
    display: block;
    margin: 5% auto;
    height: 100px;
    width: 500px;
    border-radius: 10px;
    background: url("/images/background.png");
    opacity: 0.5;
}

.project h2 {
    font-family: Franchise;
    font-size: 48px;
    color: white;
    text-align: center;
}

/* END PROJECT */

/* NAVIGATION */
nav ul {
    background-color: #1b1b1b;
    display: table;
    list-style: none;
    position: fixed;
    top: 0;
    height: 50px;
    width: 100%;
    box-shadow: 0 0 6px #888888;
    z-index: 1;
}

nav ul li {
    float: left;
}

nav ul li a {
    display: table-cell;
    height: 50px;
    line-height: 50px;
    padding: 0 65px;
    font-family: Ubuntu;
    font-size: 16px;
    color: #ffffff;
    text-decoration: none;
    background-color: #292929;
}

nav #title {
    font-family: Lobster;
    font-size: 36px;
    line-height: 50px;
    border-right: 1px solid #ffffff;
    background-color: #1b1b1b;
}

nav #menu {
    padding: 0 25px;
    background-color: #1b1b1b;
}

nav #menu:hover {
    box-shadow: none;
    background-color: #292929;
}

nav li:hover #menu {
    box-shadow: none;
    background-color: #292929;
}

nav ul ul {
    background: #292929;
    display: none;
    position: absolute;
    top: 100%;
    right: 0px;
    width: 15%;
}

nav ul ul li {
    background: #292929;
    float: left;
    position: relative;
    clear: both; 
}

nav ul li:hover > ul {
    box-shadow: none;
    display: block;
}   

/* END NAVIGATION */

/* SCROLLBAR */

/* END SCROLLBAR */

有没有什么方法可以让这个div元素占据整个100%的宽度?我相信这个问题有一个简单的答案,但我找不到。提前感谢!

2个回答

12

您必须确保所有.content的父元素都定义了height属性。

所以在您的情况下,缺少的是:

html, body {
   height: 100%;
}

替代方案

或者您可以将.content定位为fixed,这样就可以实现相同的效果,但采用了不同的方法。

.content {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
}

3
让页面主体高度达到100%。
html, body {
    height: 100%;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接