当前位置:   article > 正文

display:flex布局,最简单的案例_display:flex 里面怎么写

display:flex 里面怎么写

1. 左右贴边

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. #parent{
  8. width: 800px;
  9. background: red;
  10. height: 200px;
  11. display: flex;
  12. justify-content: space-between;
  13. }
  14. #parent span{
  15. width: 100px;
  16. height: 100px;
  17. background: yellow;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="parent">
  23. <span>1</span>
  24. <span>2</span>
  25. <span>3</span>
  26. </div>
  27. </body>
  28. </html>

2.左右不贴边

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. #parent{
  8. width: 800px;
  9. background: red;
  10. height: 200px;
  11. display: flex;
  12. justify-content: space-around;
  13. }
  14. #parent span{
  15. width: 100px;
  16. height: 100px;
  17. background: yellow;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="parent">
  23. <span>1</span>
  24. <span>2</span>
  25. <span>3</span>
  26. </div>
  27. </body>
  28. </html>

 

 3.元素自动换行

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. #parent{
  8. width: 600px;
  9. background: red;
  10. height: 300px;
  11. display: flex;
  12. /*flex布局中,默认的子元素是不换行的nowrap, 如果装不开,会缩小子元素的宽度,放到父元素里面*/
  13. flex-wrap: wrap;/*换行*/
  14. }
  15. #parent span{
  16. width: 100px;
  17. height: 100px;
  18. background: yellow;
  19. margin: 5px;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="parent">
  25. <span>1</span>
  26. <span>2</span>
  27. <span>3</span>
  28. <span>3</span>
  29. <span>3</span>
  30. <span>3</span>
  31. <span>3</span>
  32. <span>3</span>
  33. </div>
  34. </body>
  35. </html>

4.垂直居中

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. #parent{
  8. width: 600px;
  9. background: red;
  10. height: 300px;
  11. display: flex;
  12. /*默认的主轴是 x轴 row */
  13. justify-content: center;
  14. /*我们需要一个侧轴居中*/
  15. align-items: center;
  16. }
  17. #parent span{
  18. width: 100px;
  19. height: 100px;
  20. background: yellow;
  21. margin: 5px;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="parent">
  27. <span>1</span>
  28. <span>2</span>
  29. <span>3</span>
  30. </div>
  31. </body>
  32. </html>

 

5.子元素的高度自适应父元素(拉伸)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. #parent{
  8. width: 600px;
  9. background: red;
  10. height: 300px;
  11. display: flex;
  12. /*默认的主轴是 x轴 row */
  13. justify-content: center;
  14. /*拉伸,但是子盒子不要给高度*/
  15. align-items:stretch;
  16. }
  17. #parent span{
  18. width: 100px;
  19. background: yellow;
  20. margin: 5px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="parent">
  26. <span>1</span>
  27. <span>2</span>
  28. <span>3</span>
  29. </div>
  30. </body>
  31. </html>

 

 

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

闽ICP备14008679号