Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make the contents of a fixed element scrollable only when it exceeds the height of the viewport?

I have a div positioned fixed on the left side of a web page, containing menu and navigation links. It has no height set from css, the content determines the height, the width is fixed. The problem is that if the content is too much, the div will be larger than the window's height, and part of the content will not be visible. (Scrolling the window doesn't help, since the position is fixed and the div won't scroll.)

I tried to set overflow-y:auto; but that doesn't help either, the div doesn't seem to notice that part of it is outside of the window.

How can I make it's contents scrollable only, if needed, if the div hangs out of the window?

like image 617
Mkoch Avatar asked Sep 19 '13 12:09

Mkoch


People also ask

How do I show scroll only when overflow?

Use overflow: auto . Scrollbars will only appear when needed. (Sidenote, you can also specify for only the x, or y scrollbar: overflow-x: auto and overflow-y: auto ).

How do I make my overflow scrollable?

There are different values in overflow property. For example: overflow:auto; and the axis hiding procedure like overflow-x:hidden; and overflow-y:auto;. It will make vertical and horizontal scrollable bar and the auto will make only vertically scrollable bar. For vertical scrollable bar use the x and y axis.

How do I limit the scroll area in HTML?

body has a margin by default in different browsers, generally it is around 8px, so you can remove it by using margin: 0 . Your main css property should always come after your vendor prefixes such as -webkit- , -moz- , etc.


2 Answers

You probably can't. Here's something that comes close. You won't get content to flow around it if there's space below.

http://jsfiddle.net/ThnLk/1289

.stuck {     position: fixed;     top: 10px;     left: 10px;     bottom: 10px;     width: 180px;     overflow-y: scroll; } 

You can do a percentage height as well:

http://jsfiddle.net/ThnLk/1287/

.stuck {     max-height: 100%; } 
like image 133
isherwood Avatar answered Sep 30 '22 03:09

isherwood


The link below will demonstrate how I accomplished this. Not very hard - just have to use some clever front-end dev!!

<div style="position: fixed; bottom: 0%; top: 0%;">      <div style="overflow-y: scroll; height: 100%;">         Menu HTML goes in here      </div>  </div> 

http://jsfiddle.net/RyanBrackett/b44Zn/

like image 31
Ryan Brackett Avatar answered Sep 30 '22 03:09

Ryan Brackett