[英]CSS Div height not expanding to fit content or wrapping content
I am having problems getting the content in a div (or its value) to wrap around inside and having the div's height adjust to the contents. 我在将div中的内容(或其值)包装到内部并使div的高度适应内容时遇到问题。
The top one a container, message-box. 最上面的是一个容器,消息框。 There are three divs inside like in the picture attached.
如图所示,里面有三个div。 I need to have divs each-message and each-message-content adjust its height to fit the contents inside.
我需要让divs每个消息和每个消息内容调整其高度以适合内部的内容。 I have looked at many posts in this site and tried many combinations of overflow:hidden and height:auto, but they mostly end up making the each-message-content scroll sideways, and am at wits end.
我查看了该站点上的许多帖子,并尝试了许多overflow:hidden和height:auto的组合,但是它们大多最终使每个消息内容的滚动向侧面倾斜,并且很不明智。
How can I achieve this? 我该如何实现?
**** Updated with HTML ***** ****更新为HTML *****
<div className="message-box">
<div className="each-message-box">
<div className="each-message">
<div className="each-message-date">Date</div>
<div className="each-message-content">ContentContentContentContentContentContentContentContentContentContent</div>
</div>
</div>
</div>
</div>
.each-message-box {
width: 100%;
display: block;
overflow: auto;
height: auto;
margin: 1px;
}
.each-message {
width: 270px;
height: 100px;
margin: 2px;
border: 1px solid #ccc;
overflow: hidden;
height: auto;
}
.each-message-date {
border: 1px solid #ccc;
font-size: 10px;
color: #ccc;
text-align: left;
}
.each-message-content {
width: 100%;
border: 1px solid #ccc;
text-align: left;
border-radius: 10px;
height: auto;
}
Not very elegant to break words like this, but if that's what's needed. 打破这样的措词不是很优雅,但是如果需要的话。 btw apart from
className
, there's an extra in your HTML. 顺便说一句,除了
className
之外,您的HTML中还有其他内容。
EDIT: Ignore the comment about className
- react project comment added after this answer was posted. 编辑:忽略有关
className
的评论-发布此答案后,添加添加的项目评论。
.each-message-box { width: 100%; display: block; overflow: auto; height: auto; margin: 1px; } .each-message { width: 270px; height: 100px; margin: 2px; border: 1px solid #ccc; overflow: hidden; height: auto; word-break: break-all; } .each-message-date { border: 1px solid #ccc; font-size: 10px; color: #ccc; text-align: left; } .each-message-content { width: 100%; border: 1px solid #ccc; text-align: left; border-radius: 10px; height: auto; }
<div class="message-box"> <div class="each-message-box"> <div class="each-message"> <div class="each-message-date">Date</div> <div class="each-message-content">ContentContentContentContentContentContentContentContentContentContent </div> </div> </div> </div>
创建一个函数来实时记录如果输入了char的数字,并使用javascript相应地更改容器的宽度。
if your CSS div height not expanding to fit content or wrapping content. 如果您的CSS div高度未扩展为适合内容或包装内容。 Just use a simple trick.
只需使用一个简单的技巧。
before the closing tag of the message-box div add a div with class cl <div class="message-box"><div class="cl"></div></div>
在消息框div的结束标记之前,添加一个cl
<div class="message-box"><div class="cl"></div></div>
的div <div class="message-box"><div class="cl"></div></div>
Now in your Css Give this style .cl{clear:both;}
现在在您的CSS中给此样式
.cl{clear:both;}
by using this if you have height auto on the div it will still wrap the content.I hope it will work for you. 如果您在div上具有height auto,则使用此功能仍然可以包装内容。希望它对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.