【问题标题】:text-overflow ellipsis not working in nested flexbox文本溢出省略号在嵌套的弹性框中不起作用
【发布时间】:2017-02-11 19:53:59
【问题描述】:

我有一个使用 flexbox 创建的两列布局。

在右栏中,我有两行,第一行包含标题,第二行包含页面内容。

在标题中我有三列、一个按钮、一些文本和一个按钮。

我希望按钮位于列的左侧和右侧,文本占用任何额外的空间。

最后,我希望文本具有white-space:nowraptext-overflow:ellipsis 属性来截断长标题。

我的问题是这样的:我无法让文本换行在嵌套在另一个 flexbox 中的 flexbox 中正常工作,如下小提琴所示:

http://jsfiddle.net/maxmumford/rb4sk3mz/3/

.container {
  display: flex;
  height: 100vh;
}
.left {
  width: 200px;
  background-color: yellow;
}
.right {
  flex: 1;
  background-color: blue;
  color: white;
}
.header {
  background: red;
  color: white;
  display: flex;
  flex-direction: row;
}
.header .content {
  white-space: nowrap;
  flex: 0 1 auto;
  text-overflow: ellipsis;
  overflow: hidden;
  min-width: 0;
}
.header .buttons {
  background: green;
  flex: 1 0 auto;
}
.header .content:hover {
  white-space: normal;
}
<div class="container">

  <div class="left">
    content left
  </div>
  <div class="right">
    <div class="header">
      <div class="buttons">buttons</div>
      <div class="content">
        This content is really long and should wrap with ellipses, but for some reason it doesn't work when it's nested in a container with display: flex
      </div>
      <div class="buttons">buttons</div>
    </div>
    content right
  </div>

</div>

但是,当标头未嵌套在弹性框中时,完全相同的代码可以工作:

http://jsfiddle.net/maxmumford/p7115f2v/1/

.header {
  background: red;
  color: white;
  display: flex;
  flex-direction: row;
}
.header .content {
  white-space: nowrap;
  flex: 0 1 auto;
  text-overflow: ellipsis;
  overflow: hidden;
  min-width: 0;
}
.header .buttons {
  background: green;
  flex: 1 0 auto;
}
<div class="header">
  <div class="buttons">buttons</div>
  <div class="content">
    This content is really long and is wrapped correctly... This content is really long and is wrapped correctly... This content is really long and is wrapped correctly... This content is really long and is wrapped correctly... This content is really long
    and is wrapped correctly... This content is really long and is wrapped correctly... This content is really long and is wrapped correctly...
  </div>
  <div class="buttons">buttons</div>
</div>

我怎样才能在第一小提琴中实现我想要的?

谢谢

【问题讨论】:

    标签: html css flexbox ellipsis


    【解决方案1】:

    您的代码中有两个问题导致省略号无法正常工作:

    1. div.right 包含div.header,而div.header 又包含按钮和文本。

      div.right 是主容器中的弹性项目 (.container)。

      默认情况下,a flex item cannot be smaller than the size of its content。弹性项目的初始设置是min-width: auto

      这意味着您的文本长度(不换行)将为父弹性项目设置最小尺寸。使弹性项目缩小其内容的一种方法是将弹性项目设置为min-width: 0

      您已经为 .content flex 项目完成了此操作,但还需要在 .right 项目上进行设置。

    2. 您已将 .content 元素设置为 flex: 0 1 auto

      这告诉弹性项目使用内容的大小 (flex-basis: auto)。文本设置大小。

      相反,将宽度设置为 0 并让弹性容器根据需要分配空间。 You can do this with flex: 1 1 0, which is the same as flex: 1.

    .container {
      display: flex;
      height: 100vh;
    }
    .left {
      width: 200px;
      background-color: yellow;
    }
    .right {
      flex: 1;
      background-color: blue;
      color: white;
      min-width: 0;             /* NEW */
    }
    .header {
      background: red;
      color: white;
      display: flex;
      flex-direction: row;
    }
    .header .content {
      white-space: nowrap;
      flex: 1;                  /* ADJUSTMENT */
      text-overflow: ellipsis;
      overflow: hidden;
      min-width: 0;
    }
    .header .buttons {
      background: green;
      flex: 1 0 auto;
    }
    .header .content:hover {
      white-space: normal;
    }
    <div class="container">
    
      <div class="left">
        content left
      </div>
      <div class="right">
        <div class="header">
          <div class="buttons">buttons</div>
          <div class="content">
            This content is really long and should wrap with ellipses, but for some reason it doesn't work when it's nested in a container with display: flex
          </div>
          <div class="buttons">buttons</div>
        </div>
        content right
      </div>
    
    </div>

    revised fiddle

    【讨论】:

    • 如果父对象的大小由 CSS 计算,则可能还需要将其设置为 min-width: 0px
    • 如果你有一个深度嵌套的 flexbox 结构(远在树上的父级也有flex: 1,例如侧边栏布局),你可能需要在这些父级上设置min-width: 0。经过大量调试后才知道这一点。
    【解决方案2】:

    的值:

    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    

    只有当你的元素的内容超过它的宽度时才会起作用。

    可以设置.header .content的宽度:

    .header .content {
        width: 50%;
    }
    

    它会按照您的预期工作:
    http://jsfiddle.net/t82pqks4/

    【讨论】:

    • 谢谢,但这会使标题容器超出页面宽度,导致水平滚动条
    • 您问题中的两个 jsfiddles 非常不同(悬停/左菜单/等)。你能解释一下你到底在找什么吗?从你的问题看不太清楚。
    【解决方案3】:

    我希望这个解决方案能帮助到别人,因为我的页面是一个巨大的 flexbox 嵌套组合,我发现没有其他方法可以工作。所以 min-width 0 没有用,除了这个,我没有试过。

    在包含要包装的副本的 flex 列中,您需要执行此操作。

    .row {
      display: flex;
    }
    
    .col1 {
     position: relative;
     flex: 1 1 70%;
     overflow: hidden;
    }
    
    .nested {
      width: 100%;
      position: absolute;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    
    .col2 {
      flex: 1 1 30%;
      padding-left: 1rem;
    }
    <div class="row">
      <div class="col1">
         <div class="nested">This is my really long copy that will push it out of the screen, and will not respect the elippse no matter what I do!!!</div>
      </div>
      <div class="col2">
        Last Text
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-19
      • 2016-07-15
      • 1970-01-01
      • 2019-06-19
      • 2021-12-17
      • 1970-01-01
      相关资源
      最近更新 更多