CSS flex-direction 属性
CSS flex-direction 属性指定了内部元素是如何在 flex 容器中布局的,定义了主轴的方向(正方向或反方向)。
实例
以相反的顺序设置 <div> 元素内的弹性项目的方向:
<!DOCTYPE html><html><head><style>#main {width: 400px;height: 400px;border: 1px solid #c3c3c3;display: flex;flex-direction: row-reverse;}#main div {width: 50px;height: 50px;}</style></head><body><h1>flex-direction 属性</h1><div id="main"><div style="background-color:coral;">A</div><div style="background-color:lightblue;">B</div><div style="background-color:khaki;">C</div><div style="background-color:pink;">D</div><div style="background-color:lightgrey;">E</div><div style="background-color:lightgreen;">F</div></div><p><b>注释:</b>Internet Explorer 10 以及更早的版本不支持 flex-direction 属性。</p></body></html>
定义和用法
flex-direction 属性规定弹性项目的方向。
注释:如果元素不是弹性项目,则 flex 属性无效。
| 默认值: | row |
|---|---|
| 继承: | 否 |
| 动画制作: | 不支持。请参阅:动画相关属性。 |
| 版本: | CSS3 |
| JavaScript 语法: | object.style.flexDirection="column-reverse" |
浏览器支持
表格中的数字注明了完全支持该属性的首个浏览器版本。
带 -webkit- 或 -moz- 的数字表示使用前缀的首个版本。
| 属性 | |||||
|---|---|---|---|---|---|
| flex-direction | 29.021.0 -webkit- | 11.0 | 28.018.0 -moz- | 9.06.1 -webkit- | 17.0 |
CSS 语法
flex-direction: row|row-reverse|column|column-reverse|initial|inherit;
属性值
| 值 | 描述 |
|---|---|
| row | 默认值。作为一行,水平地显示弹性项目。 |
| row-reverse | 等同行,但方向相反。 |
| column | 作为列,垂直地显示弹性项目。 |
| column-reverse | 等同列,但方向相反。 |
| initial | 将此属性设置为其默认值。参阅 initial。 |
| inherit | 从其父元素继承此属性。参阅 inherit。 |
更多实例
结合使用 flex-direction 和媒体查询为不同的屏幕尺寸/设备创建不同的布局:
<!DOCTYPE html><html><head><style>* {box-sizing: border-box;}.flex-container {display: flex;flex-direction: row;font-size: 30px;text-align: center;}.flex-item-left {background-color: #f1f1f1;padding: 10px;flex: 50%;}.flex-item-right {background-color: dodgerblue;padding: 10px;flex: 50%;}/* 响应式布局 - 制作一列布局而不是两列布局 */@media (max-width: 800px) {.flex-container {flex-direction: column;}}</style></head><body><h1>响应式弹性框</h1><p>"flex-direction: row;" 水平地并排弹性项目(从左到右)。</p><p>"flex-direction: column;" 垂直地堆叠弹性项目(从上到下)。</p><p><b>请调整浏览器窗口的大小,来查看小于或等于 800 像素时的方向改变。</b></p><div class="flex-container"><div class="flex-item-left">1</div><div class="flex-item-right">2</div></div></body></html>
相关页面
CSS3 教程: CSS3 弹性框
CSS 参考手册:flex-basis 属性
CSS 参考手册:flex-flow 属性
CSS 参考手册:flex-grow 属性
CSS 参考手册:flex-shrink 属性
CSS 参考手册:flex-wrap 属性