With position: relative;
and position: absolute;
You can just use one positition: absolute
in the .sidebar
and positition: relative
in the parent object (name of .main
), note that html and body need to have height: 100%;
and the .main
has to be the direct child of the body and have min-height: 100%
I also added margin-left: 300px;
why the .content
do not get below the .sidebar
.
This effect works well for all modern browsers like IE8, Chrome, Firefox, Safari. However it doesn’t work well in IE with quircks-mode, example:
html, body {
margin: 0;
padding: 0;
height: 100%;
}
.main {
position: relative;
min-height: 100%;
}
.sidebar {
top: 0;
left: 0;
position: absolute;
width: 300px;
height: 100%;
overflow: auto;
background: #fc0;
}
.content {
margin-left: 310px;
}
<div class="main">
<div class="sidebar">
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
</div>
<div class="content">
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
</div>
</div>
With position: fixed;
You may also want an effect that the sidebar accompanies the screen, I mean stay fixed on it, you can use position: fixed;
, the only difficulty with it will be to adjust according to the margins (if you need)
html, body {
margin: 0;
padding: 0;
}
.sidebar {
top: 0;
left: 0;
position: fixed;
width: 300px;
height: 100%;
overflow: auto;
background: #fc0;
}
.content {
margin-left: 310px;
}
<div class="main">
<div class="sidebar">
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
</div>
<div class="content">
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
</div>
</div>
With display: flex;
Being an app-Chrome it supports flex which can facilitate, so use display: flex;
in the parent object (in the example I created a .main
), and flex: 1;
in children objects, this is basically the system of "Equal height Columns" (or same height Columns).
Using flex (in this example the height: 100%;
is dispensable, I only added because otherwise it could be imperceptible as the content varied):
html, body {
margin: 0;
padding: 0;
height: 100%;
}
.main {
display: flex;
}
.sidebar {
flex: 1;
max-width: 300px;
overflow: auto;
background: #fc0;
}
.content {
flex: 1;
}
<div class="main">
<div class="sidebar">
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
foo <br> foo <br> foo <br> foo <br> foo <br> foo <br>
</div>
<div class="content">
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
baz <br> baz <br> baz <br> baz <br> baz <br> baz <br>
</div>
</div>
tried
height: 100vh
?– Caio Felipe Pereira
@Caiofelipepereira If it works on Google Chrome server. The problem is someone looking for my question, wanting a crossbrowser solution
– Wallace Maxters
max-height: 100% works, but will depend on how the html and css of the parent elements are mounted'.
– Givanildo R. de Oliveira
According to http://caniuse.com/#search=vh, the support is reasonable, until. People will only stop using IE when we stop doing things for IE.. Anyway, you can follow the @Givanildor.deOliveira approach, or go to a solution
flex-box
, which is much more elegant– Caio Felipe Pereira