自己写了一段css代码,但是后有一大段空白,请问怎么解决?

我希望 container的div上面留20px给 header 下面留20px给 footer ,然后页面剩下的部分全部是container

右侧留20px给right,剩下的部分都是container,不要留空白

现在我虽然设置container的高度为100% ,但是还是会留下很大的空白,请问是什么原因造成这样的

怎么修改才能让空白消失。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>

<style>
div {
    box-sizing: border-box;
}

.header {
    height: 20px;
    background: rgba(51,255,255,1);
}

.container {
    position: relative;
    padding-top: 20px;
    padding-bottom: 20px;
    padding-left: 50px;
    background: rgba(51,0,153,1);
    height: 100%;
}

.right {
    position: absolute;
    right: 0;
    top: 0;
    width: 50px;
    background:rgba(204,0,51,1);
    height: 100%
}

.footer {
    height: 20px;
    background: rgba(153,0,0,1);
}

</style>

</head>

<body>

<div>

    <div class='header'></div>

    <div class='container'>
        <div class='list'>
            
        </div>
        
        <div class='right'>
            <ul>
                <li>111</li>
                <li>222</li>
                <li>333</li>
            </ul>
        </div>
    
    </div>
    
    <div class="footer"></div>

</div>

</body>
</html>

图片描述

修改后的代码,还是会出现大片的空白

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>

<style>

html, body, div, p, ul, li, ol, dl, dt, dd, pre, code, table, tr, td, form, fieldset,
legend, button, input, textarea, h1, h2, h3, h4, h5, h6, hr, blockquote {
    margin: 0;
    padding: 0;
}


div {
    box-sizing: border-box;
}


html {
    height: 100%;
}

.header {
    height: 20px;
    background: rgba(51,255,255,1);
}

.container {
    position: relative;
    padding-top: 20px;
    padding-bottom: 20px;
    padding-left: 50px;
    background: rgba(51,0,153,1);
    height: 100%;
}

.container ul {
    list-style: none;
}

.right {
    position: absolute;
    right: 0;
    top: 0;
    width: 50px;
    background:rgba(204,0,51,1);
    height: 100%
}

.footer {
    height: 20px;
    background: rgba(153,0,0,1);
}

</style>

</head>

<body>

<div>

    <div class='header'></div>

    <div class='container'>
        <div class='list'>
            
        </div>
        
        <div class='right'>
            <ul>
                <li>111</li>
                <li>222</li>
                <li>333</li>
            </ul>
        </div>
    
    </div>
    
    <div class="footer"></div>

</div>

</body>
</html>
阅读 3.3k
3 个回答

height: 100% 是相对于父元素的,若父元素未设置高度,那么height: 100% 的实际高度为零。此处 container 的实际高度等于 0 + 40px (padding上下产生的高度)。

解决办法:可以用 100vh 代替 100%

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>

<style>
* {
    padding: 0;
    margin: 0;
}
ul,li{
    list-style: none;
}
div {
    box-sizing: border-box;
}

.header {
    height: 20px;
    background: rgba(51,255,255,1);
}
.container {
    position: relative;
    padding-top: 20px;
    padding-bottom: 20px;
    padding-left: 50px;
    background: rgba(51,0,153,1);
    height: calc(100vh - 40px);
}

.right {
    position: absolute;
    right: 0;
    top: 0;
    width: 50px;
    background:rgba(204,0,51,1);
    height: 100%
}

.footer {
    height: 20px;
    background: rgba(153,0,0,1);
}

</style>

</head>

<body>

<div>

    <div class='header'></div>

    <div class='container'>
        <div class='list'>
            
        </div>
        
        <div class='right'>
            <ul>
                <li>111</li>
                <li>222</li>
                <li>333</li>
            </ul>
        </div>
    
    </div>
    
    <div class="footer"></div>

</div>

</body>
</html>

1.正常来讲是不会这样的,重新刷新看下。或者选择元素看下是否有别的影响了
2.container的高度不应该这么设置,会出现滚动条的。height: calc(100% - 40px); -- 减去header、footer的高度

原因:
1、height:100%是相对于父元素的高度;
2、.container的祖先元素均未设置高度;
3、默认情况下html高度并不等于窗口高度。

解决方法:
1、给.container的所有祖先元素加上height:100%(从html元素开始的所有祖先元素);
2、考虑兼容性的前提下,使用vh单位。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题