html - 如何使用 flexbox 显示具有不同规则的列?

标签 html css flexbox

段落元素中有 3 列:

.foo {
  display: flex;
  align-items: center;
}
.column {
  border: 3px solid blue;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
<p class="foo">
  <span class="first column">first</span>
  <span class="second column">second</span>
  <span class="third column">third</span>
</p>

规则是:

  • 3 列应该只显示在一行中,并且每一列都可以有很大的字符。
  • 第一列最大宽度为 70%
  • 第二列最大宽度为 50px
  • 如果第一列和第二列占整行,第三列可以不显示

不知道怎么用flex来实现。有人可以给我一些解决方案吗?

最佳答案

.foo {
    display: flex;
}

.first  { flex: 0 1 70%; }        /* can't grow, can shrink, max width 70% */

.second { flex: 0 1 50px; }       /* can't grow, can shrink, max width 50px */

.column {
    border: 1px solid blue;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}
<p class="foo">
    <span class="first column">first first first first first first first first first first first first first first first first first first first</span>
    <span class="second column">second second second second second second second second second </span>
    <span class="third column">third third third third third third</span>
</p>

关于html - 如何使用 flexbox 显示具有不同规则的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39187591/

相关文章:

html - CSS定位在元素的底部而不使用绝对

css - 显示 flex 子元素以自动调整大小以适合父元素

javascript - 在 MacOS 上滚动的替代方法

php - 在 Symfony2 中访问 HTML 表单数据

javascript - 通过单击将 css 类附加到多个 div

css - 我可以使用下划线开始 CSS 选择器 ID 或类吗?

javascript - Position Fixed - 设置宽度等于容器

html - chrome v 32 - html 元素大小问题

jquery get 方法在 safari 中工作但在 firefox 中不起作用

css - flex-shrink 不会在 IE 10/11 中缩小元素并导致父级比父级宽