文本溢出:中间跨度上的省略号

text-overflow: ellipsis on middle span

我有一个这样的酒吧:

<div class="traffic-info-bar" ng-if="hasTrafficInfo" ng-click="openTrafficInfoModal()">
    <span class="icon ion-alert-circled"></span>
    <span class="traffic-info-main-text">This is a very long placeholder text</span>
    <span class="traffic-info-read-more">Read more</span>
</div>

与CSS:

.traffic-info-bar {
  text-transform: uppercase;
  font-weight: bold;
  color:#fff;
  text-align:center;
  background-color: #007aff;
  height: 40px;
  padding-top: 12px;
}

.traffic-info-main-text {
  overflow-x: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.traffic-info-read-more {
  font-size: 10px;
  text-decoration: underline;
}

这是在小屏幕 (iPhone 5) 上的结果:

如你所见,你几乎可以看到蓝色条底部的"READ MORE"文字。这是我想要的示例。

谁能看到我如何解决这个问题?

Flexbox 可以做到这一点。

.traffic-info-bar {
  text-transform: uppercase;
  font-weight: bold;
  color:#fff;
  text-align:center;
  background-color: #007aff;
  height: 40px;
  padding-top: 12px;
  display: flex;
  justify-content: center;
  align-items: baseline;
}


.traffic-info-main-text {
  overflow-x: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.traffic-info-read-more {
  font-size: 10px;
  text-decoration: underline;
    white-space: nowrap;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet"/>
<div class="traffic-info-bar" ng-if="hasTrafficInfo" ng-click="openTrafficInfoModal()">
  <span class="icon ion-alert-circled"></span>
  <span class="traffic-info-main-text">This is a very long placeholder text</span>
  <span class="traffic-info-read-more">Read more</span>
</div>

Codepen Demo

我理解 "flexbox can do that" 的炒作,但您完全可以在不使用 flexbox 的情况下做到这一点。它更简单,只是内联块和块元素的问题。由于您使用的是跨度,默认情况下它是一个内联块,您需要将它包装在一个容器中,该容器是一个块并具有定义的宽度。

在尝试 flexbox 之前,了解这两者之间的区别很重要。


这里是 jsfiddle


这是代码片段:

.traffic-info-bar {
  text-transform: uppercase;
  font-weight: bold;
  color: #fff;
  text-align: center;
  background-color: #007aff;
  height: 40px;
  padding-top: 12px;
}

.traffic-info-main-text__container {
  float: left;
  width: 80%;
  overflow-x: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border: 1px solid pink;
  box-sizing: border-box;
  padding: 0 10px;
}

.traffic-info-main-text {
  overflow-x: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.traffic-info-read-more__container {
  float: left;
  width: 20%;
  border: 1px solid yellow;
  box-sizing: border-box;
}

.traffic-info-read-more {
  font-size: 10px;
  text-decoration: underline;
}
<div class="traffic-info-bar" ng-if="hasTrafficInfo" ng-click="openTrafficInfoModal()">
  <div class="traffic-info-main-text__container">
    <span class="icon ion-alert-circled"></span>
    <span class="traffic-info-main-text">This is a very long placeholder text</span>
  </div>
  <div class="traffic-info-read-more__container">
    <span class="traffic-info-read-more ellipses">Read more</span>
  </div>
</div>