html - 使 flex 元素从下到上

标签 html css flexbox

以下 HTML 和 CSS 创建了一个“条形图”。但是图表列从上到下增长。我怎样才能让它们从下往上生长?

* {
  box-sizing: border-box;
  font-size: 0;
  text-align: center;
  line-height: 50px;
}

.bar-chart {
  box-shadow: none;
  height: calc(50px * 3);
  width: calc(50px * 4);
}

.column {
  display: inline-flex;
  width: 100px;
  flex-direction: row;
  flex-wrap: wrap;
  height: 150px;
}

.cell {
  box-shadow: 0 0 0 1px rgb(0, 0, 0);
  width: 50px;
  height: 50px;
  font-size: 14pt;
}
<figure class="bar-chart">
  <div class="column">
    <div class="cell">one</div>
    <div class="cell">two</div>
  </div>
  <div class="column">
    <div class="cell">three</div>
    <div class="cell">four</div>
    <div class="cell">five</div>
    <div class="cell">six</div>
    <div class="cell">seven</div>
    <div class="cell">eight</div>
  </div>
</figure>

最佳答案

使用 flex-wrap: wrap-reverse 代替 flex-wrap: wrap

* {
  box-sizing: border-box;
  font-size: 0;
  text-align: center;
  line-height: 50px;
}

.bar-chart {
  box-shadow: none;
  height: calc(50px * 3);
  width: calc(50px * 4);
}

.column {
  display: inline-flex;
  width: 100px;
  flex-direction: row;
  flex-wrap: wrap-reverse;  /* ADJUSTED */
  height: 150px;
}

.cell {
  box-shadow: 0 0 0 1px rgb(0, 0, 0);
  width: 50px;
  height: 50px;
  font-size: 14pt;
}
<figure class="bar-chart">
  <div class="column">
    <div class="cell">one</div>
    <div class="cell">two</div>
  </div>
  <div class="column">
    <div class="cell">three</div>
    <div class="cell">four</div>
    <div class="cell">five</div>
    <div class="cell">six</div>
    <div class="cell">seven</div>
    <div class="cell">eight</div>
  </div>
</figure>


使用 wrap-reverse 可以切换交叉起点和交叉终点方向。

在行方向容器中,这是指顶部和底部(见下图)。

来自规范:

5.2. Flex Line Wrapping: the flex-wrap property

For the values that are not wrap-reverse, the cross-start direction is equivalent to either the inline-start or block-start direction of the current writing mode (whichever is in the cross axis) and the cross-end direction is the opposite direction of cross-start.

When flex-wrap is wrap-reverse, the cross-start and cross-end directions are swapped.

enter image description here

来源:W3C


在此处了解有关沿主轴 flex 对齐的更多信息:

在此处了解有关沿横轴 flex 对齐的更多信息:

关于html - 使 flex 元素从下到上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44749919/

相关文章:

html - 当最后一个单词后跟一个连字符时,对齐单行会在 IE 中的最后一个单词之前换行

捕获高分辨率手机而非低分辨率平板电脑的 CSS 媒体查询

css - Flexbox 拉伸(stretch) div 高度以填充父 div 的剩余高度

javascript - 如何在 target 和 currentTarget 之间切换元素的类?

html - 字体 (Roboto) 在 Chrome 上看起来比 Firefox (Windows) 更粗

javascript - 我想在值更改时验证下拉菜单,而不是在提交按钮单击时验证

css - 如何在 Bootstrap 中设置按钮网格?

html - 动画 SVG 图像

javascript - 创建一个 DIV 并给它一个 data-cart-info 属性。在 DIV 中,创建一个带有 mdc-typography--headline4"的标题作为其 CSS 类

html - CSS Flexbox - 使用 Flex Wrap 和 Flex Growth 属性时元素之间的间距