在一行flexbox子上的省略号

时间:2016-03-18 02:42:57

标签: css flexbox ellipsis

我正在使用flexbox,我有一个孩子占用了剩余的宽度:

.flex-container {
  display: flex;
}
.middle {
  flex: 1;
}
.left {
  margin-right: 15px;
}
.right {
  margin-left: 15px;
}
.ellipsis {
  display: inline-block;
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
<div class="flex-container">
  <div class="left">
    Left content
  </div>

  <div class="middle">
    <div>Middle</div>
    <div class="ellipsis">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
      dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
    <span>12345</span>
  </div>

  <div class="right">
    <button class="btn btn-sm btn-danger">Button 1</button>
    <button class="btn btn-sm btn-default">Button 2</button>
  </div>
</div>

我希望Lorem ipsum行截断并显示省略号。考虑到“中间”元素是否会弯曲剩余宽度,这是否可能?

这是一个插件: https://plnkr.co/edit/GvyR280i00hnAKHu0keF?p=preview

1 个答案:

答案 0 :(得分:4)

你只需要在flex孩子身上overflow: hidden

.flex-container {
  display: flex;
}
.middle {
  flex-grow: 1;
  overflow: hidden;
}
.left {
  margin-right: 15px;
}
.right {
  margin-left: 15px;
}
.ellipsis {
  display: inline-block;
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
<div class="flex-container">
  <div class="left">
    Left content
  </div>

  <div class="middle">
    <div>Middle</div>
    <div class="ellipsis">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
      dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
    <span>12345</span>
  </div>

  <div class="right">
    <button class="btn btn-sm btn-danger">Button 1</button>
    <button class="btn btn-sm btn-default">Button 2</button>
  </div>
</div>