flex等于1的两个div里的文字怎么不影响两个div宽度的比例时 换行就省略
可以使用CSS的flex-shrink属性来控制元素的缩小比例。将两个div的flex-shrink属性都设置为0,可以禁止它们缩小。如果需要让文字换行并且省略多余部分,可以使用text-overflow和white-space属性来控制。
例如,以下代码会让两个等宽的div里的文字换行并省略多余部分,同时保持它们的宽度比例不变:
<div class="container">
<div class="box1">This is a long text that needs to wrap</div>
<div class="box2">This is some other text that also needs to wrap</div>
</div>
.container {
display: flex;
}
.box1, .box2 {
flex: 1;
flex-shrink: 0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
在上面的例子中,flex:1会让两个div平分剩余的空间,而flex-shrink:0则会防止它们缩小。white-space:nowrap会禁止文字换行,overflow:hidden会隐藏多余部分,text-overflow:ellipsis会在末尾显示省略号。这样,如果两个div里的文字太长,它们就会自动换行并显示省略号,而不会影响它们的宽度比例
原文地址: https://www.cveoy.top/t/topic/gZ9r 著作权归作者所有。请勿转载和采集!