【问题标题】:Footer div does not respect CSS height or min-height页脚 div 不尊重 CSS 高度或最小高度
【发布时间】:2013-04-03 12:38:25
【问题描述】:

好的,我之前问了一个问题:

How to make a div fill the remaning vertical space using css

得到了一个我现在一直在玩弄的答案:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Test</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <style type="text/css" media="screen">

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

            #header {
                float: top;
                width: 100%;
                height: 15%;
                background-color: green;
            }

            #navbar {
                float: left;
                width: 20%;
                height: 70%;
                background-color: red;
            }

            #content {
                float: right;
                width: 80%;
                height: 70%;
                background-color: blue;
            }
            #footer {
                float: bottom;
                width: 100%;
                height: 15%;
                background-color: yellow;
            }

        </style>
    </head>
    <body>
        <div id="header"> Header </div>
        <div id="navbar"> Nav Bar </div>
        <div id="content"> Body </div>
        <div id="footer"> Footer</div>
    </body>
</html>

现在 ultimatley Id 想要实现这一目标:

它覆盖了 100% 的屏幕,但我可以选择这些百分比的分布方式,即:

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

#header {
    float: top;
    width: 100%;
    height: 15%;
    background-color: green;
}

#navbar {
    float: left;
    width: 20%;
    height: 70%;
    background-color: red;
}

#content {
    float: right;
    width: 80%;
    height: 70%;
    background-color: blue;
}
#footer {
    float: bottom;
    width: 100%;
    height: 15%;
    background-color: yellow;
}

正如您所见,html 和 body 的高度为 100%,从而填满了屏幕。页眉的高度百分比为 15%,导航栏和正文的百分比为 70%,页脚为 15%,它们总共将构成可见屏幕的 100%...

现在一切似乎都很好,除了我的页脚:

#footer {
    float: bottom;
    width: 100%;
    height: 10%;
    background-color: yellow;
}

如果我删除height: 15%,那么我可以看到我的背景颜色为黄色:

如果我没有它的一些灰色。并且看起来占据了大约 20% 的屏幕:

那么基本上我将如何让我的 div 占据我分配给它们的正确高度百分比?

我希望我说得通。

提前致谢。

【问题讨论】:

  • 哇新元素浮动:底部;在您的代码中...
  • @RohitAzad haha​​ha 还在学习 我认为这是合乎逻辑的:$

标签: css html


【解决方案1】:

您不能浮动到顶部或底部。那是不存在的。所以你必须从你的页眉和页脚中删除它。

并通过以下方式清除页脚:

footer {
  clear: both;
}

【讨论】:

    【解决方案2】:

    你的问题是没有:

    float:top;
    

    或

    float:bottom;
    

    你需要做的是让他们都float:left;

    这里是更新后的代码的复制和粘贴:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Test</title>
            <meta http-equiv="content-type" content="text/html; charset=utf-8" />
            <style type="text/css" media="screen">
    
                html, body{
                    padding: 0;
                    margin: 0 auto;
                    height: 100%;
                }
    
                #header {
                    float: left;
                    width: 100%;
                    height: 15%;
                    background-color: green;
                }
    
                #navbar {
                    float: left;
                    width: 20%;
                    height: 70%;
                    background-color: red;
                }
    
                #content {
                    float: right;
                    width: 80%;
                    height: 70%;
                    background-color: blue;
                }
                #footer {
                    float: left;
                    width: 100%;
                    height: 15%;
                    background-color: yellow;
                }
    
            </style>
        </head>
        <body>
            <div id="header"> Header </div>
            <div id="navbar"> Nav Bar </div>
            <div id="content"> Body </div>
            <div id="footer"> Footer</div>
        </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2012-10-24
      • 1970-01-01
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多