flex-direction CSS property sets how flex items are placed in the flex container defining the main axis and the direction (normal or reversed).

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

Note that the values row and row-reverse are affected by the directionality of the flex container. If its dir 属性为 ltr , row represents the horizontal axis oriented from the left to the right, and row-reverse from the right to the left; if the dir 属性为 rtl , row represents the axis oriented from the right to the left, and row-reverse from the left to the right.

句法

/* 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: unset;
					

The following values are accepted:

row
The flex container's main-axis is defined to be the same as the text direction. The main-start and main-end points are the same as the content direction.
row-reverse
Behaves the same as row but the main-start and main-end points are permuted.
column
The flex container's main-axis is the same as the block-axis. The main-start and main-end points are the same as the before and after points of the writing-mode.
column-reverse
Behaves the same as column but the main-start and main-end are permuted.

可访问性关注

使用 flex-direction property with values of row-reverse or column-reverse will create a disconnect between the visual presentation of content and DOM order. This will adversely affect users experiencing low vision navigating with the aid of assistive technology such as a screen reader. If the visual (CSS) order is important, then screen reader users will not have access to the correct reading order.

形式定义

初始值 row
适用于 flex containers
继承 no
计算值 如指定
动画类型 discrete

形式句法

row | row-reverse | column | column-reverse
					

范例

Reversing flex container columns and rows

HTML

<h4>This is a Column-Reverse</h4>
<div id="content">
  <div class="box" style="background-color:red;">A</div>
  <div class="box" style="background-color:lightblue;">B</div>
  <div class="box" style="background-color:yellow;">C</div>
</div>
<h4>This is a Row-Reverse</h4>
<div id="content1">
  <div class="box1" style="background-color:red;">A</div>
  <div class="box1" style="background-color:lightblue;">B</div>
  <div class="box1" style="background-color:yellow;">C</div>
</div>
					

CSS

#content {
  width: 200px;
  height: 200px;
  border: 1px solid #c3c3c3;
  display: flex;
  flex-direction: column-reverse;
}
.box {
  width: 50px;
  height: 50px;
}
#content1 {
  width: 200px;
  height: 200px;
  border: 1px solid #c3c3c3;
  display: flex;
  flex-direction: row-reverse;
}
.box1 {
  width: 50px;
  height: 50px;
}
					

结果

规范

规范 状态 注释
CSS Flexible Box Layout Module
The definition of 'flex-direction' in that specification.
候选推荐 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
flex-direction Chrome 完整支持 29 Edge 完整支持 12 Firefox 部分支持 20 注意事项
部分支持 20 注意事项
注意事项 Since Firefox 28, multi-line flexbox is supported.
注意事项 Does not support overflow when using *-reverse 。见 bug 1042151 for more info.
不支持 18 — 20 Disabled
Disabled From version 18 until version 20 (exclusive): this feature is behind the layout.css.flexbox.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
preference (needs to be set to ). To change preferences in Firefox, visit about:config.
IE 完整支持 11
完整支持 11
完整支持 10 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera 完整支持 12.1 Safari 完整支持 9 WebView Android 完整支持 4.4 Chrome Android 完整支持 29 Firefox Android 部分支持 20 注意事项
部分支持 20 注意事项
注意事项 Since Firefox 28, multi-line flexbox is supported.
注意事项 Does not support overflow when using *-reverse 。见 bug 1042151 for more info.
不支持 18 — 20 Disabled
Disabled From version 18 until version 20 (exclusive): this feature is behind the layout.css.flexbox.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
preference (needs to be set to ). To change preferences in Firefox, visit about:config.
Opera Android 完整支持 12.1 Safari iOS 完整支持 9 Samsung Internet Android 完整支持 2.0

图例

完整支持

完整支持

部分支持

部分支持

见实现注意事项。

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改: