默认主轴X轴水平向右 y轴垂直向下
属性值描述row默认从左到右row-reverse从右到左column从上到下column-reverse从下到上注意:使用属性之前先确定好主轴是x还是y
默认子元素不换行,装不开子元素会缩小子元素的大小放到父元素内。
属性值描述nowrap默认不换行 (大小可能会改变)wrap换行属性是 flex-direction 和 flex-wrap 属性的复合属性 flex-flow:row wrap; /*x主轴,换行 */
定义子项目分配剩余空间,用flex来表示占多少份
.item { flex: <number>; /* 默认值 0 */ } <style> section { display: flex; width: 60%; height: 150px; background-color: pink; margin: 0 auto; } section div:nth-child(1) { width: 100px; flex:1; height: 150px; background-color: red; } section div:nth-child(2) { flex: 1; background-color: green; } section div:nth-child(3) { width: 100px; height: 150px; background-color: blue; } </style> <body> <section> <div></div> <div></div> <div></div> </section> </body>总结:align-items是控制素有的子项,而align-self是控制单个子项
<style> div { display: flex; width: 80%; height: 300px; background-color: pink; /* 让三个子盒子沿着侧轴底侧对齐 */ /* align-items: flex-end; */ } div span { width: 150px; height: 100px; background-color: purple; margin-right: 5px; } div span:nth-child(3) { /* 我们想只让3号盒子下来底侧 */ align-self: flex-end; } </style> </head> <body> <div> <span>1</span> <span>2</span> <span>3</span> </div> </body>数值越小,排列越靠前,默认为0。 注意:和 z-index 不一样。
<style> div { display: flex; width: 80%; height: 300px; background-color: pink; } div span { width: 150px; height: 100px; background-color: purple; margin-right: 5px; } div span:nth-child(2) { /* 默认是0 -1比0小所以在前面 */ order: -1; } div span:nth-child(3) { align-self: flex-end; } </style> </head> <body> <div> <span>1</span> <span>2</span> <span>3</span> </div> </body>