html - 站点最小高度 100%

标签 html css

我正在构建一个网站,我希望它的最小高度为 100%,因此如果页面上没有太多内容,页脚将位于页面底部。 如果有更多内容,它会简单地展开。

我以一个网站为例,并根据我的需要进行了更改。
起初它似乎工作得很好,但现在它显示出两个问题:
- 网站的高度似乎总是超过 100%;一小部分超出了屏幕。
- 尽管已设置 Bottom 属性,但页脚并未显示在底部,而是显示在中间某处。

这是标记:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ Master Language="VB" CodeFile="Site.Master.vb" Inherits="Site" %>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head id="Head" runat="server">
        <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9">
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Site (b&egrave;ta)</title>
    </head>
    <body>
        <form id="form1" runat="server" autocomplete="off" class="formCss">
            <ajax:ToolkitScriptManager ID="Toolscriptmanager1" runat="server" EnableScriptGlobalization="true" EnableScriptLocalization="true">
                <Scripts>
                    <asp:ScriptReference Path="~/js/jquery-1.8.2.min.js" />
                    <asp:ScriptReference Path="~/js/jquery.curvycorners.packed.js" />
                    <asp:ScriptReference Path="~/js/Site.jquery.js" />
                    <asp:ScriptReference Path="~/js/jquery.colorize-1.3.1.js" />
                </Scripts>
            </ajax:ToolkitScriptManager>
            <asp:Label ID="ContentTitle" runat="server" CssClass="content_title"></asp:Label>
            <div id="container">
                <div id="headerContainer">
                    <div id="header">
                        <telerik:RadMenu ID="HoofdMenu" EnableEmbeddedSkins="false" Height="20px" EnableImageSprites="false" Font-Size="11px" runat="server" CollapseDelay="0" ExpandDelay="0" ClickToOpen="true" ExpandAnimation-Type="None" CollapseAnimation-Type="None" CausesValidation="false"></telerik:RadMenu>
                    </div>
                </div>

                <div id="content">
                    <asp:ContentPlaceHolder ID="Content" runat="server"></asp:ContentPlaceHolder>
                </div>
                <div id="footer">
                    This is the footer
                </div>
            </div>
       </form>
    </body>
</html>

这是 CSS:

html,body 
{
    margin:5px;
    padding:0;
    height:100%; /* needed for container min-height */  
    font-family:tahoma;
    font-size:11px;
    color:#000000;
    background-color: #8FB1B1;  
    /*background-image: url(../../Images/Afbeelding1.jpg);*/
}

.formCss
{
    height:100%;
    min-height: 100%;
}

div#container 
{
    position:relative; /* needed for footer positioning*/
    margin:0 auto; /* center, not in IE5 */
    width:100%;     
    height:auto !important; /* real browsers */
    height:100%; /* IE6: treaded as min-height*/
    min-height:100%; /* real browsers */

    background-color: #FFFFFF;  
}

div#headerContainer
{
    background-color: #8FB1B1;
}
div#header 
{
    padding-left:5px;
    padding-top: 12px;  
    height: 30px;   
    background-color: #1C2948;  /*#833D62;*/
    z-index: 100;
}


div#content 
{
    padding-left: 10px;
    padding-right: 10px;
    padding-top:10px;   
    background-color: #FFFFFF; /* #E0E5D7; #FFFFFF;*/
    padding-bottom:25px;  /* bottom padding for footer */

    /*filter:alpha(opacity=80); 
    -moz-opacity:0.80; 
    opacity:0.80;   */

}   

div#footer 
{
    position:absolute;
    height: 25px;   
    bottom:0; /* stick to bottom */
    background:#FFFFFF;     
    padding-left: 10px;
}   

我已将代码放入 Fiddle 中:http://jsfiddle.net/7uuD6/1
(不幸的是,它包含 JSFiddle 无法处理的 ASP.NET 代码)

我做错了什么? 干杯, 希杰

最佳答案

您可以使用两种方法来保持粘性页脚,但它们取决于您的浏览器支持情况。

如果您不关心 IE7 或 Firefox 17 之前的版本,您可以使用这种使用 box-model border-box 属性的方法。

Border-box Version

为此,您只需要两个元素。

HTML

<div class="page">    
</div> 
<div class="page-footer">
</div>

CSS

*, *:before, *:after {
    /* Fix the box model to include padding */
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    -ms-box-sizing: border-box;
    box-sizing: border-box;
}

html, body {
    height: 100%;
    margin: 0;
    position: relative;
}

.page {
    min-height: 100%;
    position: relative;
    margin-bottom: -150px;
    padding-bottom: 150px;
}

.page-footer {
    height: 150px;
    position: relative;
    z-index: 1;
}

如果您需要支持旧版浏览器,那么您将需要使用额外的 div 将页脚向下推。

Legacy Version

此版本所需的代码如下。

HTML

<div class="page">
    <div class="page-push">
        <!--
            This div just pushes the footer down so content does not overflow it
        -->
    </div>
</div> 
<div class="page-footer">
</div>

CSS

html, body {
    height: 100%;
    margin: 0;
    position: relative;
}

.page {
    min-height: 100%;
    position: relative;
    margin-bottom: -150px;
}

.page-push,
.page-footer{
    height:150px;
}

.page-footer {
    position: relative;
    z-index: 1;
}

关于html - 站点最小高度 100%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16539467/

相关文章:

html - 更改 NavBar 上的背景图像单击

css - 有什么方法可以仅使用 CSS 生成给定范围内的随机数吗?

jquery - 悬停在两个元素上将效果设置为一个元素

CSS 剪辑路径在 Safari 中加载失败

html - 如何在 Grails 中使用其真实路径加载图像

html - 具有外部变量的 HAML 中的元描述

javascript - 在 HTML 文档中查找单词并为该元素分配 ID/类

javascript - 为什么我的元素在页面加载和 javascript 执行时四处移动?

html - Css force div 填充它和页脚之间的空间

html - 如何在悬停时从名称中删除下划线