CSS Sticky Footers with Unknown Height

CSS Sticky Footers with Unknown Height

http://pixelsvsbytes.com/blog/2011/09/sticky-css-footers-the-flexible-way/

Summary:

For a site with a header, content area, and footer:

  1. Set html, body {height: 100%;}

  2. Set your body (or a wrapper div) to display: table; width: 100%; height: 100%;

  3. Set your header, footer, and content area to display: table-row;. Give your header and footer height: 1px;, and give your content area height: auto;

    The header and footer will both expand to fit their content. The content area will expand to fit the larger of its content, or the available space.

https://jsfiddle.net/0cx30dqf/

How to avoid an unknown height sticky footer to overlap content with CSS only?

Here is the flex solution. Browser support: IE 10+

JsFiddle Demo

html {    height: 100%;}body {    margin: 0;    min-height: 100%;    display: -ms-flexbox;    display: -webkit-flex;    display: flex;    -ms-flex-direction: column;    -webkit-flex-direction: column;    flex-direction: column;}main {    -ms-flex: 1;    -webkit-flex: 1;    flex: 1;}footer {    background: silver;    /* height: 200px; */}
<main>main</main><footer>footer</footer>

Sticky Footer (with unknown Height) with pur CSS3 using HTML5 for all browsers

I have made an example for you using jQuery http://jsfiddle.net/ed4xe1z9/

HTML

   <div id="wrap">
<header>
<p>The header</p>
</header>
<div id="main">
<div class="the-margin">
<div id="first">
<h1>First Content</h1>

</div>
<h2>Main Content</h2>

</div>
</div>
</div>
<footer>
<p>Sticky footer</p>
</footer>

CSS

        * {
margin: 0;
}
html, body {
height: 100%;
}
#wrap {
min-height: 100%;
}
body {
background: #000;
font-family: Arial;
color: #fff;
}
#main {
overflow:auto;
}
.the-margin{
margin: 50px;
}
footer {
position: relative;
clear:both;
}

jQuery on document ready

$('#main').css('padding-bottom', $('footer').height());
$('footer').css('margin-top', -$('footer').height());

Sticky footer in scroll-y-container depending on height of content

You need to create a flex container. (Though there are other ways to hande this problem as well: https://css-tricks.com/couple-takes-sticky-footer/)

For the container, set the display to flex and flex-direction to column and give the scrollable content a flex value of 1. Remove positioning from footer, and there you have it.

This will cause the content to stretch to fill the height of the container if any is available, and it will cause the footer to be stuck to the bottom of the content.

For implementation: Be sure to follow up on all the cross-browser issues with flexbox, such as prefixes and bugs. https://github.com/philipwalton/flexbugs

.wrapper{  position: relative;  height: 205px;  width: 200px;}.scroll{  border: 1px solid red;  overflow-y: scroll;  height: 100%;  width: 100%;   display:flex;  flex-direction: column;}.content{  background-color: #ccc;  flex:1;
}.footer{ background-color: #efefef;
}
<h1> little content</h1>
<div class="wrapper"> <div class="scroll"> <div class="content"> Lorem ipsum dolor sit amet </div> <div class="footer"> This must stick to the bottom until .content is too long, then go below it </div> </div></div>

<h1> large content</h1>
<div class="wrapper"> <div class="scroll"> <div class="content"> 1. Lorem ipsum dolor sit<br> 2. Lorem ipsum dolor sit<br> 3. Lorem ipsum dolor sit<br> 4. Lorem ipsum dolor sit<br> 5. Lorem ipsum dolor sit<br> 6. Lorem ipsum dolor sit<br> 7. Lorem ipsum dolor sit<br> 8. Lorem ipsum dolor sit<br> 9. Lorem ipsum dolor sit<br> 10. Lorem ipsum dolor sit<br> 11. Lorem ipsum dolor sit<br> 12. Lorem ipsum dolor sit<br> 13. Lorem ipsum dolor sit<br> </div> <div class="footer"> This must stick to the bottom until .content is too long, then go below it </div> </div></div>
<h1> large content with large footer</h1>
<div class="wrapper"> <div class="scroll"> <div class="content"> 1. Lorem ipsum dolor sit<br> 2. Lorem ipsum dolor sit<br> 3. Lorem ipsum dolor sit<br> 4. Lorem ipsum dolor sit<br> 5. Lorem ipsum dolor sit<br> 6. Lorem ipsum dolor sit<br> 7. Lorem ipsum dolor sit<br> 8. Lorem ipsum dolor sit<br> 9. Lorem ipsum dolor sit<br> 10. Lorem ipsum dolor sit<br> 11. Lorem ipsum dolor sit<br> 12. Lorem ipsum dolor sit<br> 13. Lorem ipsum dolor sit<br> </div> <div class="footer"> This must stick to the bottom until .content is too long, then go further down<br> Some additional content </div> </div></div>

Full height content block width sticky footer

You are so close ... just need to change the value from height , what you need is to set the min-height :

.container {
min-height: 100%;

Updated Fiddle


Bonus:

To keep the content all visible you can use padding on the container = to the height of your footer and header:

    .container {
min-height: 100%;
background:red;
width:1280px;
margin:0 auto;
position: relative;

/*Use box-sizing to include the values of the padding on the 100% min-height*/
box-sizing:border-box;
/*Padding for bottom and top = to the height of your elements footer-header*/
padding: 135px 0;
}

2nd Demo Fiddle

css - relative header and sticky footer, dynamic content, unknown height, all in a container with responsive width?

jsBin demo

HTML:

<div id="view">
<div id="appear"> Content that appears </div>
<div id="header"> Header </div>
<div id="content"> <h1>Content</h1> <p>Lorem ipsum...</p> </div>
<div id="footer"> Footer </div>
</div>

CSS3:

using calc() which currently has a very good xBrowser support: http://caniuse.com/#search=calc

#view {
overflow: hidden; /* NO SCROLLBARS */
margin: 0 auto;
background-color: #000;
color:#fff;

width: 100vw;
max-width: 350px;
height: 100vh;
}
#appear{
display:none;
}
#header,
#footer {
height: 44px; /* note this */
background-color: #555;
}
#content {
overflow-y: scroll; /* SCROLLBARS !!!*/
height: calc(100% - 88px); /* 44+44 = 88px */
}

