gpt4 book ai didi

html - Flexbox:如何根据子项中的文本量制作包含 div 的比例?

转载 作者:太空宇宙 更新时间:2023-11-03 19:26:40 25 4
gpt4 key购买 nike

我正在开发一个聊天应用程序,想使用 flexbox 来设置聊天消息的样式。但是,当我使用 display: flexflex-direction: row 获取一行中的所有 div 时,消息的文本部分会溢出。如何获得父 div(类 message)的大小以随 message-text div 中的文本量缩放?

.message {
margin-top: 1%;
border-top: solid 1px lightgrey;
padding: 1%;
}

.message-icon {
width: 60px;
height: 60px;
background: red;
margin-right: 1%;
}

.message-name {
width: 50px;
}

.message-text {
width: 100%;
min-height: 20px;
padding: 2px;
border-radius: 2%;
display: inline-block;
}
<div class='message'>
<div class='message-icon'></div>
<div class='message-name'>Steve:</div>
<div class='message-text'>THIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS
IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS
IS A MESSAGETHIS IS A MESSAGE</div>
</div>

最佳答案

你在容器 .message 上使用 flex,每个第一层的 child 都在那里 flex .message-*

如果您不熟悉 flex ,我强烈建议您阅读 this

.message {
margin-top: 1%;
border: solid 1px lightgrey;
padding: 1%;
display: flex; /* added */
flex-direction: row; /* added */
}

.message-icon {
width: 60px;
height: 60px;
background: red;
margin-right: 1%;
}

.message-name {
width: 50px;
}

.message-text {
width: 100%;
min-height: 20px;
padding: 2px;
border-radius: 2%;
display: inline-block; /* you dont need this, since its flexing anyways */
}
<div class='message'>
<div class='message-icon'></div>
<div class='message-name'>Steve:</div>
<div class='message-text'>THIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS
IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS IS A MESSAGETHIS
IS A MESSAGETHIS IS A MESSAGE</div>
</div>

关于html - Flexbox:如何根据子项中的文本量制作包含 div 的比例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51460679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com