【问题标题】:CSS Layout that uses 100% height使用 100% 高度的 CSS 布局
【发布时间】:2010-11-03 02:43:03
【问题描述】:

伙计们,我有一个我正在使用的 CSS 布局,它在左侧有一个页眉、页脚和一个侧边栏。它工作得很好,但唯一的问题是,如果没有足够的内容来填充主要内容,我希望侧边栏和页脚延伸到屏幕底部。我如何在 CSS 中做到这一点?我已经在此处发布了 css,因此您可以看到我正在使用的内容:

<style type="text/css">
    body
    {
        font: 100% Verdana, Arial, Helvetica, sans-serif;
        background: #666666;
        margin: 0;
        padding: 0;
        text-align: center;
        color: #000000;
    }
    .twoColHybLtHdr #container
    {
        width: 80%;
        background: #FFFFFF;
        margin: 0 auto;
        border: 1px solid #000000;
        text-align: left;
    }
    .twoColHybLtHdr #header
    {
        background: #DDDDDD;
        padding: 0 10px;
    }
    .twoColHybLtHdr #header h1
    {
        margin: 0;
        padding: 10px 0;
    }
    .twoColHybLtHdr #sidebar1
    {
        float: left;
        width: 8em;
        background: #EBEBEB;
        padding: 15px 0;
    }
    .twoColHybLtHdr #sidebar1 h3, .twoColHybLtHdr #sidebar1 p
    {
        margin-left: 10px;
        margin-right: 10px;
    }
    .twoColHybLtHdr #mainContent
    {
        margin: 0 20px 0 9em;
    }
    .twoColHybLtHdr #footer
    {
        padding: 0 10px;
        background: #DDDDDD;
    }
    .twoColHybLtHdr #footer p
    {
        margin: 0;
        padding: 10px 0;
    }
    .fltrt
    {
        float: right;
        margin-left: 8px;
    }
    .fltlft
    {
        float: left;
        margin-right: 8px;
    }
    .clearfloat
    {
        clear: both;
        height: 0;
        font-size: 1px;
        line-height: 0px;
    }
</style>

以及如何使用它的示例:

<div id="container">
    <div id="header" style="text-align: center"> Header goes here </div>
    <div id="sidebar1">Sidebar is here</div>
    <div id="mainContent">Main Content here</div>
    <br class="clearfloat" />
    <div id="footer">Footer Here</div>
</div>

【问题讨论】:

标签: html css layout


【解决方案1】:

结帐粘性页脚

http://ryanfait.com/sticky-footer/ http://www.cssstickyfooter.com/

<div id="wrap">

    <div id="main" class="clearfix">

    </div>

</div>

<div id="footer">

</div>

* {margin:0;padding:0;} 



html, body, #wrap {height: 100%;}

body > #wrap {height: auto; min-height: 100%;}

#main {padding-bottom: 150px;}  /* must be same height as the footer */

#footer {position: relative;
    margin-top: -150px; /* negative value of footer height */
    height: 150px;
    clear:both;} 

/* CLEAR FIX*/
.clearfix:after {content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;}
.clearfix {display: inline-block;}
/* Hides from IE-mac \*/
* html .clearfix { height: 1%;}
.clearfix {display: block;}
/* End hide from IE-mac */

有点重复CSS: fixed to bottom and centered

【讨论】:

    【解决方案2】:

    看看http://ryanfait.com/sticky-footer/ 您需要稍微调整您的代码,因为除了页脚之外的所有内容都需要在包装器 div 中。 乔什

    【讨论】:

      【解决方案3】:

      可以使用 100% 高度的 display:table 来完成,但自然 IE 也不支持。我经常使用带有类名 table、tr 和 td 的 div 进行表格布局,然后让 IE 用等效标记替换这些类。这样我就可以绕过整个桌子作为布局辩论。如果您不关心语义,您总是可以忽略纯粹主义者并使用真实表格执行此类操作。成为 CSS 纯粹主义者是一回事,但当市场份额最高的浏览器是采用 90 年代技术的过时产品时,这是一种昂贵的宗教。

      【讨论】:

        【解决方案4】:

        您应该查看min-height CSS property,但要小心,因为并非所有浏览器都正确支持它(尤其是 IE.. - 好像这对任何人来说都是新闻)

        您可能还想查看CSS min-height hacks on Google

        【讨论】:

          猜你喜欢
          • 2011-09-03
          • 2013-02-16
          • 2010-09-06
          • 1970-01-01
          • 1970-01-01
          • 2013-05-07
          • 2013-02-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多