and finally

jQuery:

$("#view").on("click", "#header", function () {
var $appear = $('#appear');
var io = this.io ^= 1; // Toggler

$appear.show(); // Temporarily show
var animH = $appear.height(); // Get height and
if(io) $appear.hide(); // fast hide.
$appear.slideToggle(); // Now do it with animation

$('#content').animate({ // Animate content height
height: (io?"-=":"+=")+animH
},{
step: function() {
$(this).css("overflow-y", "scroll");
},
complete : function(){
var h = 88 + (io?animH:0); // header+footer = 88px
$(this).css({height: "calc(100% - "+ h +"px)"});
}
});
});

To explain the beauty above:

the trick is to set a fixed height to header and to Footer,
than using calc(100% - 88px) for the scrollable content you get the needed space.

On jQuery animate you just quickly get the Appearing Top content height and apply it to the calc() magic in jQuery.

position: sticky' not working when 'height' is defined

The issue here is with height, but not the height you thought about. Let's first start by the definition of the sticky position:

A stickily positioned element is an element whose computed position
value is sticky. It's treated as relatively positioned until its
containing block crosses a specified threshold (such as setting top to
value other than auto) within its flow root (or the container it
scrolls within), at which point it is treated as "stuck" until meeting
the opposite edge of its containing block.

The important part here is the last sentence which explain that the position sticky will end when the element reach the edge of its containing block and in your case the containing block of the sticky element is the body and you set the body to be height:100% and you are having an overflow of content.

So when setting the height of main to be 92% and the footer to be 8%, you already made the footer at the oppsite edge of its containing block. Here is an illustration where I added a background color to the body so you can clearly see this:

html,body {  height: 100%;  margin: 0;}html {  background:white;}body {  background:blue;}
#main { height: 92%;}#landing { display: flex; align-items: center; justify-content: center; height: 100%; text-align: center;}#landingContent { width: 20vw;}#footerNav { height: 8%; display: flex; align-items: center; position: sticky; top: 0px; background:red; color:#fff;}
<div id="main">    <div id="landing">        <div id="landingContent">            <h1 class="logo">Logo</h1>            <p id="landingParagraph">Lorem ipsum, paragraph content, etc etc.</p>            <button>Button</button>        </div>    </div></div><div id="footerNav">    <div id="footerNavContent">        <h1 class="logo">Logo</h1>    </div></div><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p><p>Hello</p>

Sticky footer and content with 100% height

You can set .my_content to 100% of the viewport height minus the height and (vertical) padding of the other elements (i.e. header height, footer height, top and bottom padding on .content-container) on your page like so:

.my_content {
min-height: calc(100vh - 140px);
}

DEMO

If your header and footer have variable heights, this won't work though.



Related Topics



Leave a reply



Submit