如何在Flex布局中处理长文本的换行问题?

17
我需要制作一个响应式网页,使父元素的宽度是动态的。有两个弹性项目,一个是长的(动态的),另一个是短的(静态的)。
我希望结果看起来像第二行,长文本被打断(或重叠时隐藏),而短文本始终正确显示。
我尝试使用flex-shrink: 0,但似乎总会出现溢出。
在这种情况下,如何消除溢出?
我确实需要弹性布局,并且不应涉及js。

.parent {
  display: flex;
  flex-direction: row;
  width: 15rem;
  background: yellowgreen;
  padding: 10px;
  overflow: hidden;
}
.flex-item {
  width: 10em;
  padding: 10px;
  background: yellow;
  flex: 1 1 50%;
}
.block1 {
  background: red;
}
.block2 {
  background: orange;
}
.nos {
  flex-shrink: 0 !important;
}
<div class="parent">
  <div class="flex-item">
    <div class="block1">
      longlonglonglonglonglonglonglonglonglonglonglonglonglong
    </div>
  </div>
  <div class="flex-item nos">
    <div class="block2">
      Display
    </div>
  </div>
</div>
<br/>
<div class="parent">
  <div class="flex-item">
    <div class="block1">
      longlonglonglonglong...
    </div>
  </div>
  <div class="flex-item">
    <div class="block2">
      Display
    </div>
  </div>
</div>

https://jsfiddle.net/templefox/Lw3hhz8j/


我现在也在编写一个 flex-display,为什么我不能只使用 <br> 来换行呢? - Penguin9
3个回答

11
只需要将 word-break:break-all 属性添加到其父级 div 中即可。点击此JSFiddle链接可以实时查看效果。

2

Something like this.

*{
  box-sizing:border-box;
}
.parent {
    display: flex;
    flex-direction: row;
    width: 16rem;
    background: yellowgreen;
    padding: 10px;
    overflow: hidden;
    position: relative;
}
.flex-item {
    width: 100%;
    padding: 10px;
    background: yellow;
    flex: 1 1;
}
.block1 {
  background: red;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-right:70px;
}
.block2 {
  background: orange;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nos {
    flex-shrink: 0 !important;
    position: absolute;
    right: 10px;
    max-width: 70px;
}
<div class="parent">
  <div class="flex-item">
    <div class="block1">
      longlonglonglonglonglonglonglonglonglonglonglonglonglong
    </div>
  </div>
  <div class="flex-item nos">
    <div class="block2">
      Display
    </div>
  </div>
</div>


2

.parent {
   display: flex;
   width: 15rem;
   padding: 10px;
   background: yellowgreen;
   /* overflow: hidden       <-- not necessary at this point */
   /* flex-direction: row    <-- default value; can be omitted */
}
.flex-item {
   /* width: 10em            <-- not necessary at this point */
   /* flex: 1 1 50%          <-- not necessary at this point */
   padding: 10px;
   background: yellow;
   display: flex;            /* new */
   min-width: 0;             /* see note #1 */

}
.block1 {
   width: 10em;
   overflow: hidden;         /* see note #2 */
   text-overflow: ellipsis;  /* see note #2 */
   white-space: nowrap;      /* see note #2 */
   background: red;
}
.block2 {
  background: orange;
}

/* .nos { flex-shrink: 0 !important; } */
<div class="parent">
  <div class="flex-item">
    <div class="block1">longlonglonglonglonglonglonglonglonglonglonglonglonglong</div>
  </div>
  <div class="flex-item">
    <div class="block2">Display</div>
  </div>
</div>
<br/>
<div class="parent">
  <div class="flex-item">
    <div class="block1">longlonglonglonglong...</div>
  </div>
  <div class="flex-item">
    <div class="block2">Display</div>
  </div>
</div>

修订版的代码

注:

  1. 为什么 flex 项目不能缩小到内容大小以下?
  2. 如何在多行文本上应用省略号?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接