flex-direction

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 2015 年 9 月以来,该特性已在各大浏览器中可用。

flex-direction CSS 属性设置弹性项目在弹性容器中的排列方式,它定义了主轴和方向(正常或反向)。

试一试

flex-direction: row;
flex-direction: row-reverse;
flex-direction: column;
flex-direction: column-reverse;
<section class="default-example" id="default-example">
  <div class="transition-all" id="example-element">
    <div>Item One</div>
    <div>Item Two</div>
    <div>Item Three</div>
  </div>
</section>
#example-element {
  border: 1px solid #c5c5c5;
  width: 80%;
  display: flex;
}

#example-element > div {
  background-color: rgb(0 0 255 / 0.2);
  border: 3px solid blue;
  width: 60px;
  margin: 10px;
}

请注意,rowrow-reverse 的值受弹性容器方向性(directionality)的影响。如果其 dir 属性是 ltr,则 row 表示从左到右的水平轴,row-reverse 表示从右到左;如果 dir 属性是 rtl,则 row 表示从右到左的轴,row-reverse 表示从左到右。

语法

css
/* The direction text is laid out in a line */
flex-direction: row;

/* Like <row>, but reversed */
flex-direction: row-reverse;

/* The direction in which lines of text are stacked */
flex-direction: column;

/* Like <column>, but reversed */
flex-direction: column-reverse;

/* Global values */
flex-direction: inherit;
flex-direction: initial;
flex-direction: revert;
flex-direction: revert-layer;
flex-direction: unset;

接受以下值:

row

弹性容器的主轴被定义为与文本方向相同。主轴起点(main-start)主轴终点(main-end)与内容方向相同。

row-reverse

行为与 row 相同,但主轴起点(main-start)主轴终点(main-end)与内容方向相反。

column

弹性容器的主轴与块轴相同。主轴起点(main-start)主轴终点(main-end)与书写模式的之前(before)之后(after)点相同。

column-reverse

行为与 column 相同,但主轴起点(main-start)主轴终点(main-end)与内容方向相反。

无障碍

使用 flex-direction 属性,并将值设置为 row-reversecolumn-reverse 会在内容的视觉呈现和 DOM 顺序之间造成脱节。这将对使用屏幕阅读器等辅助技术导航的低视力用户产生不利影响。如果视觉(CSS)顺序很重要,那么屏幕阅读器用户将无法访问正确的阅读顺序。

正式定义

初始值row
应用于弹性容器
继承性
计算值同指定值
动画类型离散

正式语法

flex-direction = 
row |
row-reverse |
column |
column-reverse

示例

反转弹性容器的行和列

HTML

html
<h4>This is a Column-Reverse</h4>
<div id="col-rev" class="content">
  <div class="box red">A</div>
  <div class="box lightblue">B</div>
  <div class="box yellow">C</div>
</div>
<h4>This is a Row-Reverse</h4>
<div id="row-rev" class="content">
  <div class="box red">A</div>
  <div class="box lightblue">B</div>
  <div class="box yellow">C</div>
</div>

CSS

css
.content {
  width: 200px;
  height: 200px;
  border: 1px solid #c3c3c3;
  display: flex;
}

.box {
  width: 50px;
  height: 50px;
}

#col-rev {
  flex-direction: column-reverse;
}

#row-rev {
  flex-direction: row-reverse;
}

.red {
  background-color: red;
}

.lightblue {
  background-color: lightblue;
}

.yellow {
  background-color: yellow;
}

结果

规范

规范
CSS 弹性盒子布局模块第 1 级
# flex-direction-property

浏览器兼容性

另见