Footer always on the boottom without display:fixed

给你一囗甜甜゛ 提交于 2019-12-13 05:52:54

问题


I want to make my footer display always on the bottom of the page even when the content does not fill the page, but when the content fills the page i want it to display only when i scroll to the very bottom of the page. I was thinking to check if the page can be scrolled with JavaScript and if no I'll add a class to make footer fixed else if yes ill remove that class, but I don't know how to check that with JavaScript. The idea is, I want my footer fixed when I cant scroll ,and when I can scroll I don't want it fixed. How can I do that ? My footer HTML is :

<footer class="smallFooter">
            <p> @EDUARDVALENTIN 2015 </p>
            <a href="https://www.facebook.com/danadesignsartoria?fref=ufi"><img src="img/fb-logo.png" /></a>
            <a href="#"><img src="img/instagram-logo.png" /></a>
            <a href="https://www.youtube.com/channel/UCqe4oWvPuSP8kTL70V1P9Gg/feed"><img src="img/yt-logo.png" /></a>
            <a href="https://twitter.com/SartoriaAsti"><img src="img/twitter-logo.png" /></a>
        </footer>

And CSS:

.smallFooter{   
    bottom:0;
    width:100%;  
    position:fixed;
    height:35px;
    background-color:#0E0E0E ;

}



.smallFooter p{
    position:absolute;
    display: inline-block;
    box-sizing:border-box;
    color:white;
    font-size:10px;
    float:left;

}


footer img{
    width:25x;
    height:25px;
    display:inline-block;
    float:right;
    margin-right:3%;
    padding-top:8px;

}

回答1:


Just set min-height in your "containter" element above your footer.

min-height: 100%

Set the position of your footer as absolute.

Here is an example: http://matthewjamestaylor.com/blog/keeping-footers-at-the-bottom-of-the-page




回答2:


The solution I've used before is this sticky footer

The footer height needs to be known, and the most important pieces are the body height and wrapper with the min-height and negative margin-bottom.




回答3:


Get document height and other content height like in code

//$(".site_wrapper").height() -> Content height which is your center warraper
//$(".header_wrapper").height() ->  header wrapper height
//$(".header_wrapper").height() -> footer warpper height
//$(".site_wrapper").height() -> your complete page's wrraper
$(document).ready(function(){
if($(document).height() > $(".site_wrapper").height()){
            var dHeight = $(document).height();
            var fHeight = $(".footer_warpper").height();
            var hHeight = $(".header_wrapper").height();
            var height = dHeight -  (fHeight + hHeight) - 50; //adjust 50 +/- according to your page
            $(".main_table").css("height",height);
        }
});



回答4:


For a CSS-only solution that doesn't depend on fixed height, use display: table.

CSS

html, body {
    height: 100%;
}
body {
    display: table;
    width: 100%;
}
.content {
    display: table-row;
    height: 100%;
}
.smallFooter {
    display: table-row;
    height: 1px;
}

HTML

<div class="content">
    <p>Main content goes here.</p>
</div>
<footer class="smallFooter">
    <p>Footer content goes here</p>
</footer>

See it in action with this fiddle




回答5:


**HTML**
<div class="mydiv">
  Suspendisse potenti.
</div>

<footer class="smallFooter">
        <p> @EDUARDVALENTIN 2015 </p>
        <a href="https://www.facebook.com/danadesignsartoria?fref=ufi"><img src="img/fb-logo.png" /></a>
        <a href="#"><img src="img/instagram-logo.png" /></a>
        <a href="https://www.youtube.com/channel/UCqe4oWvPuSP8kTL70V1P9Gg/feed"><img src="img/yt-logo.png" /></a>
        <a href="https://twitter.com/SartoriaAsti"><img src="img/twitter-logo.png" /></a>
    </footer>

CSS

html,body{
height:100%;
padding:0;
margin:0;
}
.mydiv{
    background : red;
}
.smallFooter{   
 width:100%;  
height:35px;
background-color:#0E0E0E ;
position:relative;
}



 .smallFooter p{
position:absolute;
display: inline-block;
box-sizing:border-box;
color:white;
font-size:10px;
float:left;

}


footer img{
width:25x;
height:25px;
display:inline-block;
float:right;
margin-right:3%;
padding-top:8px;

}

JS

$(".mydiv").css({"min-height":($("body").outerHeight()-$(".smallFooter").outerHeight())});

You can try something like this as well using a little bit of Jquery. Does this help? Have the body take full height and the remaining contents take what is left.

http://jsfiddle.net/53hf73nL/1/ when there is a lot of content http://jsfiddle.net/53hf73nL/2/ when there is not much content.



来源:https://stackoverflow.com/questions/32124710/footer-always-on-the-boottom-without-displayfixed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!