容器属性

flex布局适合一维布局, 线性排列
image.png

主轴和交叉轴

flex-direction

flex弹性盒子内分主轴和交叉轴,每个轴都有起点和终点
改变轴的方向
display: flex;
flex-direction:

  • row
  • row-reverse
  • column
  • column-reverse

image.pngimage.pngimage.pngimage.png

换行和缩写

flex-wrap

盒子内的元素如果不写width和height, 则width由内容决定,height撑满(stretch)整个盒子
image.png
使用flex-wrap: wrapflex-wrap: wrap-reverse换行
盒子会根据子元素的多少平居分成N等分,元素都排在每部分的最上面
image.pngimage.png

flex-flow

flex-flow是flex-direction和flex-wrap的缩写

  1. flex-flow: column wrap;

主轴对齐

justify-content

  • flex-start 沿主轴start位置对齐
  • flex-end 沿主轴end位置对齐
  • center 沿主轴居中对齐
  • space-around 子元素两侧空间相等
  • space-between子元素之间空间相等
  • space-evenly 空间平均分配

    1. display: flex;
    2. flex-wrap: wrap;
    3. flex-direction: column-reverse;

    image.pngimage.pngimage.pngimage.pngimage.pngimage.png

    交叉轴对齐

    align-content

    align-content 交叉轴整体方向上的对齐,必须配合flex-wrap: wrap才能生效

  • stretch 默认值

  • flex-start
  • flex-end
  • center
  • space-around
  • space-between
  • space-evenly

注意:align-content 只对多行有效,单行无效

  1. display: flex;
  2. flex-wrap: wrap;
  3. flex-direction: column-reverse;

image.pngimage.pngimage.png
image.pngimage.pngimage.png

align-items

align-items指定交叉轴方向每一行内的对齐方式
如果交叉轴方向只有一行,那么align-items比align-content用的更多

  • stretch 默认
  • flex-start
  • flex-end
  • center 相当于align-content: space-around
  • baseline 与小写字母x对齐
    1. display: flex;
    2. flex-wrap: wrap;
    3. flex-direction: column-reverse;
    image.pngimage.pngimage.pngimage.png

    应用

    内联与块的上下左右居中布局

    文字居中

    image.png
    line-height
    单行文字可以用line-height
    1. .box {
    2. background: skyblue;
    3. width: 500px;
    4. height: 500px;
    5. line-height: 500px;
    6. text-align: center;
    7. }
    display: table-cell;
    多行文字
    1. .box {
    2. display: table-cell;
    3. vertical-align: middle;
    4. }
    display: flex
    1. .box {
    2. display: flex;
    3. align-items: center;
    4. }

    块居中

    image.png
    方法1:
    1. .box {
    2. display: flex;
    3. justify-content: center;
    4. align-items: center;
    5. }
    方法2:display: flex; +margin: auto; 触发BFC
    ```css .box { display: flex; }

.child { margin: auto; }

  1. <a name="bUY5C"></a>
  2. ##### 方法3:
  3. ```css
  4. .box {
  5. position: relative
  6. }
  7. .child {
  8. position: absolute;
  9. left: 0;
  10. right: 0;
  11. top: 0;
  12. bottom: 0;
  13. margin: auto;
  14. }

方法4:
  1. .box {
  2. position: relative
  3. }
  4. .child {
  5. position: absolute;
  6. left: 50%;
  7. top: 50%;
  8. transform: translate(-50%, -50%)
  9. }

不定项居中布局

轮播图或分页中间的圆点按钮数量不确定
image.png

flex实现

  1. <div class="box">
  2. <div class="child"></div>
  3. <div class="child"></div>
  4. <div class="child"></div>
  5. <div class="child"></div>
  6. </div>
  1. .box {
  2. background: skyblue;
  3. width: 500px;
  4. height: 100px;
  5. display: flex;
  6. justify-content: center;
  7. align-items: flex-end;
  8. }
  9. .box > div {
  10. width: 20px;
  11. height: 20px;
  12. background: pink;
  13. border-radius: 50%;
  14. margin: 10px;
  15. }

传统方式实现

  1. <div class="box">
  2. <section>
  3. <div class="child"></div>
  4. <div class="child"></div>
  5. <div class="child"></div>
  6. <div class="child"></div>
  7. </section>
  8. </div>
  1. .box {
  2. background: skyblue;
  3. width: 500px;
  4. height: 100px;
  5. position: relative;
  6. }
  7. section {
  8. position: absolute;
  9. bottom: 0;
  10. text-align: center;
  11. width: 100%;
  12. }
  13. section > div {
  14. width: 20px;
  15. height: 20px;
  16. background: pink;
  17. border-radius: 50%;
  18. margin: 10px;
  19. display: inline-block;
  20. }

均分列布局

image.png

flex实现

  1. <div class="box">
  2. <div class="child"></div>
  3. <div class="child"></div>
  4. <div class="child"></div>
  5. <div class="child"></div>
  6. </div>
  1. .box {
  2. box-sizing: border-box;
  3. background: skyblue;
  4. height: 100px;
  5. display: flex;
  6. justify-content: space-between;
  7. align-items: flex-end;
  8. padding: 0 20px;
  9. }
  10. .box > div {
  11. width: 20px;
  12. height: 20px;
  13. border-radius: 50%;
  14. background: pink;
  15. }

float实现

用float时,父组件必须固定宽度,margin要计算出确定的值,可以看出用浮动要麻烦很多,还有局限性

  1. <div class="box">
  2. <section>
  3. <div class="child"></div>
  4. <div class="child"></div>
  5. <div class="child"></div>
  6. <div class="child"></div>
  7. </section>
  8. </div>
  1. * { box-sizing: border-box;}
  2. .box {
  3. background: skyblue;
  4. width: 320px;
  5. height: 100px;
  6. overflow: hidden;
  7. padding: 0 20px;
  8. position: relative;
  9. }
  10. section {
  11. width: 400px;
  12. position: absolute;
  13. bottom: 0;
  14. }
  15. section::after {
  16. content: '';
  17. display: block;
  18. clear: both;
  19. }
  20. section > div {
  21. width: 20px;
  22. height: 20px;
  23. border-radius: 50%;
  24. background: pink;
  25. float: left;
  26. margin-right: 65px;
  27. }

子项分组布局

使用margin:auto让margin自适应
image.png

  1. <div class="box">
  2. <div class="child"></div>
  3. <div class="child"></div>
  4. <div class="child"></div>
  5. <div class="child"></div>
  6. <div class="child"></div>
  7. <div class="child"></div>
  8. <div class="child"></div>
  9. </div>
  1. .box {
  2. background: skyblue;
  3. width: 500px;
  4. height: 100px;
  5. display: flex;
  6. align-items: center;
  7. }
  8. .box > div {
  9. width: 20px;
  10. height: 60px;
  11. background: pink;
  12. margin-right: 10px;
  13. }
  14. .child:nth-of-type(5) {
  15. margin-right: auto;
  16. }
  17. .child:nth-of-type(3) {
  18. margin-right: auto;
  19. }