【问题标题】:simple css height简单的CSS高度
【发布时间】:2011-02-17 06:36:10
【问题描述】:

尝试使网站中的元素表现如下时卡住了:

<div class="for100procHeight">
  <div class="header">fixed top</div>
  <div class="content">long text with scrolling content</div>
  <div class="footer">fixed bottom</div>
</div>

快速提醒css高度:

  • 百分比计算如下 相对于高度 生成框的包含块。
  • 如果它具有块级子级,则 高度是两者之间的距离 顶部的顶部边界边缘 块级子框<...>
  • 但是,如果元素 具有非零顶部填充和/或顶部 边框,或者是根元素,那么 内容从上边距开始 最上面的孩子的边缘<...>

乐观的解决方案是:

.for100procHeight { height: 100%; }
.header, .footer { height: 20px; }
.content { height:100%; overflow:scroll; }
/* failed badly: footer didn't fit into the window (heightwise) */

随着乐观情绪的减弱,又进行了一次尝试:

<div class="for100procHeight">
  <div class="header">fixed top</div>
  <div class="footer">fixed bottom</div>
  <div class="wrapper">
    <div class="content">long text with scrolling content</div>
  </div>
</div>

.for100procHeight { height: 100%; }
.header, .footer { position: absolute; height: 20px; }
.content { height:100%; overflow:scroll; }
.header { top: 0px; left: 0px; }
.footer { bottom: 0px; left: 0px; }
.wrapper { padding-top: 20px; padding-bottom: 20px; }
/* failed badly: scroll down arrow was behind footer */
/* failed badly: scroll down arrow didn't fit into the window (heightwise) */

除了高度是浏览器窗口的 100%(导致元素底部位于可见区域之外)之外,这看起来几乎应有尽有。 Margins/Paddings/Wrappers/etc 不会改变这一点。

我查看了Complex height in CSS,但是客户端/元素高度上的 JS 支持看起来更加迷人。

编辑:

从 complex-height-in-css 复制粘贴并带有扩展名

---------------------------------------
| fixed top |   | fixed top |        |
|-----------|   |-----------|        |
|  long   |^|   |  long   |^|        |
|  text   |_|   |  text   |_|        |
|  with   |_|   |  with   |_|        |
|scrolling| |   |scrolling| |        |
| content | |   | content | |        |
|-----------|   |-----------|        |
| fixed bott|   | fixed bott|        |
--------------------------------------

我意识到没有提到其中两个,但也没有将 &lt;div class="for100procHeight"&gt; 作为整个页面内容。

解决方案:

<html>
<head>
    <style>
    html, body {
        height: 100%;
        margin: 0px;
        padding: 0px;
        overflow: hidden;
    }
    .page {
        position: absolute;
        height: 100%;
        width: 100%;
    }
    .content {
        width: 100%;
        height: 100%;
        overflow: scroll;
        overflow-x: hidden;
    }
    .content .inner {
        padding-top: 30px;
        padding-bottom: 30px;
    }
    .top {
        width: 100%;
        margin-left: -17px;
        position: absolute;
        top: 0px;
        left: 0px;
        height: 30px;
        background: url('background.gif');
    }
    .bottom {
        width: 100%;
        margin-left: -17px;
        position: absolute;
        left: 0px;  
        bottom: 0px;
        height: 30px;
        background: url('background.gif');
    }
    .top span, .bottom span {
        padding-left: 17px;
        z-index: 2000;
    }
    .top span, .bottom span {
        display: block;
        width: 100%;
    }

    #list2.page {
        margin-left: 110%;
    }
    </style>
</head>
<body>
    <div id="list1" class="page">
        <div class="top"><span>top</span></div>
        <div class="bottom"><span>bottom</span></div>
        <div id="listContent" class="content">
            <a id="page1" name="page1" style="width:1px;"></a>
            <div class="inner">
            ...
            </div>
        </div>
    </div>
    <div id="list2" class="page">
        <div class="top"><span>top</span></div>
        <div class="bottom"><span>bottom</span></div>
        <div id="listContent" class="content">
            <a id="page2" name="page2" style="width:1px;"></a>
            <div class="inner">
            ...
            </div>
        </div>
    </div>
