flex布局中text-overflow失效的解決方案


在開發中我們經常會遇到這種布局,要求文字垂直居中,且超出使用省略號
3fb17a7a25bc88033926ae80fe85eae3.png
說到垂直居中,兼容性最好的就是flex布局,但在flex布局下出現了text-overflow失效的情況

實例代碼

<div class="wrapper"> <div class="flex item">hahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh</div> </div>
.flex{ display: flex; align-items: center; } .item{ height: 40px; background-color: bisque; overflow: hidden; text-overflow: ellipsis; }

出現了如下效果,我們可以看出over-flow屬性是生效的,而text-overflow卻失效了
b5bf47a78b8f7a50e6a92f05a882b02d.png

解決方案

方案一

在文本外面再多包裝一層div元素

<div class="wrapper"> <div class="flex item"> <div class="item-con">hahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh</div> </div> </div>
.flex{ display: flex; align-items: center; } .item{ height: 40px; background-color: bisque; } .item-con{ overflow: hidden; text-overflow: ellipsis; }

3fb17a7a25bc88033926ae80fe85eae3.png

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM