Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrollbar without fixed height/Dynamic height with scrollbar

I have this HTML structure:

<div id="body">     <div id="head">         <p>Dynamic height without scrollbar</p>     </div>     <div id="content">         <p>Dynamic height with scrollbar</p>     </div>     <div id="foot">         <p>Fixed height without scrollbar</p>     </div>   </div> 

I want to have the three parts inside the main part (#body) without overflow. So I need a scroll bar in the middle part.

I tried this CSS:

#content{     border: red solid 1px;     overflow-y: auto; } 

And this:

#content{     border: red solid 1px;     overflow-y: auto;     height: 100%; } 

But neither of them work.

I made an example at JSFiddle.

Can I do this with only CSS and HTML? I'd prefer to avoid Javascript.

like image 749
GuillaumeS Avatar asked Jun 11 '12 13:06

GuillaumeS


People also ask

Does 100vw include scrollbar?

Many assume that width: 100vw is the same as width: 100% . This is true on a page that doesn't scroll vertically, but what you might not realize is that the viewport actually includes the scrollbars, too.

How can I increase scrollbar height?

To get the height of the scroll bar the offsetHeight of div is subtracted from the clientHeight of div. OffsetHeight = Height of an element + Scrollbar Height. ClientHeight = Height of an element. Height of scrollbar = offsetHeight – clientHeight.

What is the difference between overflow auto and scroll?

overflow: autoThe auto value is similar to scroll , but it adds scrollbars only when necessary: You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.


1 Answers

Flexbox is a modern alternative that lets you do this without fixed heights or JavaScript.

Setting display: flex; flex-direction: column; on the container and flex-shrink: 0; on the header and footer divs does the trick:

HTML:

<div id="body">     <div id="head">         <p>Dynamic size without scrollbar</p>         <p>Dynamic size without scrollbar</p>         <p>Dynamic size without scrollbar</p>       </div>     <div id="content">         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>         <p>Dynamic size with scrollbar</p>     </div>     <div id="foot">         <p>Fixed size without scrollbar</p>         <p>Fixed size without scrollbar</p>     </div>   </div> 

CSS:

#body {     position: absolute;     top: 150px;     left: 150px;     height: 300px;     width: 500px;     border: black dashed 2px;     display: flex;     flex-direction: column; }  #head {     border: green solid 1px;     flex-shrink: 0; }  #content{     border: red solid 1px;     overflow-y: auto;     /*height: 100%;*/ }  #foot {     border: blue solid 1px;     height: 50px;     flex-shrink: 0; } 
like image 140
mtyaka Avatar answered Sep 20 '22 10:09

mtyaka