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 tha
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.
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.
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.
JSFIDDLE
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;
}
$(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'});
}
});
});
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() {
var length = $(this).find('li').length;
if (length > 3) {
$(".parent").addClass('modify');
}
})
/*for beauty*/
ul {
margin: 0 auto;
width: 50%;
border: 1px solid #ccc;
padding: 3px;
}
ul li {
padding: 3px;
background: #ccc;
margin: 2px 0;
list-style: none;
}
/*main class*/
.modify {
overflow-y: scroll;
height: 76px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="parent">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
<li>item 4</li>
</ul>
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.