【问题标题】:Prevent a child element from overflowing its parent in flexbox [duplicate]防止子元素在flexbox中溢出其父元素[重复]
【发布时间】:2017-10-04 05:03:49
【问题描述】:

我正在开发一个显示大型卡片网格的网络应用程序,其高度本质上是可变的。

为了美观,我们使用 jQuery 的 .matchHeight() 来平衡每行中卡片的高度。

它的性能不能很好地扩展,所以今天我一直在迁移到一个基于弹性盒的解决方案,它的速度要快得多。

但是,我丢失了一个行为 - 如果卡片标题的内容不合适,应该用省略号截断。

目标:

  1. 3 列
  2. 列宽因父项而异
  3. 列间距不变
  4. 行内高度均等

如何安排尊重容器大小和尊重text-overflow: ellipsis;white-space: nowrap;

(没有 jQuery 标签,因为我们正在远离它)

我的解决方案是目前的形式,除了截断之外,它实现了我的所有目标:

https://codepen.io/anon/pen/QvqZYY

#container { 
                display: flex; 
                flex-direction: row; 
                flex-wrap: wrap; 
                
                justify-content: flex-start;    /* Bias cards to stack from left edge       */ 
                align-items: stretch;           /* Within a row, all cards the same height  */ 
                
                border: thin solid gray; 
            } 
            .card-wrapper { 
                width: 33.33%; 
                display: flex; 
                background: #e0e0ff; 
            } 
            .card { 
                flex-grow: 1; 
                margin: 7px; 
                display: flex; 
                flex-direction: column; 
                border: thin solid gray; 
                background: #e0ffff; 
            } 
            .card div { 
                border: thin solid gray; 
            } 
            .card div:nth-child(1) { 
                white-space: nowrap; 
                text-overflow: ellipsis; 
            } 
            .card div:nth-child(2) { 
                flex-grow: 2; 
            } 
<div id="container"> 
            <div class="card-wrapper"> 
                <div class="card"> 
                    <div>Title</div> 
                    <div>Multiline<br/>Body</div> 
                    <div>Footer</div> 
                </div> 
            </div> 
            <div class="card-wrapper"><div class="card"><div>Really long rambling title that pushes beyond the bounds of the container, unless your screen is really, really wide</div><div>Body</div><div>Footer</div></div></div> 
            <div class="card-wrapper"><div class="card"><div>Title</div><div>Body</div><div>Footer</div></div></div> 
            <div class="card-wrapper"><div class="card"><div>Title</div><div>Body</div><div>Footer</div></div></div> 
            <div class="card-wrapper"><div class="card"><div>Title</div><div>Body</div><div>Footer</div></div></div> 
        </div> 

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    弹性项目的初始设置是min-width: auto。这意味着默认情况下,弹性项目不能小于其内容的大小。

    因此,text-overflow: ellipsis 无法工作,因为弹性项目只会扩展,而不是允许溢出。 (出于同样的原因,滚动条也不会呈现。)

    要覆盖此行为,请使用 min-width: 0overflow: hiddenMore details.

    #container {
      display: flex;
      flex-wrap: wrap;
      border: thin solid gray;
    }
    
    .card-wrapper {
      width: 33.33%;
      display: flex;
      background: #e0e0ff;
    }
    
    .card {
      flex-grow: 1;
      margin: 7px;
      display: flex;
      flex-direction: column;
      border: thin solid gray;
      background: #e0ffff;
      overflow: hidden;        /* NEW */
    }
    
    .card div {
      border: thin solid gray;
    }
    
    .card div:nth-child(1) {
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;        /* NEW */
    }
    
    .card div:nth-child(2) {
      flex-grow: 2;
    }
    <div id="container">
      <div class="card-wrapper">
        <div class="card">
          <div>Title</div>
          <div>Multiline<br/>Body</div>
          <div>Footer</div>
        </div>
      </div>
      <div class="card-wrapper">
        <div class="card">
          <div>Really long rambling title that pushes beyond the bounds of the container, unless your screen is really, really wide</div>
          <div>Body</div>
          <div>Footer</div>
        </div>
      </div>
      <div class="card-wrapper">
        <div class="card">
          <div>Title</div>
          <div>Body</div>
          <div>Footer</div>
        </div>
      </div>
      <div class="card-wrapper">
        <div class="card">
          <div>Title</div>
          <div>Body</div>
          <div>Footer</div>
        </div>
      </div>
      <div class="card-wrapper">
        <div class="card">
          <div>Title</div>
          <div>Body</div>
          <div>Footer</div>
        </div>
      </div>
    </div>

    【讨论】:

    • 就 flex 最小尺寸问题而言,没有理由选择另一个。但是一般的 CSS 是有区别的。 min-width: 0 将允许项目缩小,但内容可能会明显溢出 (you can test this in your codepen) ...
    • overflow: hidden 实现了相同的最小尺寸目标,但溢出内容被隐藏。另外,the overflow property is necessary for text-overflow: ellipsis to work.
    • 感谢您的澄清。 overflow: hidden; 就是这样。
    • min-width: 0 修复了我的类似问题。我已将max-width: x 放在我的容器上,因此文本溢出将起作用,但这会将其他项目推出容器响应。 min-width: 0 阻止了 :) 谢谢!
    • 谢谢你。被困了几个小时.. :)
    猜你喜欢
    • 2012-06-13
    • 1970-01-01
    • 2013-05-13
    • 2013-08-22
    • 2018-04-12
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多