Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  188] [ 1]  / answers: 1 / hits: 21726  / 10 Years ago, fri, september 19, 2014, 12:00:00

I have a div with preset width and height. I need to change the height of that div based on content. I was looking for some JavaScript but couldn't find appropriate for me.



Here is my simple example, which needs an improvement http://jsfiddle.net/dmitry313/1zr6Lhwa/



HTML



<div id=content>
<h2>Text</h2>
<p>Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example </p>
</div>


CSS



    h2 {
font:18px/30px Arial;
}
#content {
width:200px;
height:100px;
border:1px solid #000;
background: yellow;
}


EDIT: Initially preset width and height are important and can't be changed


More From » jquery

 Answers
18

Use min-height instead of height so that the <div> expands accordingly.





h2 {
font:18px/30px Arial;
}
#content {
width:200px;
min-height:100px;
border:1px solid #000;
background: yellow;
}

<div id=content>

<h2>Text</h2>

<p>Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example</p>
</div>




[#69399] Wednesday, September 17, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminkyrap

Total Points: 631
Total Questions: 89
Total Answers: 109

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
;