【问题标题】:Prioritize boxes inside flexbox对 flexbox 中的盒子进行优先级排序
【发布时间】:2019-01-07 02:38:01
【问题描述】:

我在 flexbox 中有三个 divs。我希望它们显示为内联。但是如果第一个 div 中的文本太多,我想截断这个文本,以确保我总是能看到第二个和第三个 div 的文本。

HTML:

<div class="flex">
  <div class="flex-1 item">
   !!!Scale me down If I am too big for the screen!!! Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
  </div>
  <div class="flex-2 item">Always show me. Dont let flex-1 push me outside</div>
  <div class="flex-3 item">Always show me. Dont let flex-1 push me outside</div>
</div>

CSS:

.flex {
  display: flex;
}

.item {
  margin: 2px;
  border: 1px solid black;
  height: 2em;
  white-space: nowrap;
}

.flex-1 {
  flex: 1 1 5em;
}

.flex-2 {
  flex: 1 1 5em;
}

.flex-3 {
  flex: 1 1 5em;
}

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

【问题讨论】:

    标签: css responsive-design flexbox


    【解决方案1】:

    我推荐使用 Sass/Scss 来使用 DRY 原则。使用混入

    Scss

    @mixin trunc-text {
       white-space: nowrap;
       overflow: hidden;
       text-overflow: ellipsis;
    }
    

    在你的课堂上

     @for $i from 1 through 3 {
      .flex-#{$i} { 
        @include trunc-text;
          @if $i == 1 {
            flex: 5 1 5rem
          } @else {
            flex: 1 1 5rem;
          }
      }
    }
    

    一开始这可能会吓到你,但它非常强大。

    Css中的输出

    .flex-1 {
      flex: 1 1 5em;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    
    .flex-2 {
      flex: 1 1 5em;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    
    .flex-3 {
      flex: 1 1 5em;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      相关资源
      最近更新 更多