我有一个div
,它是一个包装器,还有一个子div
。我正在使用flexbox
将它们放在彼此之下。现在,孩子divs有一个文本。文本与align-items: center;
垂直对齐。子div的宽度取决于它的文本。当文本与包装器一样长时,div应该停止生长并且内部的文本应该被剪切掉。为此,我尝试使用text-overflow: ellipsis;
。它没有按预期工作。我知道我可以在display: inline-block;
的情况下使用flexbox
,但我想使用flexbox
将文字与text-overflow: ellipsis;
结合使用,但似乎无法与我合作例。目前,具有长文本的项目与包装器重叠,并且也没有省略号。该项目具有固定高度。
我在这个主题中找到的唯一答案是这个,但答案对我没有帮助:Ellipsis in flexbox container
希望它足够清楚。
.wrapper {
display: flex;
flex-direction: column;
background-color: lightcoral;
padding: 10px;
flex-wrap: wrap;
}
.wrapper__item {
min-width: 0;
flex-basis: 50%;
align-self: flex-start;
flex-direction: row;
width: auto;
max-width: 100%;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
padding: 5px 10px;
border: 1px solid black;
border-radius: 5px;
margin-bottom: 10px;
background-color: white;
height: 50px;
}
<div class="wrapper">
<div class="wrapper__item">Proin eget tortor risus. Cras ultricies ligula sed magna dictum porta. Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec rutrum congue leo eget malesuada. Vivamus suscipit
tortor eget felis porttitor volutpat. Nulla porttitor accumsan tincidunt. Curabitur aliquet quam id dui posuere blandit. Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus. Donec rutrum congue leo eget malesuada. Curabitur arcu
erat, accumsan id imperdiet et, porttitor at sem. Mauris blandit aliquet elit, eget tincidunt nibh pulvinar a. Nulla porttitor accumsan tincidunt. Cras ultricies ligula sed magna dictum porta.</div>
<div class="wrapper__item">Proin eget tortor ri.</div>
<div class="wrapper__item">Proin eget tortor ri.Proin eget tortor ri.</div>
</div>
答案 0 :(得分:3)
text-overflow: ellipsis
无法使用Flex容器(display: flex
)。
如果您将文本包装在span
中,那么
注1:父溢出是由宽度+填充引起的,box-sizing: border-box
将解决这个问题。
注2:要使align-items: center
产生效果,该项目需要一个高度,此处为50px。
Stack snippet
.wrapper {
display: flex;
flex-direction: column;
background-color: lightcoral;
padding: 10px;
}
.wrapper__item {
align-self: flex-start;
max-width: 100%;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
padding: 5px 10px;
border: 1px solid black;
border-radius: 5px;
margin-bottom: 10px;
background-color: white;
height: 50px;
}
.wrapper__item span {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
&#13;
<div class="wrapper">
<div class="wrapper__item"><span>Proin eget tortor risus. Cras ultricies ligula sed magna dictum porta. Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec rutrum congue leo eget malesuada. Vivamus suscipit
tortor eget felis porttitor volutpat. Nulla porttitor accumsan tincidunt. Curabitur aliquet quam id dui posuere blandit. Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus. Donec rutrum congue leo eget malesuada. Curabitur arcu
erat, accumsan id imperdiet et, porttitor at sem. Mauris blandit aliquet elit, eget tincidunt nibh pulvinar a. Nulla porttitor accumsan tincidunt. Cras ultricies ligula sed magna dictum porta.</span></div>
<div class="wrapper__item">Proin eget tortor ri.</div>
<div class="wrapper__item">Proin eget tortor ri.Proin eget tortor ri.</div>
</div>
&#13;
答案 1 :(得分:1)
无需在display:flex
课程中使用.wrapper__item
,因为text-overflow
在flex
行中无效。
对于height
,您可以使用line-height
,因为您的文字在一行中......或者您可以同时使用height
和line-height
Stack Snippet
body {
margin: 0;
}
.wrapper {
display: flex;
flex-direction: column;
background-color: lightcoral;
padding: 10px;
}
.wrapper__item {
align-self: flex-start;
flex-direction: row;
width: auto;
max-width: 100%;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
padding: 0px 10px;
border: 1px solid black;
border-radius: 5px;
margin-bottom: 10px;
background-color: white;
box-sizing: border-box;
line-height:50px;
}
<div class="wrapper">
<div class="wrapper__item">Proin eget tortor risus. Cras ultricies ligula sed magna dictum porta. Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec rutrum congue leo eget malesuada. Vivamus suscipit
tortor eget felis porttitor volutpat. Nulla porttitor accumsan tincidunt. Curabitur aliquet quam id dui posuere blandit. Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus. Donec rutrum congue leo eget malesuada. Curabitur arcu
erat, accumsan id imperdiet et, porttitor at sem. Mauris blandit aliquet elit, eget tincidunt nibh pulvinar a. Nulla porttitor accumsan tincidunt. Cras ultricies ligula sed magna dictum porta.</div>
<div class="wrapper__item">Proin eget tortor ri.</div>
<div class="wrapper__item">Proin eget tortor ri.Proin eget tortor ri.</div>
</div>