简体   繁体   English

CSS使用flex-direction列在flexbox父级中截断文本

[英]CSS Truncate text within a flexbox parent with flex-direction column

How can I truncate the text below? 如何截断下面的文字? The min-width: 0; 最小宽度:0; hack doesn't work when flex-direction is set to column. 当flex-direction设置为column时,hack无效。 Neither does this answer . 这个答案也没有。

 .parent { border: 1px solid red; display: flex; align-items: center; flex-direction: column; width: 300px; } .text { white-space: nowrap; min-width: 0; overflow: hidden; text-overflow: ellipsis; } 
 <div class="parent"> <img src="http://placekitten.com/g/200/200" /> <div class="text"> Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum </div> </div> 

When you give align-items: center the text overflows the parent and the width is not constrained and so ellipsis will not appear. 当您指定align-items: centertext溢出parent ,并且宽度不受限制 ,因此不会出现省略号

If you remove align-items: center it works because align-items: stretch is the default - but the image get stretched out now: 如果您删除align-items: center则因为align-items: stretch是默认设置-但是图像现在被拉伸了:

 .parent { border: 1px solid red; display: flex; /*align-items: center;*/ flex-direction: column; width: 300px; } .text { white-space: nowrap; min-width: 0; overflow: hidden; text-overflow: ellipsis; } 
 <div class="parent"> <img src="http://placekitten.com/g/200/200" /> <div class="text"> Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum </div> </div> 

Solution

Add width: 100% to the text - see demo below: text添加width: 100% -参见下面的演示:

 .parent { border: 1px solid red; display: flex; align-items: center; flex-direction: column; width: 300px; } .text { white-space: nowrap; min-width: 0; overflow: hidden; text-overflow: ellipsis; width: 100%; } 
 <div class="parent"> <img src="http://placekitten.com/g/200/200" /> <div class="text"> Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum </div> </div> 

The min-width: 0; 最小宽度:0; hack doesn't work when flex-direction is set to column. 当flex-direction设置为column时,hack无效。

Note that in min-width: 0 if for overriding the the flex item width as it is auto in the row direction (for column direction you use min-height ). 请注意,在min-width: 0如果要覆盖flex项目的宽度,则为min-width: 0 ,因为它在行方向是自动的(对于方向,则使用min-height )。 See example below for min-height: 0 used in a column flexbox : 请参见下面的示例以了解min-height: 0列flexbox中使用

Replace the display: flex; 更换display: flex; in Parent Class into display: block and add to img tag the following 在父类中display: block并将以下代码添加到img标签

 .parent { border: 1px solid red; display: block; align-items: center; width: 300px; } img{ display:block; margin:auto; } .text { white-space: nowrap; min-width: 0; overflow: hidden; text-overflow: ellipsis; margin: 0 auto; } 
 <div class="parent"> <img src="http://placekitten.com/g/200/200" /> <div class="text"> Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum </div> </div> 

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

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