I'm trying to make a website with no vertical scrolling for a page, but i need that one of the DIVs i have to expand vertically to the bottom of the page (at most), and that when it has content that does not fit, the div should create a vertical scroller.
i already have the css for the inside of the div figured out in this fiddle, creating the scroller when needed. i also have figured out how to make the container div grow to occupy exactly the vertical space it has in the page. i just can't make them work out together!
please have in mind that in jsfiddle you won't be able to view the content of the whole website and in that sense what you get for the 2nd fiddle doesn't really show what's being done, but it works as i intended though.
just another note: as they are different fiddles, the id#container div in the 1st fiddle is he id#dcontent div of the 2nd example.
there is one other thing: for a type of content, this div will scroll vertically, but for other type of content, i want it to scroll horizontally, as it will have a product "slider" displaying elements horizontally inside this DIV.
please also look at this photo because it might be easier to understand what i'm trying to say: PICTURE
i tried looking to other questions regarding these topics, but none seemed to cover all of the aspects i'm trying to solve... :S
if there is something else i can provide to help you/me :) figuring it out, pls let me know!
thanks!
EDIT1: fixed typos
EDIT2: added picture for explanation
Try this:
CSS:
#content {
margin: 0 auto;
border: 1px solid red;
width:800px;
position:absolute;
bottom:0px;
top:0px;
overflow:auto
}
HTML:
<body>
<div id="content">
...content goes here...
</div>
<body>
If you don't like absolute positioning in this case, just can play with making a parent and child div to this one, both with position:relative
.
EDIT: Below should work (I just put the css inline for the moment):
<div style="margin:0 auto; width:800px; height:100%; overflow:hidden">
<div style="border: 1px solid red; width:800px; position:absolute; bottom:0px; top:0px; overflow:auto">
...content goes here...
</div>
</div>
i think it's pretty easy. just use this css
.content {
width: 100%;
height:(what u wanna give);
float: left;
position: fixed;
overflow: auto;
overflow-y: auto;
overflow-x: none;
}
after this just give this class to ur div just like -
<div class="content">your stuff goes in...</div>
I'm surprised no one's mentioned calc()
yet.
I wasn't able to make-out your specific case from your fiddles, but I understand your problem: you want a height: 100%
container that can still use overflow-y: auto
.
This doesn't work out of the box because overflow
requires some hard-coded size constraint to know when it ought to start handling overflow. So, if you went with height: 100px
, it'd work as expected.
The good news is that calc()
can help, but it's not as simple as height: 100%
.
calc()
lets you combine arbitrary units of measure.
So, for the situation you describe in the picture you include:
Since all the elements above and below the pink div are of a known height (let's say, 200px
in total height), you can use calc
to determine the height of ole pinky:
height: calc(100vh - 200px);
or, 'height is 100% of the view height minus 200px.'
Then, overflow-y: auto
should work like a charm.
This is a horizontal solution with the use of FlexBox and without the pesky absolute
positioning.
body {_x000D_
height: 100vh;_x000D_
margin: 0;_x000D_
display: flex;_x000D_
flex-direction: row;_x000D_
}_x000D_
_x000D_
#left,_x000D_
#right {_x000D_
flex-grow: 1;_x000D_
}_x000D_
_x000D_
#left {_x000D_
background-color: lightgrey;_x000D_
flex-basis: 33%;_x000D_
flex-shrink: 0;_x000D_
}_x000D_
_x000D_
#right {_x000D_
background-color: aliceblue;_x000D_
display: flex;_x000D_
flex-direction: row;_x000D_
flex-basis: 66%;_x000D_
overflow: scroll; /* other browsers */_x000D_
overflow: overlay; /* Chrome */_x000D_
}_x000D_
_x000D_
.item {_x000D_
width: 150px;_x000D_
background-color: darkseagreen;_x000D_
flex-shrink: 0;_x000D_
margin-left: 10px;_x000D_
}
_x000D_
<html>_x000D_
_x000D_
<body>_x000D_
<section id="left"></section>_x000D_
<section id="right">_x000D_
<div class="item"></div>_x000D_
<div class="item"></div>_x000D_
<div class="item"></div>_x000D_
</section>_x000D_
</body>_x000D_
_x000D_
</html>
_x000D_
$(document).ready(function() {
//Fix dropdown-menu box size upto 2 items but above 2 items scroll the menu box
$("#dropdown").click(function() {
var maxHeight = 301;
if ($(".dropdown-menu").height() > maxHeight) {
maxHeight = 302;
$(".dropdown-menu").height(maxHeight);
$(".dropdown-menu").css({'overflow-y':'scroll'});
} else {
$(".dropdown-menu").height();
$(".dropdown-menu").css({'overflow-y':'hidden'});
}
});
});
_x000D_
This is what I managed to do so far. I guess this is kind of what you're trying to pull out. The only thing is that I can still not manage to assign the proper height to the container DIV.
The HTML:
<div id="container">
<div id="header">HEADER</div>
<div id="fixeddiv-top">FIXED DIV (TOP)</div>
<div id="content-container">
<div id="content">CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT</div>
</div>
<div id="fixeddiv-bottom">FIXED DIV (BOTTOM)</div>
</div>
And the CSS:
html {
height:100%;
}
body {
height:100%;
margin:0;
padding:0;
}
#container {
width:600px;
height:50%;
text-align:center;
display:block;
position:relative;
}
#header {
background:#069;
text-align:center;
width:100%;
height:80px;
}
#fixeddiv-top {
background:#AAA;
text-align:center;
width:100%;
height:20px;
}
#content-container {
height:100%;
}
#content {
text-align:center;
height:100%;
background:#F00;
margin:0 auto;
overflow:auto;
}
#fixeddiv-bottom {
background:#AAA;
text-align:center;
width:100%;
height:20px;
}
You can do this assignment easily by using jquery. In this way, you can define number of row limitation. Furthermore, you can regular breakpoints height that want adding vertical scrolling. I must say that more than 3 rows get modify class and also height is 76px.
$(document).ready(function() {_x000D_
var length = $(this).find('li').length;_x000D_
if (length > 3) {_x000D_
$(".parent").addClass('modify');_x000D_
}_x000D_
})
_x000D_
/*for beauty*/_x000D_
_x000D_
ul {_x000D_
margin: 0 auto;_x000D_
width: 50%;_x000D_
border: 1px solid #ccc;_x000D_
padding: 3px;_x000D_
}_x000D_
_x000D_
ul li {_x000D_
padding: 3px;_x000D_
background: #ccc;_x000D_
margin: 2px 0;_x000D_
list-style: none;_x000D_
}_x000D_
_x000D_
/*main class*/_x000D_
_x000D_
.modify {_x000D_
overflow-y: scroll;_x000D_
height: 76px;_x000D_
}
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>_x000D_
_x000D_
<ul class="parent">_x000D_
<li>item 1</li>_x000D_
<li>item 2</li>_x000D_
<li>item 3</li>_x000D_
<li>item 4</li>_x000D_
</ul>
_x000D_
You can do this just with flexboxes and overflow
property.
Even if parent height is computed too.
Please see this answer or JSFiddle for details.
Quick Answer with Main Points
Pretty much the same answer as the best chosen answer from @Joum, to quicken your quest of trying to achieve the answer to the posted question and save time from deciphering whats going on in the syntax --
Answer
Set position attribute to fixed, set the top and bottom attributes to your liking for the element or div that you want to have an "auto" size of in comparison to its parent element and then set overflow to hidden.
.YourClass && || #YourId{
position:fixed;
top:10px;
bottom:10px;
width:100%; //Do not forget width
overflow-y:auto;
}
Wallah! This is all you need for your special element that you want to have a dynamic height according to screen size and or dynamic incoming content while maintaining the opportunity to scroll.
Source: Stackoverflow.com