18

UPDATE 2

So when the content in #main increases, it should push down the footer, like so:

scroll

... so the footer should not be position: fixed;. It should be on the bottom when there is not enough content and should be pushed down when there is more content than the height of the page.

In both scenarios, #sidebar needs to span the height from the bottom of #header to the top of #footer.

UPDATE

Some brutal specifics... the footer should be at the bottom whenever the content on the page is small, but when the content is large enough, it should push the footer down (this is the functionality described in the sticky footer links I've provided). I need the sidebar to always be between the header and footer at full height (from bottom of header to top of footer).

This is quite the challenge for me. Ideas...?


I'm trying to make this layout work without using JavaScript... here's what I mean in picture form:

BAD... current layout bad layout

GOOD... desired layout good layout

Notice how the sidebar extends all the way to the footer in the desired layout. I'm using the sticky footer approaches, http://ryanfait.com/sticky-footer/ and http://www.cssstickyfooter.com/, and now I need to extend the sidebar to span the height from the header to the footer. Here's what I have...

http://jsfiddle.net/UnsungHero97/2ZhpH/

... and the code in case jsFiddle is down...

HTML

<div id="wrapper">
    <div id="header"><div id="header-content">Header</div></div>
    <div id="content">
        <div id="sidebar">Sidebar<br/>Sidebar<br/>Sidebar<br/></div>
        <div id="main">Main</div>
    </div>
    <div class="push"></div>
</div>
<div id="footer"><div id="footer-content">Footer</div></div>

CSS

html, body {
    margin: 0px;
    padding: 0px;
    min-height: 100%;
    height: 100%;
}
#wrapper {
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin: 0 auto -50px; /* the bottom margin is the negative value of the footer's height */
}
#footer {
    height: 50px;
}
#footer-content {
    border: 1px solid magenta;
    height: 32px; /* height + top/bottom paddding + top/bottom border must add up to footer height */
    padding: 8px;
}
.push {
    height: 50px;
    clear: both;
}    

#header {
    height: 50px;
}
#header-content {
    border: 1px solid magenta;
    height: 32px; /* height + top/bottom paddding + top/bottom border must add up to footer height */
    padding: 8px;
}
#content {
    height: 100%;
}
#sidebar {
    border: 1px solid skyblue;
    width: 100px;
    height: 100%;
    float: left;
}

Any suggestions on how to do this? I've tried using position: fixed but that approach becomes very ugly when the page is large enough and you need to scroll.

Hristo
  • 45,559
  • 65
  • 163
  • 230
  • What are you exactly trying to do? Are you trying to build that layout, such that the footer sticks to the bottom of the page, no matter what the screensize is? – sniper Jul 26 '11 at 23:05
  • @sniper... no, the footer should be at the bottom whenever the content on the page is small, but when the content is large enough, it should push the footer down (this is the functionality described in the sticky footer links I've provided). I need the sidebar to always be between the header and footer at full height (from bottom of header to top of footer) – Hristo Jul 26 '11 at 23:12
  • @Hristo, just use a table? ;-) – Qtax Jul 26 '11 at 23:50
  • @Rob... mind showing me at least 1 of 3987? I bet its not exactly what I'm looking for... – Hristo Jul 26 '11 at 23:51
  • thirtydot@ and @Lollero , i was looking for it all day, i tried both of yours approach on a asp.net masterpage and both fail, i found [this](http://stackoverflow.com/questions/6080671/two-column-page-layout-with-header-and-footer) that worked. Thanks – newway Sep 30 '11 at 20:24
  • @newway... that solution is NOT what I was looking for. but I'm glad it works for you :) – Hristo Oct 04 '11 at 07:21

4 Answers4

24

With little content: http://jsfiddle.net/2ZhpH/41/

With lots of content: http://jsfiddle.net/2ZhpH/42/

I added position: relative to #wrapper, and then:

#sidebar {
    border: 1px solid skyblue;
    width: 100px;
    position: absolute;
    left: 0;
    top: 50px;
    bottom: 50px;
}
#main {
    margin-left: 102px
}

