How Can a Variable-Height Sticky Footer Be Defined in Pure CSS

How can a variable-height sticky footer be defined in pure CSS?

All other solutions here are out of date and either use JavaScript, or table hacks.

With the advent of the CSS flex model, solving the variable-height sticky footer problem becomes very, very easy: while mostly known for laying out content in the horizontal direction, Flexbox actually works just as well for vertical layout problems. All you have to do is wrap the vertical sections in a flex container and choose which ones you want to expand. They'll automatically take up all the available space in their container.

Note how simple the markup and the CSS are. No table hacks or anything.

The flex model is supported by 96%+ of the browsers in use today.

html, body {  height: 100%;  margin: 0; padding: 0;  /* to avoid scrollbars */}
#wrapper { display: flex; /* use the flex model */ min-height: 100%; flex-direction: column; /* learn more: http://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/ */}
#header { background: yellow; height: 100px; /* can be variable as well */}
#body { flex: 1; border: 1px solid red;}
#footer{ background: lime;}
<div id="wrapper">  <div id="header">Title</div>  <div id="body">Body</div>  <div id="footer">    Footer<br/>    of<br/>    variable<br/>    height<br/>  </div></div>

Keep footer with variable height on bottom

2014 UPDATE: The modern way to solve this layout problem is to use the flexbox CSS model. It's supported by all major browsers and IE11+.


Here's a solution for sticky footer of flexible height using divs with display: table-row:

html, body {  height: 100%;  margin: 0;}.wrapper {  display: table;  height: 100%;  width: 100%;  background: yellow;}.content {  display: table-row;  /* height is dynamic, and will expand... */  height: 100%;  /* ...as content is added (won't scroll) */  background: turquoise;}.footer {  display: table-row;  background: lightgray;}
<div class="wrapper">  <div class="content">    <h2>Content</h2>  </div>  <div class="footer">    <h3>Sticky footer</h3>    <p>Footer of variable height</p>  </div></div>

Sticky footer with a variable height

if you use display:table as a base , then your sticky footer can be any size and will be pushed down if content grows.

http://dabblet.com/gist/5971212

html {
height:100%;
width:100%;
}
body {
height:100%;
width:100%;
display:table;
table-layout:fixed;
margin:0 auto;
width:80%;
}
.tr {
display:table-row;
background:turquoise
}
section.tr {
height:100%;
background:yellow
}

for

<header class="tr"> <h1>title</h1><p>make me grow</p></header>
<section class="tr"><article>article</article></section>
<footer class="tr"> <p>Footer</p><p>make me grow</p></footer>

Sticky header, sticky footer (variable height), fluid middle?

All other solutions here are out of date and either resort to JavaScript, or don't support a variable height footer.

With the advent of the CSS flex model, solving the variable-height sticky footer problem becomes very, very easy: while mostly known for laying out content in the horizontal direction, Flexbox actually works just as well for vertical layout problems. All you have to do is wrap the vertical sections in a flex container and choose which ones you want to expand. They'll automatically take up all the available space in their container.

Note how simple the markup and the CSS are. No table hacks or anything.

The flex model is supported by all major browsers as well as allegedly IE11+, though my IE doesn't render this snippet correctly yet.

html, body {  height: 100%;}
#headContainer { background: yellow; height: 100px; /* can be variable as well */}
#wrapper { display: flex; /* use the flex model */ min-height: 100%; flex-direction: column; /* learn more: http://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/ */}
#bodyContainer { flex: 1; border: 1px solid orange;}
#footContainer { background: lime;}
<div id="wrapper">  <div id="headContainer">Title</div>  <div id="bodyContainer">Stuff goes here</div>  <div id="footContainer">    Footer<br/>    of<br/>    variable<br/>    height<br/>  </div></div>

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 keep included footer at bottom of short page without min-height, sticky-footer or javascript

I know this post is pretty old, but I found a great resource for this exact thing.

http://ryanfait.com/sticky-footer/

here is just the css:

* {
margin: 0;
}
html, body {
height: 100%;
}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -155px; /* the bottom margin is the negative value of the footer's height */
}
.footer, .push {
height: 155px; /* .push must be the same height as .footer */
}

/*

Sticky Footer by Ryan Fait
http://ryanfait.com/

*/

Good luck.

How to make a sticky footer using CSS?

Following a clean method implemented from an online source no longer available (dead link), the minimum code you should need for your page would be (note - probably best to use #bottom-footer instead of footer #bottom-footer for the selection of your footer - that could be part of the issue):

html {
position: relative;
min-height: 100%;
}
body {
margin: 0 0 100px; /* bottom = footer height */
}
#bottom-footer {
position: absolute;
left: 0;
bottom: 0;
height: 100px;
width: 100%;
}

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>

How do I use CSS to position a fixed variable height header and a scrollable content box?

Assuming by "fixed" you mean position:fixed, I don't think it's possible in pure CSS, as position:fixed takes the element out of the document flow.

However, it should just take a line or two of JavaScript to get what you want. Something like this (untested, only for example purposes, will need syntax tweaked to actually work):

var height = document.getElementById("head").offsetHeight;
document.getElementById("content").style.marginTop = height + 'px';

Something like that should get you the rendered height of the fixed <div> and set the content <div>'s margin accordingly. You'll also need to explicitly set a background color on the fixed <div>, otherwise the content will appear to bleed into the fixed one when scrolling.

CSS flexbox: variable height footer beneath variable height content area

Thanks @henryfhchan on Twitter for the solution! https://codepen.io/anon/pen/oJpvMZ

<div>
<img src="...">
<footer>caption is variable-size!</footer>
</div>

div {
display: flex;
flex-direction: column;
height: 100vh;
}

img {
display: block;
max-width: 100%;
object-fit: contain;
flex: 0 1 auto;
min-height: 0; /* necessary */
}

footer {
padding: 10px;
flex: auto
}


Related Topics



Leave a reply



Submit