I have a div with dynamic content. When the content loads in the div I want the div to resize to fit the content. But I want this resizing to have a maximum height. If the content needs more than this maximum height I'd like there to be a scrollbar.
I've searched through the questions here but haven't been able to find what I'm looking for (apart from one where the question-asker replied saying he'd figured it out himself). I've read I can do it with jquery but I'm pretty new to that, but I did go through the demos of resizing a div, and changing classes and stuff like that but I'm really at a loss on how to implement what I'm looking for. I have tried to find a solution to this, so please be gentle.
Answer: Use the CSS max-width Property You can simply use the CSS max-width property to auto-resize a large image so that it can fit into a smaller width <div> container while maintaining its aspect ratio.
Method 2: We can make the display attribute of the child container to table-row and display attribute of parent container to table, that will take all the height available from the parent div element. To cover all the width, we can make the width of parent div to 100%.
If height: auto; the element will automatically adjust its height to allow its content to be displayed correctly. If height is set to a numeric value (like pixels, (r)em, percentages) then if the content does not fit within the specified height, it will overflow.
div {height:auto;overflow:scroll;max-height:200px;}
Use:
<div style="max-height: 400px; overflow-y:scroll; overflow-x: hidden">
<p>Some dynamic content loads inside the div</p>
</div>
Then add: <div style="clear:both"></div>
before closing the "div":
<div style="max-height: 400px">
<p>Some dynamic content loads inside the div</p>
<div style="clear:both"></div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With