结合文本溢出:省略号与display:flex

时间:2017-08-03 16:59:30

标签: html css css3 flexbox

我希望使用display: flex;进行居中,同时在文本溢出时出现...溢出。似乎只要我引入display: flex,省略号就会停止工作。关于如何将这两者结合起来的任何建议? JSFiddle:https://jsfiddle.net/silverwind/sw78h02b/1/



.target {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  background: red;
  margin: 2rem;
  display: flex; /* remove this to get ellipsis to show */
}

<div class="target">
  Text here is very very long that it will overflow the width of the containing element. Adding another sentence to make sure it will overflow.
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:5)

将您的文字放在span内,并使用display: flex对内容进行父级和文字溢出。

.target {
  background: red;
  margin: 2rem;
  display: flex; 
}
span {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
<div class="target">
  <span>Text here is very very long that it will overflow the width of the containing element. Adding another sentence to make sure it will overflow.</span>
</div>