Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a div to use certain percentage of the height, while using overflow:auto to put a scrollbar?

I have the following code:

div.leftnav {
    overflow: auto ;
    height: 50% ;
}

That, when I set a big div of this class, does not work (it doesn't show the scrollbar nor resize it to 50%, however it works perfectly if instead if I use

height: 400px ;

Or some other "absolute" value.

like image 819
Martín Fixman Avatar asked Dec 07 '10 16:12

Martín Fixman


People also ask

How auto adjust the div height according to content?

Syntax: height: length|percentage|auto|initial|inherit; Property Values: height: auto; It is used to set height property to its default value.

How do I make my div overflow scroll?

For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.

How do you set the height of a tbody with overflow scroll?

If you want tbody to show a scrollbar, set its display: block; . Set display: table; for the tr so that it keeps the behavior of a table. To evenly spread the cells, use table-layout: fixed; . Anyhow, to set a scrollbar, a display reset is needed to get rid of the table-layout (which will never show scrollbar).


1 Answers

height will only work if the parent element has a height instruction as well.

So say you have really simple markup :

<html>
    <body>
        <div class="leftnav">
            really long text
        </div>
    </body>
</html>

Then the following CSS will work for you :

div.leftnav {
    overflow: auto ;
    height: 50%;
}
html, body { 
    height:100%;
}

You need to have height instructions all the way though. If there is a height instruction missing on one of the parent elements, then the 100% won't mean anything to the div. If you can't access all the elements down the tree, then you will need a parent element with a fixed height :

<div class="leftnav-container">
    <div class="leftnav">
        really long text
    </div>
</div>

Then you need this css :

div.leftnav {
    overflow: auto ;
    height: 50%;
}
div.leftnav-container {
    height : 500px;
}
like image 172
Hugo Migneron Avatar answered Nov 10 '22 10:11

Hugo Migneron