</body>
</html>

【问题讨论】:

    标签: html css cross-browser


    【解决方案1】:

    如果我误解了您要查找的内容,请告诉我,但是这样的内容如何:

    CSS

    * {
        margin: 0px;
        padding: 0px;
    }
    html, body {
        height: 100%;
    }
    #wrapper {
        position: relative;
        min-height: 100%;   
    }
    #header {
        border: 1px solid #e3e3e3;
        position: relative;
        top: 0px;
        left: 0px;
    }
    #footer {
        border: 1px solid #e3e3e3;
        position: relative;
        margin-top: -20px; /* negative value of footer height */
        height: 20px;
        clear:both;
    }
    #content {
        overflow: auto;
        padding-bottom: 20px;
        padding-left: 20px;
    }
    

    要使这项工作适用于 IE,您需要在 &lt;head&gt; 部分中添加一个条件来将此样式提供给 IE 6 及更低版本以及 IE 8 及更高版本。

    <!--[if !IE 7]>
        <style type="text/css">
            #wrapper {display:table;height:100%}
        </style>
    <![endif]-->
    

    ...和一个 Opera 修复:

    body:before {
       content:"";
       height:100%;
       float:left;
       width:0;
       margin-top:-32767px;
    }
    

    HTML

    <div id="wrapper">
        <div id="header">header</div>
        <div id="content">
             <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
        </div>
    </div>
    <div id="footer">footer</div>
    

    ...check out the demo on jsFiddlea demo with scrolling content。请注意,我的第一个示例中有滚动条的原因是 jsFiddle 在 iF​​rame 的主体上放置了一个 padding: 10px;,其中包含我无法覆盖的演示。

    这里有一些关于“粘性页脚”和其他好东西的参考资料:

    如果我误解了您要查找的内容,那么也许这是一个更好的例子?

    ...以及相关的教程

    我希望这会有所帮助。如果您有任何问题,请告诉我。

    【讨论】:

    • 他想要 100% 的内容高度。
    • 随着内容的增加,高度也会增加。
    • 此解决方案并非在所有浏览器中都显示相同。检查 IE。页脚处还有 20px 的间隙。您不必引入 -20 边距来补偿页脚高度。页脚的高度可能会发生变化,这不是一个灵活的解决方案。
    • 嗯..它应该工作。我按照cssstickyfooter.com 的示例进行操作。无论如何,我的 Mac 上没有 IE,所以我还不能测试它……我相信你。
    • 哦...我错过了有关 IE 的信息。我需要包括一个条件。查看我的修订版。
    【解决方案2】:

    请参阅下面的代码并在 http://jsfiddle.net/SyHd9/4/ 上查看工作解决方案

    跨浏览器兼容

    <div class="for100procHeight">
        <div class="header">fixed top</div>
        <div class="content">long text with scrolling content</div>       
    </div>
    <div class="footer">fixed bottom</div>
    
    html { height: 100%; }
    body { min-height: 100%; height: 100%; }
    
    .for100procHeight{
        position:relative;
        width:900px;
        height:100%;
        * html height:100%;
        background-color:#ccc;
    }
    .header{
        width:100%;
        height:100px;
        background-color:red;
        clear:both;
    }
    .content{
        width:100%;
        clear:both;
    }
    .footer{
        width:900px;
        height:100px;
        background-color:blue;
        clear:both;
    }
    p{
    padding:10px;
    }
    

    【讨论】:

    • 我认为您的元素嵌套不正确...看看我提供一堆内容时会发生什么:jsfiddle.net/SyHd9/3
    • 您引入了一个p 标签,您需要将它添加到您的样式中并给它一个填充。检查jsfiddle.net/SyHd9/4
    • 好的...但是在您刚刚给我的链接中,如果您向下滚动,您会看到一堆文本隐藏在页脚下方并一直向下...我是不是误会了什么?
    • 更多内容页脚不适合浏览器窗口,与乐观解决方案相同。关键是让页脚和页眉始终可见。并且只有 loremipsum 可滚动。这不是您希望网站至少具有浏览器高度的问题
    • 您在询问身高,这就是解决方案。如果您只需要滚动内容,请接受此答案并开始另一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多