gpt4 book ai didi

html - 文本在嵌套的 flexbox 容器中溢出

转载 作者:行者123 更新时间:2023-11-28 01:05:02 25 4
gpt4 key购买 nike

我试图让第一个“消息标题”的内容在它到达绿色容器的边缘时被截断。相反,它在外面溢出。我认为这可能是 Flexbox 的一个怪癖——有人试图做类似的事情吗?

在帖子底部添加了相关的codepen。

标记

<div class="container">
<div class="message">
<div class="message-content">
<div class="message-header">
Header
</div>
<div class="message-body">
<div class="message-body__left">
AV
</div>
<div class="message-body__right">
<div class="message-main">
<div class="message-type">

</div>
<div class="message-details">
<div class="message-title">
Test string Test string Test string Test string Test string Test string Test string Test string
</div>
<div class="message-subtitle">
Blah blah
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="message">
<div class="message-content">
<div class="message-header">
Header
</div>
<div class="message-body">
<div class="message-body__left">
AV
</div>
<div class="message-body__right">
<div class="message-main">
<div class="message-type">

</div>
<div class="message-details">
<div class="message-title">
Test string Test string
</div>
<div class="message-subtitle">
Blah blah
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="message">
<div class="message-content">
<div class="message-header">
Header
</div>
<div class="message-body">
<div class="message-body__left">
AV
</div>
<div class="message-body__right">
<div class="message-main">
<div class="message-type">

</div>
<div class="message-details">
<div class="message-title">
Test string
</div>
<div class="message-subtitle">
Blah blah
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

CSS

* {
min-width: 0;
box-sizing: border-box;
}

.container {
display: flex;
flex-direction: column;
padding: 1.5rem;
max-width: 400px;
background: red;
}

.message {
display: flex;
flex-direction: column;
flex-shrink: 0;
margin-bottom: 2rem;
}

.message-content {
display: flex;
flex-direction: column;
margin-right: 20%;
max-width: 80%;
align-items: flex-start;
background: green;
}

.message-header {
display: flex;
}

.message-body {
display: flex;
}

.message-body__left {
display: inline-flex;
flex-shrink: 0;
margin-right: 0.5rem;
background: teal;
display: none;
}

.message-body__right {
position: relative;
background: #eff2f5;
border-radius: 3px;
word-wrap: break-word;
white-space: normal;
}

.message-main {
display: flex;
}

.message-type {
position: relative;
width: 3rem;
height: 3rem;
display: flex;
flex-shrink: 0;
background: blue;
}

.message-details {
flex: 1;
display: flex;
flex-direction: column;
}

.message-title {
padding-bottom: 0.25rem;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

.message-subtitle {
display: flex;
justify-content: space-between;
}

此处相关代码笔 - https://codepen.io/anon/pen/VGgQdL

最佳答案

white-space: nowrap in .message-title 选择器导致了这个问题。

编辑 1.

.message-content 中删除 display: flex 是有效的,因为对于 overflow: hidden; text-overflow: ellipsis 工作父容器应该有一个固定的宽度。使用 .message-content { display: flex; } 启用它不会发生,因为宽度是流动的。

如果您确实需要在父级上保留 display: flex,您可以尝试手动添加固定宽度 - .message-title: { max-width: 234px; }宽度:234px。它的工作原理是一样的,除了它在小屏幕尺寸时会中断。

* {
min-width: 0;
box-sizing: border-box;
}

.container {
display: flex;
flex-direction: column;
padding: 1.5rem;
max-width: 400px;
background: red;
}

.message {
display: flex;
flex-direction: column;
flex-shrink: 0;
margin-bottom: 2rem;
}

.message-content {
display: flex;
flex-direction: column;
margin-right: 20%;
max-width: 80%;
align-items: flex-start;
background: green;
}

.message-header {
display: flex;
}

.message-body {
display: flex;
}

.message-body__left {
display: inline-flex;
flex-shrink: 0;
margin-right: 0.5rem;
background: teal;
display: none;
}

.message-body__right {
position: relative;
background: #eff2f5;
border-radius: 3px;
word-wrap: break-word;
white-space: normal;
}

.message-main {
display: flex;
}

.message-type {
position: relative;
width: 3rem;
height: 3rem;
display: flex;
flex-shrink: 0;
background: blue;
}

.message-details {
flex: 1;
display: flex;
flex-direction: column;
}

.message-title {
padding-bottom: 0.25rem;
/* white-space: nowrap; */
overflow: hidden;
text-overflow: ellipsis;
}

.message-subtitle {
display: flex;
justify-content: space-between;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<title>Cover</title>
</head>
<body>
<div class="container">
<div class="message">
<div class="message-content">
<div class="message-header">
Header
</div>
<div class="message-body">
<div class="message-body__left">
AV
</div>
<div class="message-body__right">
<div class="message-main">
<div class="message-type">

</div>
<div class="message-details">
<div class="message-title">
Test string Test string Test string Test string Test string Test string Test string Test string
</div>
<div class="message-subtitle">
Blah blah
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="message">
<div class="message-content">
<div class="message-header">
Header
</div>
<div class="message-body">
<div class="message-body__left">
AV
</div>
<div class="message-body__right">
<div class="message-main">
<div class="message-type">

</div>
<div class="message-details">
<div class="message-title">
Test string Test string
</div>
<div class="message-subtitle">
Blah blah
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="message">
<div class="message-content">
<div class="message-header">
Header
</div>
<div class="message-body">
<div class="message-body__left">
AV
</div>
<div class="message-body__right">
<div class="message-main">
<div class="message-type">

</div>
<div class="message-details">
<div class="message-title">
Test string
</div>
<div class="message-subtitle">
Blah blah
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

关于html - 文本在嵌套的 flexbox 容器中溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52427879/

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