Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing a div to fit content with maximum height

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.

like image 779
qudoz Avatar asked May 01 '12 13:05

qudoz


People also ask

How do I resize a content to fit in a div?

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.

How do I stretch a div to max width?

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%.

How do you autofit height in CSS?

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.


2 Answers

 div {height:auto;overflow:scroll;max-height:200px;}
like image 136
meWantToLearn Avatar answered Sep 18 '22 16:09

meWantToLearn


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>
like image 40
Nick Avatar answered Sep 21 '22 16:09

Nick