Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling div without fixed height

I need to build a dynamically-resizing scrolling div.

The div should dynamically resize to fit the screen. But if the content doesn't fit on the screen, it should display a scrollbar. So the browser's own scrollbar should never need to become active.

I can get a scrollbar to appear in the div by placing another div inside it and using overflow: auto.

<div id="gridcontainer" style="overflow:auto;height:300px; width:100px;" >
    <div id="gridcontent" style="height:100%">
    <!--Put loads of text in here-->
    </div>
</div>

The trouble is that this only works when the first div has a fixed height. I had hoped I could just set the first div to height:100%, but sadly not- this property appears to get ignored, and the scrollbar just doesn't appear.

I have tried putting the divs in a table with height:100%, and setting the first div to height:auto, hoping it might take its height from its parent. But the div still seems to ignore the height property.

So my question is: Can this be done using just html, or- failing that- javascript?

like image 338
Urbycoz Avatar asked Apr 06 '11 09:04

Urbycoz


People also ask

How can I make a div scrollable without height?

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 make a scrollable container with dynamic height?

The key is to use Flexbox for all containers that wrap the scrollable container, and give the outermost container a predefined height. Since content lays vertically on the page by default, I recommend making each container use the vertical (column) flex layout rather than the default horizontal (row) layout.

How do I make my Div overflow?

Just use overflow: auto . Since your content by default just breaks to the next line when it cannot fit on the current line, a horizontal scrollbar won't be created (unless it's on an element that has word-wrapping disabled). For the vertical bar,it will allow the content to expand up to the height you have specified.

What is the opposite of overflow hidden?

hidden. The opposite of the default visible is hidden. This literally hides any content that extends beyond the box. This is particularly useful in use with dynamic content and the possibility of an overflow causing serious layout problems.


2 Answers

You could stretch the div using absolute positioning. This way it will always take the size of the browser window (or the closest positioned ancestor).

Given this HTML:

<div id="gridcontainer"></div> 

the CSS should be something like:

#gridcontainer {   position: absolute;   top: 0; bottom: 0; left: 0; right: 0;   overflow: auto; } 

Live Demo

like image 85
kapa Avatar answered Oct 08 '22 18:10

kapa


Since IE9 you can use viewport units.

Let's say that the height of your container is dynamic, unless its size is greater than the window height. In that case we stop the expansion & activate the scroll.

#container{    background: #eaeaea;    max-height: 100vh;    overflow-y: scroll;  }    div{    outline: 1px solid orange;    width: 200px;    height: 200px;  }
<div id='container'>    <div></div>    <div></div>    <div></div>    <div></div>    <div></div>    <div></div>    <div></div>  </div>
like image 29
Creaforge Avatar answered Oct 08 '22 19:10

Creaforge