javascript - 固定 div 下的页脚

标签 javascript html css

我目前很难让网站的页脚正常工作。我认为这是因为我固定定位的标题和容器 div,但我需要将它们固定在滚动时保持在顶部。当容器 div 为空时,我不确定如何将页脚显示在底部。

HTML:-

 <body>
        <div id="wrapper">
        <div id="header">
            <div id="headerContent">
            </div>
        </div>
        <script>
            $(document).ready(function() {
                  $('#container').css('marginTop', $('#header').outerHeight(true) + $('#navbar').outerHeight(true) );
            });
        </script>  
                <div id="navbar">
                    <div id ="navbarContent">  

                </div>
        </div>
        <div id="container">
        </div>
        <div id="footer">
        <div id="footerContent">
        </div>
        </div>   
 </div>
    </body>
</html>

CSS:-

#container{
    width:960px;
    margin:auto;
    overflow:hidden;
}

#wrapper{
    min-height:100%;
    position:relative;
}


#navbar{
    width:100%;
    height:40px;
    margin:auto;
    background-color:#4e8885;
    position:fixed;
    top:120px;
    padding:0px;
}

#header{
    width:100%;
    height:120px;
    margin:auto;
    background-color:#8bbcba;
    position:fixed;
    top:0px;
}

#footer{
    width:100%;
    min-height:20px;
    margin:auto;
    background-color:#8bbcba;
    position:absolute;
    bottom:0;
    left:0;
}

最佳答案

在您使用固定高度的示例中,没有必要动态计算边距。您应该能够设置 container 的边距以分别匹配 margin-topmargin-bottom 的页眉和页脚的总数.看到这个 jsFiddle:http://jsfiddle.net/9Jdrr/

如果需要动态计算的话,按照这个安排应该就可以了。

关于javascript - 固定 div 下的页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15745322/

相关文章:

javascript - 有没有办法从音频文件中获取诸如分贝级别之类的信息并将该信息转换为 json 数组?

javascript - 删除元素 javascript jquery

html - css float 在顶部获得双边距(不是来自 body )

html - 在 FireFox 中对齐链接和提交按钮

jquery - 每 15 秒复制一次 div 并为其添加动画

javascript - iframe没有定义请求全屏的方法

javascript - React .map 使用 Fetch API 返回未定义

html - 在 Sinatra 表单中格式化日期字段

css - ul li wide than browser window 强制到新行进行分页

css - 如何创建网格/平铺 View ?