Reputation: 11888
I do have a div where I want a scrollbar (overflow-y: auto; height: 300px;
) to appear if the text inside it is to big. However, my div already contains the property height:auto;
(for some vertical alignment sake). Is there anyway I can keep height:auto
and still having the scrollbar appearing when it is necessary ?
HTML:
<div class = 'valign scrollbar'>
My Text
</div>
CSS:
.valign {
height: auto;
}
.scrollbar {
height: 300px;
overflow-y: auto;
}
Upvotes: 4
Views: 13455
Reputation: 1
As long as the parent container has a set height, max-height: 100%;
seems to work.
.container {
background-color: green;
height: 400px;
width: 400px;
}
.valign {
height: auto;
}
.scrollbar {
max-height: 100%;
overflow-y: auto;
background-color: red;
}
<div class="container">
<div class='valign scrollbar'>
Short: Standard Lorem Ipsum passage, used since the 1500s:
</div>
</div>
<br><br>
<div class="container">
<div class='valign scrollbar'>
Long: Standard Lorem Ipsum passage, used since the 1500s:
<br><br>
"Lorem ipsum dolor sit amet, consectetur adipiscing 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."
<br><br>
1914 translation by H. Rackham
<br><br>
"On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."
</div>
</div>
Upvotes: 0
Reputation: 28645
Sounds like you might be looking for max-height which will limit the div height to the value provided.
.scrollbar {
max-height: 300px;
overflow-y: auto;
}
Upvotes: 9
Reputation: 2731
You can either set a fixed height. Or set a max-height css style. If you want it to reach no more than 500px, for example you would call
.scrollbar {
max-height:500px;
overflow-y: auto;
}
That should help you put a limit to how big the element can get.
Upvotes: 2