medBouzid
medBouzid

Reputation: 8442

add overflow auto to an absolute positioned div

I have a menu and sidebar which are positioned fixed and a main area positioned absolute

What I want to achieve is to set a min-width to the content inside the main area, and have scrollbar x and y on it so I can navigate inside it

I tried to add overflow:auto to the #main and overflow:hidden to the body but scroll-bars are not showing inside #main, I am not sure what I am doing wrong :

this is the jsfiddle :

https://jsfiddle.net/mody5/Lhc3fbke/

and this is the code :

<div id="sidebar">Sidebar</div>

<div id="menu">Menu</div>

<div id="main">

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>

css :

html, body{
    height: 100%;
}

body{
    overflow: hidden;
}
#sidebar{
    width: 200px;
    position: fixed;
    background: blue;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 999;
    padding-top: 100px;
}

#menu{
    position: fixed;
    top: 0; left: 0; right: 0;
    height: 50px;
    background: red;
    color:white;
    z-index: 9999;
}

#main{
    position: absolute;
    left: 200px;
    top: 50px;
    right: 0;
    min-height: 100%;
    min-width: 1400px;
    height: 100%;

    overflow: auto;

}

Upvotes: 2

Views: 55

Answers (2)

nicael
nicael

Reputation: 19049

The problem was the main div not overflowing, but just exempting the borders of the window. It was just larger.

You should set the main's borders to the window borders (changing also height:100% to bottom:0 because the first was bottom-lowerflowing), and add the scrollable div inside the main with the free dimensions (i.e. they can be large), like this:

html,
body {
  height: 100%;
}
body {
  overflow: hidden;
}
#sidebar {
  width: 200px;
  position: fixed;
  background: blue;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 999;
  padding-top: 100px;
}
#menu {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 50px;
  background: red;
  color: white;
  z-index: 9999;
}
#main {
  position: absolute;
  left: 200px;
  top: 50px;
  right: 0;
  bottom:0;
  overflow: scroll;
}
#scrollable {
  min-width: 1400px;
  min-height: 100%;
}
<div id="sidebar">Sidebar</div>

<div id="menu">Menu</div>

<div id="main">
  <div id="scrollable">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
    in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </div>
</div>

Upvotes: 3

Solrac
Solrac

Reputation: 931

Just FORCE that behavior, try this:

<div id="main" style="overflow:scroll !important">
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
  proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>

Upvotes: 0

Related Questions