(why position: relative? Just to avoid something like this: http://jsfiddle.net/2ZhpH/40/)

thirtydot
  • 224,678
  • 48
  • 389
  • 349
  • GOD! That's what I was trying. I was using `position: absolute; top: 50px; bottom: 50px;` and it wasn't working for me :( +1. A job well done! – Hristo Jul 27 '11 at 01:00
  • @thirtydot... full screen looks fine. what do you not like about it? – Hristo Jul 27 '11 at 01:03
  • Disregard my last comment, I confused myself. It looks fine. – thirtydot Jul 27 '11 at 01:04
  • 1
    +1 for setting both `top` and `bottom`, didn't think of that. – Qtax Jul 27 '11 at 01:05
  • @thirtydot... the other thing that would be really awesome to have is when there is little content and a lot of content in #sidebar, the footer should stop at the bottom of #sidebar when resizing the window (it currently stops at the bottom of #main). – Hristo Jul 27 '11 at 01:05
  • That problem can't be fixed, due to `#sidebar` being absolutely positioned. :( – thirtydot Jul 27 '11 at 01:20
  • 1
    I notice that when #sidebar has text content that extends downward longer than #main content, the sidebar text extends over the footer rather than pushing it down and stretching the sidebar background. Is there a fix for that? – teaman Dec 31 '12 at 07:25
  • 1
    +1 @thirtydot. This is truly amazing. This question absolutely needs to be renamed. It should come up in the first results. I have been looking for this for days, I can't even believe this is actually doable with CSS only. Many many thanks. – Mick Mar 03 '13 at 15:53
  • Too bad the accepted solution doesn't allow for links in the footer :-( Tested in Firefox 38.0.5 – Petr 'PePa' Pavel Jun 07 '15 at 12:01
  • 1
    @Petr'PePa'Pavel You can add position: relative to the footer and you can use the links in the footer without issues. – Sami Jul 31 '15 at 09:40
8

Reading really isnt my thing so dont hang me if i missed something, but I think this should do it.

http://jsfiddle.net/Fggk6/ - Note that the sidebar uses background image, it's a lot easier that way. Both sidebar and content area backgrounds come from #wrap

Also note that the sticky footer has my fingerprints all over it since its my own design ..it might be dirty.

Joonas
  • 7,227
  • 9
  • 40
  • 65
  • +1. interesting approach. this pretty much does what I want it to do, but I like @thirtydot's answer better. thanks for your response! – Hristo Jul 27 '11 at 01:15
  • This works nicely. @Hristo: If you can get away with using [faux columns](http://www.alistapart.com/articles/fauxcolumns/), go for it. – thirtydot Jul 27 '11 at 01:22
  • Lollero & thirtydot... this was just for fun, I was just curious if this can be done using only HTML/CSS. it can! thanks for your help! – Hristo Jul 27 '11 at 01:26
  • I enjoyed it. These types of questions is the reason why i started answering questions here at SO. Thank you. :) – Joonas Jul 27 '11 at 01:28
  • These types of interactions are the reason why I like to ask such questions :) – Hristo Jul 27 '11 at 01:32
  • nice trick. although problem with this is responsiveness and having multiple layout (i.e. one without the sidebar).. – tkit Jul 01 '15 at 14:20
0

If you add position:absolute; to css style #sidebar it will extend all the way down.

You will then need to amend div id="main to <div id="main" style="margin-left:100px;position:absolute;">Main</div> or create another css class.

I hope this is clear.

Barry Kaye
  • 7,682
  • 6
  • 42
  • 64
  • This doesn't work quite right. Notice how the sidebar extends beyond the footer. I need it to stop at the top of the footer. – Hristo Jul 26 '11 at 23:10
0

How about that ?

http://jsfiddle.net/2ZhpH/25/

UPDATE

Added background color to show that with a footer fixed to the bottom, normal that the height of the sidebar is beyond the footer. With z-index, you won't notice it.

Warface
  • 5,029
  • 9
  • 56
  • 82