html - 文本溢出 : ellipsis causes flex item to overflow flex container

标签 html css flexbox ellipsis

<分区>

我在 flex 容器中有两列。其中一列(橙色列)包含一个头像,其宽度应恰好为容器的 10%。

另一列(蓝色列)包含面板,面板的标题可能很长,因此我应该通过 css 将其截断。这张图片显示了我的期望:

我的期望:

What I expect

但是当我尝试执行 text-overflow: ellipsis 时,我的第二行变得比 flex 容器更宽。这是此行为的示例:

https://jsfiddle.net/8krtL9ev/1/

.container {
  background: #ddd;
  width: 400px;
  height: 200px;
  padding: 10px;
  display: flex;
}

.row1 {
  background: red;
  flex-basis: 10%;
  flex-shrink: 0;
}

.row2 {
  background: blue;
  flex-basis: 90%;
  margin-left: 10px;
  padding: 10px;
  display: flex;
}

.panel {
  background: green;
  flex-basis: 50%;
  overflow: hidden;
}

.panel-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
<div class="container">

  <div class="row1">
  </div>

  <div class="row2">
    <div class="panel">
      <div class="panel-title">
        <span>Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum.</span>
      </div>
    </div>
  </div>
</div>

我该如何解决这个问题?

最佳答案

使用 width 代替 flex-basis。因为在 this Documentation他们提到

9.9.3. Flex Item Intrinsic Size Contributions

The main-size min-content/max-content contribution of a flex item is its outer min-content/max-content size, clamped by its flex base size as a maximum (if it is not growable) and/or as a minimum (if it is not shrinkable), and then further clamped by its min/max main size properties.

不要忘记将宽度 calc(90% - 30px) 设置为 row2 宽度

.container {
  background: #ddd;
  width: 400px;
  height: 200px;
  padding: 10px;
  display: flex;
}

.row1 {
    background: red;
    /* flex-basis: 10%; */
    width: 10%;
    flex-shrink: 0;
}

.row2 {
    background: blue;
    /* flex-basis: 90%; */
    width: calc(90% - 30px);
    margin-left: 10px;
    padding: 10px;
    display: flex;
}

.panel {
  background: green;
  flex-basis: 50%;
  overflow: hidden;
}

.panel-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
<div class="container">

  <div class="row1">
  </div>

  <div class="row2">
    <div class="panel">
      <div class="panel-title">
        <span>Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum.</span>
      </div>
    </div>
  </div>
</div>

关于html - 文本溢出 : ellipsis causes flex item to overflow flex container,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41286912/

相关文章:

javascript - 在node.js中从mysql接收数据绘制图形

php - 如何以不同于其他帖子的方式显示第一篇帖子?

html - 图片上的文字百分比 html css

jquery - HTML5/CSS 根据其他列的相互高度对齐列表项

html - 使用 flex 在右侧显示 Logo 中心和菜单图标

html - C# 从站点获取完整的 html 文档

html - ul 子弹未对准

jquery - 加载窗口时 SVG 绘制动画进度

javascript - 在按钮外单击时隐藏下拉列表

html - 在不破坏子元素的情况下将 div 拆分为多列