当前位置:   article > 正文

Css提高——flex布局及其相关属性

Css提高——flex布局及其相关属性

目录:

1、传统布局与flex布局的区别

2、flex的布局原理

 3、flex常见的父项属性

3.1、flex-direction :设置主轴的方向

3.2、justify-content 设置主轴上的子元素排列方式

3.3、flex-wrap 设置子元素是否换行

3.4、align-items 设置侧轴上的子元素排列方式(单行 )

3.5、align-content 设置侧轴上的子元素的排列方式(多行)

         3.6、flex-flow(flex-direction 和 flex-wrap 属性的复合属性

 4、flex常见的子项属性 

4.1、flex

4.2、align-self 控制子项自己在侧轴上的排列方式


1、传统布局与flex布局的区别

2、flex的布局原理

flex 是 flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性,任何一个容器都可以 指定为 flex 布局。

  • 当我们为父盒子设为 flex 布局以后,子元素的 float、clear 和 vertical-align 属性将失效。
  • 伸缩布局 = 弹性布局 = 伸缩盒布局 = 弹性盒布局 =flex布局

 3、flex常见的父项属性

  • flex-direction:设置主轴的方向
  • justify-content:设置主轴上的子元素排列方式
  • flex-wrap:设置子元素是否换行
  • align-content:设置侧轴上的子元素的排列方式(多行)
  • align-items:设置侧轴上的子元素排列方式(单行)
  • flex-flow:复合属性,相当于同时设置了 flex-direction 和 flex-wrap

3.1、flex-direction :设置主轴的方向

1、主轴和侧轴的概念

2、flex-direction的属性值

3、例子

如果我想要将flex的子元素从右往左排列的话,则需要对父元素添加flex-direction: row-reverse;属性

1、代码:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .main {
  9. width: 800px;
  10. height: 600px;
  11. margin: 0 auto;
  12. border-radius: 12px;
  13. background-color: skyblue;
  14. display: flex;
  15. flex-direction: row-reverse;
  16. }
  17. span {
  18. width: 200px;
  19. margin-left: 10px;
  20. height: 100%;
  21. border-radius: 12px;
  22. background-color: pink;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div class="main">
  28. <span>1</span>
  29. <span>2</span>
  30. <span>3</span>
  31. </div>
  32. </body>
  33. </html>
2、 效果图

3.2、justify-content 设置主轴上的子元素排列方式

1、justify-content 的属性值:

2、例子:
2、代码: 
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style>
  9. div {
  10. display: flex;
  11. width: 800px;
  12. height: 400px;
  13. background-color: pink;
  14. /* 我们现在的主轴是y轴 */
  15. flex-direction: column;
  16. /* justify-content: center; */
  17. justify-content: space-between;
  18. }
  19. div span {
  20. width: 150px;
  21. height: 100px;
  22. background-color: purple;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div>
  28. <span>1</span>
  29. <span>2</span>
  30. <span>3</span>
  31. </div>
  32. </body>
  33. </html>
3、效果图:

3.3、flex-wrap 设置子元素是否换行

1、flex-wrap 属性值

2、例子
1、代码
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style>
  9. div {
  10. display: flex;
  11. width: 600px;
  12. height: 400px;
  13. background-color: pink;
  14. /* flex布局中,默认的子元素是不换行的, 如果装不开,会缩小子元素的宽度,放到父元素里面 */
  15. /* flex-wrap: nowrap; */
  16. flex-wrap: wrap;
  17. }
  18. div span {
  19. width: 150px;
  20. height: 100px;
  21. background-color: purple;
  22. color: #fff;
  23. margin: 10px;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div>
  29. <span>1</span>
  30. <span>2</span>
  31. <span>3</span>
  32. <span>4</span>
  33. <span>5</span>
  34. </div>
  35. </body>
  36. </html>
2、效果图

3.4、align-items 设置侧轴上的子元素排列方式(单行 )

1、align-items 属性值

2、例子
1、代码:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style>
  9. div {
  10. display: flex;
  11. width: 800px;
  12. height: 400px;
  13. background-color: pink;
  14. /* 默认的主轴是 x 轴 row */
  15. flex-direction: column;
  16. justify-content: center;
  17. /* 我们需要一个侧轴居中 */
  18. /* 拉伸,但是子盒子不要给高度 */
  19. /* align-items: stretch; */
  20. align-items: center;
  21. /* align-content: center; */
  22. }
  23. div span {
  24. width: 150px;
  25. height: 100px;
  26. background-color: purple;
  27. color: #fff;
  28. margin: 10px;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div>
  34. <span>1</span>
  35. <span>2</span>
  36. <span>3</span>
  37. </div>
  38. </body>
  39. </html>
2、效果图:

3.5、align-content 设置侧轴上的子元素的排列方式(多行)

1、align-content属性值

2、例子
1、代码
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style>
  9. div {
  10. display: flex;
  11. width: 800px;
  12. height: 400px;
  13. background-color: pink;
  14. /* 换行 */
  15. flex-wrap: wrap;
  16. /* 因为有了换行,此时我们侧轴上控制子元素的对齐方式我们用 align-content */
  17. /* align-content: flex-start; */
  18. /* align-content: center; */
  19. /* align-content: space-between; */
  20. align-content: space-around;
  21. }
  22. div span {
  23. width: 150px;
  24. height: 100px;
  25. background-color: purple;
  26. color: #fff;
  27. margin: 10px;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div>
  33. <span>1</span>
  34. <span>2</span>
  35. <span>3</span>
  36. <span>4</span>
  37. <span>5</span>
  38. <span>6</span>
  39. </div>
  40. </body>
  41. </html>
2、效果图:

3.6、flex-flow(flex-direction 和 flex-wrap 属性的复合属性)

1、flex-flow 属性值

 4、flex常见的子项属性 

4.1、flex

1、flex属性

2、例子
 1、代码:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .main {
  9. width: 800px;
  10. height: 400px;
  11. background-color: skyblue;
  12. margin: 0 auto;
  13. border-radius: 12px;
  14. display: flex;
  15. }
  16. span {
  17. flex: 1;
  18. background-color: green;
  19. height: 100px;
  20. border-radius: 12px;
  21. margin-left: 10px;
  22. }
  23. .main span:nth-child(2) {
  24. flex: 2;
  25. background-color: blue;
  26. height: 100px;
  27. border-radius: 12px;
  28. margin-left: 10px;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div class="main">
  34. <span>1</span>
  35. <span>2</span>
  36. <span>3</span>
  37. </div>
  38. </body>
  39. </html>
2、效果图

4.2、align-self 控制子项自己在侧轴上的排列方式&

1、定义

align-self

order:

2、例子:
1、代码:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .main {
  9. width: 90%;
  10. height: 400px;
  11. background-color: skyblue;
  12. margin: 0 auto;
  13. border-radius: 12px;
  14. display: flex;
  15. flex-direction: row;
  16. }
  17. span {
  18. flex: 1;
  19. background-color: green;
  20. height: 100px;
  21. border-radius: 12px;
  22. margin-left: 10px;
  23. }
  24. .main span:nth-child(2) {
  25. flex: 2;
  26. background-color: blue;
  27. height: 100px;
  28. border-radius: 12px;
  29. align-self: flex-end;
  30. margin-left: 10px;
  31. }
  32. .main span:nth-child(3) {
  33. height: 100px;
  34. border-radius: 12px;
  35. order: -1;
  36. margin-left: 10px;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div class="main">
  42. <span>1</span>
  43. <span>2</span>
  44. <span>3</span>
  45. </div>
  46. </body>
  47. </html>
2、效果图:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/241856
推荐阅读
相关标签
  

闽ICP备14008679号