【问题标题】:Truncate text in flex item截断弹性项目中的文本
【发布时间】:2019-10-14 09:42:03
【问题描述】:

我有一个左右两侧的标题。左侧包含一些我想保留在一行中的文本,如果需要,用省略号截断。但是,当我将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;
}
<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>

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

【问题讨论】:

    标签: html css flexbox ellipsis


    【解决方案1】:

    您需要通过添加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>

    相关:

    Why don't flex items shrink past content size?(了解min-width

    Why is a flex-child limited to parent size?(了解flex-shrink

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多