简体   繁体   English

内部div上的jQuery append(),同时扩展外部div,以使内部div元素适合内部

[英]Jquery append() on inner div while expanding the outer div so that the inner div elements fits inside

I have div structure similar to this. 我有与此类似的div结构。 I'm using jquery to append elements inside the div with the id append. 我正在使用jquery将id内的元素附加到div中。 However, the outer div doesn't expand automatically to fit the appended elements. 但是,外部div不会自动扩展以适合附加的元素。

<div class="well" id='expand'>
    <div class="container">
        <!-- some other divs -->
    </div>
    <div id= "append">
    </div>
</div

I solved the issue by increasing the height of the outer div ie 我通过增加外部div的高度(即

 var div_height = $('#expand').height();
 $('#expand').css({"height":div_height + append_height})

Is there a better way to do this? 有一个更好的方法吗?

You could do it with pure CSS using min-height and height:auto which causes the expand DIV to adjust its height according to its content: 您可以使用min-heightheight:auto使用纯CSS来执行此操作,这将导致expand DIV根据其内容调整其高度:

#expand{
    height:auto;
    min-height:50px;
    height:auto !important;  /* for IE as it does not support min-height */
}

Also add height:auto !important; 还添加height:auto !important; to the child element for mobile support. 子元素以获得移动支持。

 .parent { display: flex; border: 2px solid #222; flex-direction: row; padding: 10px; } .parent .child { padding-right: 10px; flex-grow: 1; width: 33%; } .parent .child:last-child { padding-right: 0; } .parent .child .content { border: 1px solid blue; height: 100%; } .parent .child:first-child .content { border: 1px solid red; } 
 <div class="parent"> <div class="child"> <div class="content"> blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah </div> </div> <div class="child"> <div class="content">Div 2</div> </div> <div class="child"> <div class="content">Div 3</div> </div> </div> 

This can be done by 这可以通过

#expand{
height: auto;
clear: both;
}

Clear property erases any float values (left or right) within the div. Clear属性会清除div中的所有浮点值(左或右)。 This prevents the expansion of width of the outer div due to float property of inner div. 这样可以防止由于内部div的float属性而导致外部div的宽度扩展。 If you have any issue with the responsiveness, you could use some simple HTML : 如果您对响应性有任何疑问,可以使用一些简单的HTML:

<div class="well" id='expand'>
    <div class="container">
        <div><!-- content --></div>
        <br>
        <div><!-- content --></div>
    </div>
    <div id= "append">
    </div>
</div>

Adding line break tag ensures that the div moves to next line across all devices. 添加换行标签可确保div跨所有设备移至下一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM