20

I just want to have a sidebar that will be 100% of window height, but nothing works except this:

#sidebarBack {
background: rgba(20, 20, 20, .3);
position: fixed;
width: 250px;
height: 100%;
left: 0;
}

I don't want to have position: fixed, because I have horizontally scrollable content, so a fixed part would remain, well, fixed.

Is there any way to do such a thing, maybe with relative or absolute position?

Here's an quick Fiddle just for a test and explanation: JSFiddle

3
  • Forgive me, but I'm a little curious - If you are able to scroll it, doesn't that defeat the idea of making it the height of the page?
    – George
    Commented Nov 28, 2012 at 16:17
  • Do you want the sidebar to scroll out of the window, or stay where it is? And hide the scrollable code under the sidebar?
    – EricG
    Commented Nov 28, 2012 at 16:20
  • I want it to scroll out of the window if content is being scrolled. Like a relative/absolute position... Just like Wordpress admin panel... They have something similar...
    – dvlden
    Commented Nov 28, 2012 at 16:22

5 Answers 5

44

You can use the new and so-useful-I-can't-imagine-what-took-W3C-so-long vh CSS unit:

height:100vh;
2
  • and if i have a menu bar on top?
    – Toolkit
    Commented Jun 9, 2015 at 3:08
  • Why the hell everyone keeps suggesting on using vh... If you have a navbar at the top (fixed), you would face a pain on mobile devices, because it would be overlapped by a browser's navbar, and you won't do anything with that. Commented May 23, 2019 at 16:54
36

tl;dr - add html, body {height:100%;} to your CSS.


Percentage values in CSS are inherited from some ancestor that already has height declared. In your case, you need to tell all parents of your sidebar to be 100% height. I'm assuming that #sidebarBack is a direct child of body.

Essentially, your code above is telling #sidebarBack to be 100% height of its parent. Its parent (we are assuming) is body, so you need to set height: 100%; on body as well. We can't stop there, however; body inherits height from html, so we also need to set height: 100%; on html. We can stop here, because html inherits its properties from viewport, which already has a declared height of 100%.

This also means if you end up putting the #sidebar inside another div, then that div also needs height:100%;.

Here is an Updated JSFiddle.

Changed your CSS to:

html, body {
    height:100%;
}

#sidebar {
    background: rgba(20, 20, 20, .3);
    width: 100px;
    height: 100%;
    left: 0;
    float:left;
}

section#settings {
    width:80%;   
    float:left;
    margin-left:100px;
    position:fixed;
}
4
  • Hmmm if I add this it makes my content scrollable horizontally and vertically and does not give 100% height to sidebar in relative/absolute position.
    – dvlden
    Commented Nov 28, 2012 at 16:24
  • right - a lot of people forget that the tags: html, body, form are still HTML elements that don't have a default height of 100% - they need to be set manually.
    – Losbear
    Commented Nov 28, 2012 at 16:25
  • @NenaddvL I'm not sure why you've selected this answer, as it doesn't appear to accomplish what you asked for. Among other issues, this answer effectively disables all scrolling by setting position:fixed on your #settings section. I've added an answer below that I think will be a more complete solution to your problem (see here: stackoverflow.com/a/13610461/416630).
    – Lee
    Commented Nov 28, 2012 at 17:05
  • @Darren I have noticed more than one question be marked as a duplicate of this one, so I have added some more information to your answer and revised the question to better serve as a canonical Q&A in the future. Feel free to adjust your answer as you wish to improve it!
    – TylerH
    Commented Aug 13, 2014 at 18:32
7

First, tell the body element to fill the window, rather than shrinking to the size of the content:

body { position: absolute; min-height: 100%; }

by using min-height instead of height, body will be allowed to expand beyond the window's height when the content is longer than the window (i.e. this allows for vertical scrolling when needed).

Now, set your "#sidebar" to be position:absolute, and use top:0; bottom:0; to force it to fill the parent element's vertical space:

#sidebar {
    position: absolute;
    top:0; bottom:0;
    left: 0;
    width: 100px;
    background: rgba(20, 20, 20, .3);
}

Here are a couple of jsFiddles:

As you'll see, in both examples, I've preserved your width setting on the "#settings" section, thus showing that horizontal scrolling works as you requested.

1
  • I am having issues with every single answer. None actually resolves the problem. Except checked answer if I replace his fixed to absolute position on #settings. However neither that way it is good because html, body 100% gives me scrollable page in height and that is now what I want. I think I will change design and positions because it definetly does not go this way. Thanks for your help too!
    – dvlden
    Commented Nov 28, 2012 at 17:52
1

Try the following

#sidebarBack {
 background: rgba(20, 20, 20, .3);
 position: fixed;
 width: 250px;
 top:0;
 bottom:0;
 left: 0;
}
1
  • Hmmm not quitely what I need. It remain fixed and just pulls it over header because of top:0;
    – dvlden
    Commented Nov 28, 2012 at 16:24
0

Try this -

html,body{height:100%;}
#sidebar {
background: rgba(20, 20, 20, .3);
/*position: fixed;*/
width: 100px;
height: 100%;
left: 0;
float:left;
}


section#settings {
width: 62%;
height: auto;
display: inline-block;
position: relative;
margin-left: 100px;
float:left;
}

1
  • Doesn't make sidebar 100% height still :(
    – dvlden
    Commented Nov 28, 2012 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.