博客列表 >flex容器属性的功能,参数,以及作用

flex容器属性的功能,参数,以及作用

Ghcᝰ
Ghcᝰ原创
2021年03月25日 19:53:29557浏览

什么是flex容器?

  • 把元素转为Flex布局,那么这个元素就被称为Flex容器,即弹性容器!

Flex四大属性的介绍

1:主轴方向与换行方式

flex-flow(主轴方向与换行方式)属性的用法:
  • 主轴方向水平排列且不换行:flex-flow: row nowrap
    默认值主轴水平不换行
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>弹性flex容器与项目</title>
  8. <style>
  9. * {
  10. box-sizing: border-box;
  11. margin: 0;
  12. padding: 0;
  13. }
  14. :root {
  15. font-size: 10px;
  16. }
  17. body {
  18. font-size: 1.6rem;
  19. }
  20. .container {
  21. display: flex;
  22. height: 20rem;
  23. border: 1px solid black;
  24. }
  25. .item {
  26. padding: 1rem;
  27. width: 10rem;
  28. background-color: blue;
  29. border: 1px solid black;
  30. }
  31. .container {
  32. /* 默认值:主轴水平方排列向且不换行 */
  33. flex-flow: row nowrap;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div class="container">
  39. <div class="item">item1</div>
  40. <div class="item">item2</div>
  41. <div class="item">item3</div>
  42. <div class="item">item4</div>
  43. <div class="item">item5</div>
  44. <div class="item">item6</div>
  45. <div class="item">item7</div>
  46. <div class="item">item8</div>
  47. </div>
  48. </body>
  49. </html>

  • 主轴方向水平排列并换行:flex-flow: row nowrap;
    水平换行
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>弹性flex容器与项目</title>
  8. <style>
  9. * {
  10. box-sizing: border-box;
  11. margin: 0;
  12. padding: 0;
  13. }
  14. :root {
  15. font-size: 10px;
  16. }
  17. body {
  18. font-size: 1.6rem;
  19. }
  20. .container {
  21. display: flex;
  22. height: 20rem;
  23. border: 1px solid black;
  24. }
  25. .item {
  26. padding: 1rem;
  27. width: 10rem;
  28. background-color: blue;
  29. border: 1px solid black;
  30. }
  31. .container {
  32. /* 主轴水平方排列向并换行 */
  33. flex-flow: row wrap;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div class="container">
  39. <div class="item">item1</div>
  40. <div class="item">item2</div>
  41. <div class="item">item3</div>
  42. <div class="item">item4</div>
  43. <div class="item">item5</div>
  44. <div class="item">item6</div>
  45. <div class="item">item7</div>
  46. <div class="item">item8</div>
  47. </div>
  48. </body>
  49. </html>

  • 主轴方向垂直排列且不换行:column nowrap
    垂直排列不换行
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>弹性flex容器与项目</title>
  8. <style>
  9. * {
  10. box-sizing: border-box;
  11. margin: 0;
  12. padding: 0;
  13. }
  14. :root {
  15. font-size: 10px;
  16. }
  17. body {
  18. font-size: 1.6rem;
  19. }
  20. .container {
  21. display: flex;
  22. height: 200rem;
  23. border: 1px solid black;
  24. }
  25. .item {
  26. padding: 1rem;
  27. width: 10rem;
  28. background-color: blue;
  29. border: 1px solid black;
  30. }
  31. .container {
  32. /* 主轴垂直方向排列不换行 */
  33. flex-flow: column nowrap;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div class="container">
  39. <div class="item">item1</div>
  40. <div class="item">item2</div>
  41. <div class="item">item3</div>
  42. <div class="item">item4</div>
  43. <div class="item">item5</div>
  44. <div class="item">item6</div>
  45. <div class="item">item7</div>
  46. <div class="item">item8</div>
  47. </div>
  48. </body>
  49. </html>

  • 主轴方向垂直排列并换行:column wrap
    垂直换行
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>弹性flex容器与项目</title>
  8. <style>
  9. * {
  10. box-sizing: border-box;
  11. margin: 0;
  12. padding: 0;
  13. }
  14. :root {
  15. font-size: 10px;
  16. }
  17. body {
  18. font-size: 1.6rem;
  19. }
  20. .container {
  21. display: flex;
  22. height: 20rem;
  23. border: 1px solid black;
  24. }
  25. .item {
  26. padding: 1rem;
  27. width: 10rem;
  28. background-color: blue;
  29. border: 1px solid black;
  30. }
  31. .container {
  32. /* 主轴垂直方排列向并换行 */
  33. flex-flow: column wrap;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div class="container">
  39. <div class="item">item1</div>
  40. <div class="item">item2</div>
  41. <div class="item">item3</div>
  42. <div class="item">item4</div>
  43. <div class="item">item5</div>
  44. <div class="item">item6</div>
  45. <div class="item">item7</div>
  46. <div class="item">item8</div>
  47. </div>
  48. </body>
  49. </html>

2:项目在主轴上的对其方式

justify-content(项目在主轴上的对其方式)属性的用法:
  • flex-start 默认值:起始线(左对齐)
  • flex-end 终止线(右对齐)
  • center 居中对齐
  • space-between 两端对齐
  • space-around 分散对齐
  • space-evenly 平均对齐
    下面演示平均对齐,其他只是参数替换,具体效果自行测试:
    项目平均对齐
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>弹性flex容器与项目</title>
  8. <style>
  9. * {
  10. box-sizing: border-box;
  11. margin: 0;
  12. padding: 0;
  13. }
  14. :root {
  15. font-size: 10px;
  16. }
  17. body {
  18. font-size: 1.6rem;
  19. }
  20. .container {
  21. display: flex;
  22. height: 20rem;
  23. border: 1px solid black;
  24. }
  25. .item {
  26. padding: 1rem;
  27. width: 10rem;
  28. background-color: blue;
  29. border: 1px solid black;
  30. }
  31. .container {
  32. /* .项目在主轴上的平均对齐,前提是主轴上存在剩余空间 */
  33. justify-content: space-evenly;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div class="container">
  39. <div class="item">item1</div>
  40. <div class="item">item2</div>
  41. <div class="item">item3</div>
  42. <div class="item">item4</div>
  43. <div class="item">item5</div>
  44. <div class="item">item6</div>
  45. <div class="item">item7</div>
  46. <div class="item">item8</div>
  47. </div>
  48. </body>
  49. </html>

3.项目在交叉轴上的对齐方式

align-items(项目在交叉轴上的对齐方式)属性的用法:
  • stretch 默认值:拉伸
  • flex-start 起始线(顶部对齐)
  • flex-end 终止线(底部对齐)
  • center 剧中对齐
    下面演示交叉轴居中对齐,其他只是参数替换,具体效果自行测试:
    交叉轴剧中对齐
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>弹性flex容器与项目</title>
  8. <style>
  9. * {
  10. box-sizing: border-box;
  11. margin: 0;
  12. padding: 0;
  13. }
  14. :root {
  15. font-size: 10px;
  16. }
  17. body {
  18. font-size: 1.6rem;
  19. }
  20. .container {
  21. display: flex;
  22. height: 20rem;
  23. border: 1px solid black;
  24. }
  25. .item {
  26. padding: 1rem;
  27. width: 10rem;
  28. background-color: blue;
  29. border: 1px solid black;
  30. }
  31. .container {
  32. /* .项目在交叉轴上的平均对齐 */
  33. align-items: center;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div class="container">
  39. <div class="item">item1</div>
  40. <div class="item">item2</div>
  41. <div class="item">item3</div>
  42. <div class="item">item4</div>
  43. <div class="item">item5</div>
  44. <div class="item">item6</div>
  45. <div class="item">item7</div>
  46. <div class="item">item8</div>
  47. </div>
  48. </body>
  49. </html>

4.项目在多行容器交叉轴上的对齐方式

align-content(项目在多行容器交叉轴上的对齐方式)属性的用法:
把项目看成整体时参数:
  • stretch 默认值:拉伸
  • flex-start 起始线(顶部对齐)
  • flex-end 终止线(底部对齐)
  • center 剧中对齐
    把项目看成个体时参数:
  • space-between 两端对齐
  • space-around 分散对齐
  • space-evenly 平均对齐
下面演示项目在多行容器交叉轴上两端对齐,其他只是参数替换,具体效果自行测试:

两端对齐

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>弹性flex容器与项目</title>
  8. <style>
  9. * {
  10. box-sizing: border-box;
  11. margin: 0;
  12. padding: 0;
  13. }
  14. :root {
  15. font-size: 10px;
  16. }
  17. body {
  18. font-size: 1.6rem;
  19. }
  20. .container {
  21. display: flex;
  22. height: 20rem;
  23. border: 1px solid black;
  24. }
  25. .item {
  26. width: 10rem;
  27. background-color: blue;
  28. border: 1px solid black;
  29. }
  30. .container {
  31. /* .项目在交叉轴上的平均对齐 */
  32. flex-flow: row wrap;
  33. align-content: space-between;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div class="container">
  39. <div class="item">item1</div>
  40. <div class="item">item2</div>
  41. <div class="item">item3</div>
  42. <div class="item">item4</div>
  43. <div class="item">item5</div>
  44. <div class="item">item6</div>
  45. <div class="item">item7</div>
  46. <div class="item">item8</div>
  47. </div>
  48. </body>
  49. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议