简体   繁体   English

如何调整div高度以根据动态生成的文本进行扩展?

[英]How to adjust div height to expand according to dynamically generated text?

I'm receiving a description for a product through an API call of a service. 我通过服务的API调用收到有关产品的说明。 This description, can be potentially to big to not fit on my div. 这种描述可能很大,不适合我的div。 This is my initial div: 这是我最初的div:

<div class="div-info">
  <h1 class="equipment-name">Title</h1>
  <h4 class="equipment-city-state">{{equipment.coordinates.city}} - {{equipment.coordinates.state}}</h4>
  <ul class="equipment-price">
    <li>Foo</li>
    <li>Bar</li>
    <li>Test</li>
  </ul>
  <p class="equipment-city-state">{{equipment.coordinates.city}} - {{equipment.coordinates.state}}</p>
  <p class="service-description" [@visibilityChanged]="visibility[i]">{{equipment.description}}</p>
</div>

And this is what looks like: 看起来像这样:

在此处输入图片说明

And when the user clicks on the 'Ler mais' button should display the text, and if not fit in div, should increase the div. 当用户单击“ Ler mais”按钮时,应显示文本,如果不适合div,则应增加div。 This is what looks like when the user clicks on the button 这是用户单击按钮时的样子

在此处输入图片说明

I would like the two first paragraphs Lorem would be entire displayed inside the div , even with the div height must be increased. 我希望Lorem的前两个段落将全部显示在div ,即使必须增加div的高度。

Just add this to your css so that the divInfo div will expand automatically to accommodate it's child elements: 只需将其添加到您的css中,以便divInfo div将自动扩展以容纳其子元素:

.divInfo {
    height: auto;
}

jsFiddle with dummy text: https://jsfiddle.net/AndrewL64/38rxe82m/ 带有伪文本的jsFiddle: https ://jsfiddle.net/AndrewL64/38rxe82m/

Try copy-pasting random long sentences or lists in the fiddle above and the divInfo will expand accordingly. 尝试在上面的小提琴中复制粘贴随机的长句子或列表,然后divInfo会相应地扩展。

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

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