嵌套的flexbox项中的截断文本

时间:2017-01-20 13:18:07

标签: css css3 flexbox ellipsis truncated

我想要在flexbox项目中截断文本。这完全没问题。但是,如果此flexbox项目包含在另一个flexbox项目中,则会中断。有没有办法让一切变得流畅而不会爆发?

我已经制作了一支笔,你看到它在这里打破了:http://codepen.io/anon/pen/apJYaN

.accordeon {
  width: 300px;
  background-color: #eeeeee;
}

.accordeon__row {
  /* if you remove this, then it works */
  display: flex;
}

.accordeon__row-label {
  flex-basis: 30%;
}

.accordeon__row-content {
  list-style-type: none;
  padding-left: 0;
  margin-bottom: 0;
  margin-top: 0;
}

.accordeon__row-item {
  display: flex;
}

.accordeon__row-value {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
<div class="accordeon">
  <div class="accordeon__row">
    <div class="accordeon__row-label">
      Mobility
    </div>
    <ul class="accordeon__row-content">
      <li class="accordeon__row-item">
        <div class="accordeon__row-value">
          1 min
        </div>
        <div class="accordeon__row-value">
          Supermarket
        </div>
      </li>
      <li class="accordeon__row-item">
        <div class="accordeon__row-value">
          1 min
        </div>
        <div class="accordeon__row-value">
          Supermarket
        </div>
      </li>
    </ul>
  </div>
  <div class="accordeon__row">
    <div class="accordeon__row-label">
      Mobility
    </div>
    <ul class="accordeon__row-content">
      <li class="accordeon__row-item">
        <div class="accordeon__row-value">
          1 min
        </div>
        <div class="accordeon__row-value">
          Supermarket
        </div>
      </li>
      <li class="accordeon__row-item">
        <div class="accordeon__row-value">
          1 min
        </div>
        <div class="accordeon__row-value">
          Supermarket
        </div>
      </li>
    </ul>
  </div>
  <div class="accordeon__row">
    <div class="accordeon__row-label">
      Schools
    </div>
    <ul class="accordeon__row-content">
      <li class="accordeon__row-item">
        <div class="accordeon__row-value">
          1 min
        </div>
        <div class="accordeon__row-value">
          Akademisches Gymnasium Beethovenplatz 1
        </div>
      </li>
      <li class="accordeon__row-item">
        <div class="accordeon__row-value">
          1 min
        </div>
        <div class="accordeon__row-value">
          Supermarket
        </div>
      </li>
    </ul>
  </div>
</div>

2 个答案:

答案 0 :(得分:4)

您可以添加以下行:

.accordeon__row-content {
  ...
  min-width: 0; /* https://www.w3.org/TR/css-flexbox-1/#min-size-auto */
}

并调整此行:

.accordeon__row-label {
  flex: 0 0 30%; /* fixed width flex item, previously flex-basis: 30%; */
}

<强> codepen

答案 1 :(得分:0)

不确定您想要它的样子,但是您可以通过使用网格布局而不是flexbox来实现它。所以没有什么突破父容器。

以下是如何完成网格布局:

.col-1 {width: 8.33%;}
.col-2 {width: 16.66%;}
.col-3 {width: 25%;}
.col-4 {width: 33.33%;}
.col-5 {width: 41.66%;}
.col-6 {width: 50%;}
.col-7 {width: 58.33%;}
.col-8 {width: 66.66%;}
.col-9 {width: 75%;}
.col-10 {width: 83.33%;}
.col-11 {width: 91.66%;}
.col-12 {width: 100%;}

[class*="col-"] {
    float: left;
    padding: 0;

}

.row {
    width: 300px;
    margin: 0 auto;

}

.row::after {
    content: "";
    clear: both;
    display: block;
}

然后给你的每个accordeon__行行类,并给你的每个accordeon__row-item col-12类。 这样,您可以确保该项始终包含在父行中。 您可以使用为项目分配不同的col-n类。

https://jsfiddle.net/alabianc/755vgz29/1/这会以网格布局显示您的代码。根据屏幕宽度,更容易更改布局。