【问题标题】:Extend page content in height or show white background under content在高度上扩展页面内容或在内容下显示白色背景
【发布时间】:2012-10-24 16:40:33
【问题描述】:

我使用backstretch 作为背景图像,这样我就不会遇到分辨率问题。图像是一个纹理(它曾经有一个白色区域,应该是内容部分的背景颜色 - 在这里我没有遇到这样的问题,但其他问题)。现在中间的内容部分(包括页眉、页脚)应该有白色背景。

问题

如果内容不够高,我会在底部看到纹理,但它应该是白色的。这是页面底部的截图:

白色部分应垂直延伸。所以要么内容部分必须增加高度(根据窗口高度),要么我需要一个粘性页脚解决方案。我尝试了一些方法,但没有一个对我有用(例如来自 How do I force a DIV block to extend to the bottom of a page even if it has no content?sticky footer solution from Ryan Fait 的提示)。

这是我页面的精简版。这是没有 jQuery 部分的JSFiddle

HTML

<div id="header">
    <p>Introduction</p>
</div>
<div id="main-wrap">
    <div id="column1">
        <p>Menu</p>
    </div>
    <div id="column2">
        <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
    </div>
    <div class="clearer"></div>
</div>
<div id="footer">
    <p>Some Links</p>
</div>

CSS

body {
    background-color: black;
}

#header {
    background-color: white;
    width: 380px;
    margin: 0 auto;
}


#main-wrap {
    background-color: white;
    width: 380px;
    margin: 0 auto;
}


#column1{
    float: left;
    width: 60px;
}


#column2{
    float: left;
    padding-left: 20px;
    width: 300px;
}

#footer {
    background-color: white;
    width: 380px;
    margin: 0 auto;
}

.clearer{
    float: none;
    clear: both;
}

我能想象的唯一可行的方法是使用 Javascript 计算页面底部的间隔 div ...该解决方案应该适用于所有主要浏览器(IE7-IE9、FF、Chrome、Opera、Safari )。

当前的 JS 解决方案

<script language="javascript" type="text/javascript">
    <!--
    $(document).ready(function(){
        $.backstretch("img/background.jpg");

        var headerheight = $('#header').height();
        var column1height = $('#column1').height();
        var column2height = $('#column2').height();
        var columnheight = Math.max(column1height, column2height);
        var footerheight = $('#footer').height();
        var contentheight = headerheight + columnheight + footerheight;
        var innerheight = $(window).height();
        // calculate height of content to fill out full height of window
        var height = innerheight - headerheight - footerheight;
        // only if the the content has not enough height
        if (contentheight < innerheight) {
            $('#main-wrap').css('min-height', Math.max(height, columnheight));
        }
    });
    -->
</script>

CSS 解决方案会更好,但我没有找到任何解决方案。

【问题讨论】:

    标签: html css


    【解决方案1】:

    你可以让 jQuery 计算最小高度:

    $(function() {
      $("the_content").css("min-height", Math.max(window.innerHeight, parseInt($("the_content").css("height"))));
    })
    

    当然你可以证明 window.innerHeight 是合理的,例如如果您的容器有填充(被添加到宽度)

    【讨论】:

    • 很高兴听到我可以帮助你(:
    • 现在我发现了一个问题。在我的起始页上一切正常,但在内容较长的子页面上,我的column1 没有完全用白色背景填充。计算的高度(window.innerHeightwindow.height())太小。为什么window.innerHeight 在这里失败了?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多