截断弹性项目中的文本

Mar*_*ode 1 html css ellipsis flexbox

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

.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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

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

Tem*_*fif 7

您需要通过添加来禁用默认的最小宽度,并通过添加和添加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 */
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

有关的:

为什么弹性项目不会缩小到超过内容大小?(为了理解min-width

为什么 Flex-Child 受限于父级大小?(为了理解flex-shrink