Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a div full screen and scrollable?

Tags:

css

With absolute, it scrolls but doesn't get 100% in height:

.class {
 position: absolute;
 left: 0;
 right: 0;
 top: 0;
 bottom: 0;
 height: 100%;
 width: 100%;
 z-index: 1000000;
 background: rgba(0, 0, 0, 0.9);
}

With fixed, it gets 100% in height but doesn't scroll

.class {
 position: fixed;
 left: 0;
 top: 0;
 height: 100%;
 width: 100%;
 z-index: 1000000;
 background: rgba(0, 0, 0, 0.9);
}

I would like to avoid adding a fixed heigth to the child element and make it overflow: scroll

like image 422
rob.m Avatar asked Jan 21 '15 15:01

rob.m


People also ask

How do I enable scrolling on a div?

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.

What tags make screen scrollable?

Usually, a scroll box is obtained with the usage of the <div> tag, and the customization for the bar is done using CSS properties. While creating a scroll box, we use a CSS property known as 'overflow' and set it to 'scroll' to let the browser know that it is to add the horizontal and vertical scroll bars.

How do I make a scrollable HTML page?

It can be easily done by using the overflow property. The overflow property has different values. E.g., overflow: auto; and an axis hiding procedure like overflow-x: hidden; and overflow-y: auto; will make a bar scrollable vertically and horizontally, and the "auto" value will add only a vertically scrollable bar.


2 Answers

So first of all, if you want to have 100% height and width, you will have to define WHAT that is. So you have to tell the html/body that the size they have, is 100% width/height.

Now you don't want to let the page scroll down, if the text goes out of the div, because you will see white space if you do. So set overflow-y to scroll, so it will scroll inside the div, and not in the document itself.

html,body{
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
.fullwidth{
    width:100%;
    height: 100%;
    background-color: red;
    overflow-y: scroll;
}

Here is a working fiddle:

WORKING FIDDLE

like image 183
Refilon Avatar answered Oct 10 '22 18:10

Refilon


You need to add overflow:auto so that it scrolls if the content overflows the container.

.class {
    ...
    overflow:auto;
}

http://jsbin.com/kuqaqumude/1/edit?html,css,output

For more details concerning overflow: auto and overflow: visible,
see: http://www.w3.org/TR/CSS21/visufx.html#overflow-clipping

like image 26
Joseph Marikle Avatar answered Oct 10 '22 19:10

Joseph Marikle