博客列表 >flex容器的常用属性及其使用案例

flex容器的常用属性及其使用案例

安超
安超原创
2022年10月27日 22:20:09368浏览

1.html中flex容器的常用属性及其使用案例

flex容器的常用属性:
1.flex-direction: (row/column),默认是row
2.flex-wrap:是否换行 nowrap-不换行
3.justify-content:子元素沿主轴方向的的排列方式 flex-stat等
4.align-items:次轴方向是否缩放,如stretch
5.align-content:多上是否换行,如 start等
以上可以简化为:
1.flex-flow :flex-direction flex-wrap
2.place-content : justify-content
3.place-items :aligh-items align-content

  1. place-items
    常用的缩放代码:
    flex : flex-grow flex-shrank flex-basis

    2.flex容器的案例

    a.flex容器内包含三个子元素,居中排列

    flex中的三个子元素居中排列
    代码如下:

    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. </head>
    9. <body>
    10. <!-- flex中三个子元素居中显示 -->
    11. <div class="container">
    12. <div class="flex">1</div>
    13. <div class="flex">2</div>
    14. <div class="flex">3</div>
    15. </div>
    16. <style>
    17. .container{
    18. height: 15em;
    19. display: flex;
    20. background-color: aqua;
    21. flex-direction: row;
    22. justify-content: center;
    23. align-items: center;
    24. }
    25. .flex{
    26. border: 0.1em solid chocolate;
    27. text-align: center;
    28. width: 10em;
    29. height: 10em;
    30. background-color: brown;
    31. }
    32. </style>
    33. </body>
    34. </html>

    <!-- flex中的三个子元素,第一个第三个占25%,第二个占50% -->

  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. </head>
  9. <body>
  10. <!-- flex中三个子元素居中显示 -->
  11. <div class="container">
  12. <div class="flex">1</div>
  13. <div class="flex">2</div>
  14. <div class="flex">3</div>
  15. </div>
  16. <!-- flex中的三个子元素,第一个第三个占25%,第二个占50% -->
  17. <br>
  18. <div class="container_1">
  19. <div class="divchild">1</div>
  20. <div class="divchild">2</div>
  21. <div class="divchild">3</div>
  22. </div>
  23. <style>
  24. body{
  25. margin: 0;
  26. padding: 0;
  27. }
  28. .container_1{
  29. display: flex;
  30. width: 80%;
  31. h20em;
  32. background-color: chartreuse;
  33. justify-content: center;
  34. }
  35. .container_1 .divchild{
  36. border: 1px solid #000;
  37. text-align: center;
  38. height: 20em;
  39. }
  40. .container_1 .divchild:nth-of-type(1),
  41. .container_1 .divchild:nth-of-type(3)
  42. {
  43. flex:1;
  44. }
  45. .container_1 .divchild:nth-of-type(2){
  46. flex:3;
  47. }
  48. .container{
  49. height: 15em;
  50. display: flex;
  51. background-color: aqua;
  52. flex-direction: row;
  53. justify-content: center;
  54. align-items: center;
  55. }
  56. .flex{
  57. border: 0.1em solid chocolate;
  58. text-align: center;
  59. width: 10em;
  60. height: 10em;
  61. background-color: brown;
  62. }
  63. </style>
  64. </body>
  65. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议