截断弹性项目中的文本

Truncate text in flex item

我有一个 header 有左边和右边。左侧包含一些我想保留在一行中的文本,如果需要用省略号截断。但是,当我将 white-space: nowrap 应用于它时,整个 header 就会超出其容器。这就是我的意思:

.container {
  width: 300px;
  height: 400px;
  border: 1px solid black;
}

.header {
  height: 80px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border: 1px solid red;
}

.header-right,
.header-left {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid blue;
}

.title {
  text-overflow: ellipsis;
  white-space: nowrap;
}

img {
  height: 20px;
  width: 20px;
  margin: 10px;
}
<div class="container">
  <div class="header">
    <div class="header-left">
      <img src="https://image.flaticon.com/icons/png/128/181/181548.png">
      <span class="title">Title: Keep me on a single line</span>
    </div>
    <div class="header-right">
      <img src="https://image.flaticon.com/icons/png/128/181/181548.png">
      <img src="https://image.flaticon.com/icons/png/128/181/181548.png">
      <img src="https://image.flaticon.com/icons/png/128/181/181548.png">
    </div>
  </div>
</div>

有谁知道如何将标题保留在一行但将其截断以使 header 不超出范围?

您需要通过添加 min-width:0 来禁用默认的最小宽度,并通过添加 flex-shrink:0; 和添加 overflow:hidden

来禁用图像及其容器的收缩

.container {
  width: 300px;
  height: 400px;
  border: 1px solid black;
}

.header {
  height: 80px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border: 1px solid red;
}

.header-right,
.header-left {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid blue;
  min-width:0; /* Added */
}

.title {
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow:hidden; /* Added */
}

img {
  height: 20px;
  width: 20px;
  margin: 10px;
  flex-shrink:0; /* Added */
}

.header-right {
  flex-shrink:0; /* Added */
}
<div class="container">
  <div class="header">
    <div class="header-left">
      <img src="https://image.flaticon.com/icons/png/128/181/181548.png">
      <span class="title">Title: Keep me on a single line</span>
    </div>
    <div class="header-right">
      <img src="https://image.flaticon.com/icons/png/128/181/181548.png">
      <img src="https://image.flaticon.com/icons/png/128/181/181548.png">
      <img src="https://image.flaticon.com/icons/png/128/181/181548.png">
    </div>
  </div>
</div>

相关:

(理解min-width

(理解flex-shrink