最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamically change height of "div", based on content with preset "width" and &a

programmeradmin2浏览0评论

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 /

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

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/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

Share Improve this question edited Sep 19, 2014 at 13:51 Mia asked Sep 19, 2014 at 13:02 MiaMia 4434 gold badges11 silver badges25 bronze badges 3
  • 2 why dont you use height: auto;? – Alex Commented Sep 19, 2014 at 13:06
  • Is what you're setting the minimum expected size? or is the div supposed to bee 0 height and width? – T J Commented Sep 19, 2014 at 13:11
  • @Alex Initially preset "width" and "height" are important and can't be changed – Mia Commented Sep 19, 2014 at 13:18
Add a ment  | 

4 Answers 4

Reset to default 6

Just remove height from CSS. If you need a preset height, use min-height instead. What about that one:

h2 {
  font: 18px/30px Arial;
}

.content {
  width: 200px;
  min-height: 100px;
  border: 1px solid #000;
  background: yellow;
}
<div class="content">
<h2>Text</h2>
<p>Example Example</p>  
</div>   
<br>
<div class="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>

Edit: if you want to keep the height of the container and show scrollable content, use overflow: auto;

h2 {
  font: 18px/30px Arial;
}

.content {
  width: 200px;
  height: 100px;
  overflow: auto;
  border: 1px solid #000;
  background: yellow;
}
<div class="content">
<h2>Text</h2>
<p>Example Example</p>  
</div>   
<br>
<div class="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>

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>

Try this:

$('#content').height($('#content')[0].scrollHeight)

I would use height:auto like this;

#content {
    height : auto;
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论