博客列表 >弹性布局之弹性容器-PHP九期线上班

弹性布局之弹性容器-PHP九期线上班

老袁
老袁原创
2019年11月05日 14:56:57455浏览

给全局定义的public.css

  1. .container {
  2. border: 2px dashed red;
  3. margin: 15px;
  4. background-color: #cdc;
  5. }
  6. .item {
  7. box-sizing: border-box;
  8. border: 1px solid;
  9. padding: 20px;
  10. background-color: #ede;
  11. }
  12. .flex {
  13. display: flex;
  14. flex-direction: row;
  15. }

弹性容器的两种类型

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>弹性容器的二种类型</title>
  6. <link rel="stylesheet" href="css/style1.css">
  7. </head>
  8. <body>
  9. <h3>1. 块级弹性容器</h3>
  10. <div class="container flex">
  11. <span class="item">item1</span>
  12. <span class="item">item2</span>
  13. <span class="item">item3</span>
  14. </div>
  15. <div class="container flex">
  16. <span class="item">item1</span>
  17. <span class="item">item2</span>
  18. <span class="item">item3</span>
  19. </div>
  20. <hr style="height: 3px; background-color:green;">
  21. <h3>2. 行内弹性容器</h3>
  22. <div class="container inline-flex">
  23. <span class="item">item1</span>
  24. <span class="item">item2</span>
  25. <span class="item">item3</span>
  26. </div>
  27. <div class="container inline-flex">
  28. <span class="item">item1</span>
  29. <span class="item">item2</span>
  30. <span class="item">item3</span>
  31. </div>
  32. </body>
  33. </html>
  34. CSS:
  35. @import "public.css";
  36. .inline-flex {
  37. display: inline-flex;
  38. }


弹性容器(盒子)做导航

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>弹性容器(盒子)做导航</title>
  6. <link rel="stylesheet" href="css/style2.css">
  7. </head>
  8. <body>
  9. <nav>
  10. <a href="">首页</a>
  11. <a href="">教学视频</a>
  12. <a href="">社区问答</a>
  13. <a href="">软件下载</a>
  14. <a href="">联系我们</a>
  15. </nav>
  16. </body>
  17. </html>
  18. CSS:
  19. a {
  20. text-decoration: none;
  21. background-color: lightgreen;
  22. color: black;
  23. padding: 5px 10px;
  24. margin: 0 5px;
  25. border-radius: 5px 5px 0 0;
  26. }
  27. nav {
  28. display: flex;
  29. border-bottom: 1px solid gray;
  30. }
  31. a:hover, a:focus, a:active {
  32. background-color: orangered;
  33. color: white;
  34. }


定义弹性容器的主轴方向: 弹性元素的主轴上的排列方向

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <link rel="stylesheet" href="css/style3.css">
  6. <title>定义弹性容器的主轴方向: 弹性元素的主轴上的排列方向</title>
  7. </head>
  8. <body>
  9. <h3>1. row: 默认从左到右, 水平排列</h3>
  10. <div class="container flex row">
  11. <span class="item">item1</span>
  12. <span class="item">item2</span>
  13. <span class="item">item3</span>
  14. </div>
  15. <h3>2. row-reverse: 从右到左, 水平排列</h3>
  16. <div class="container flex row-reverse">
  17. <span class="item">item1</span>
  18. <span class="item">item2</span>
  19. <span class="item">item3</span>
  20. </div>
  21. <h3>3. column: 从上到下, 垂直排列</h3>
  22. <div class="container flex column">
  23. <span class="item">item1</span>
  24. <span class="item">item2</span>
  25. <span class="item">item3</span>
  26. </div>
  27. <h3>4. column-reverse: 从下到上, 垂直排列</h3>
  28. <div class="container flex column-reverse">
  29. <span class="item">item1</span>
  30. <span class="item">item2</span>
  31. <span class="item">item3</span>
  32. </div>
  33. </body>
  34. </html>
  35. @import "public.css";
  36. .row {
  37. flex-direction: row;
  38. }
  39. .row-reverse {
  40. flex-direction: row-reverse;
  41. }
  42. .column {
  43. flex-direction: column;
  44. }
  45. .column-reverse {
  46. flex-direction: column-reverse;
  47. }


做一个博客首页

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>blog</title>
  6. <link rel="stylesheet" href="css/style4.css">
  7. </head>
  8. <body>
  9. <header>
  10. <h1>blog</h1>
  11. </header>
  12. <nav>
  13. <a href="">首页</a>
  14. <a href="">教学视频</a>
  15. <a href="">社区问答</a>
  16. <a href="">软件下载</a>
  17. <a href="">联系我们</a>
  18. </nav>
  19. <main>
  20. <article>
  21. <img src="images/1.jpg" alt="">
  22. <p>文字介绍</p>
  23. <button>按钮文字</button>
  24. </article>
  25. <article>
  26. <img src="images/2.jpg" alt="">
  27. <p>文字介绍</p>
  28. <button>按钮文字</button>
  29. </article>
  30. <article>
  31. <img src="images/3.jpg" alt="">
  32. <p>文字介绍</p>
  33. <button>按钮文字</button>
  34. </article>
  35. </main>
  36. <footer>
  37. <p>Copyright &#169; 2018</p>
  38. </footer>
  39. </body>
  40. </html>
  41. CSS:
  42. @import "style2.css";
  43. * {
  44. outline: 1px solid #ccc;
  45. margin: 10px;
  46. padding: 10px;
  47. }
  48. body, nav, main, article {
  49. display: flex;
  50. }
  51. body, article {
  52. flex-direction: column;
  53. }
  54. footer {
  55. border-top: 1px solid #ccc;
  56. }
  57. nav {
  58. padding-bottom: 0;
  59. }


5.弹性元素溢出与创建多行容器

以主轴水平方向为例进行演示

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>弹性元素溢出与创建多行容器</title>
  6. <link rel="stylesheet" href="css/style5.css">
  7. </head>
  8. <body>
  9. <h1>以主轴水平方向为例进行演示:</h1>
  10. <h3>1. nowrap: 默认不换行, 元素自动缩小填充容器</h3>
  11. <div class="container flex row nowrap">
  12. <span class="item">item1</span>
  13. <span class="item">item2</span>
  14. <span class="item">item3</span>
  15. <span class="item">item4</span>
  16. <span class="item">item5</span>
  17. <span class="item">item6</span>
  18. <span class="item">item7</span>
  19. <span class="item">item8</span>
  20. <span class="item">item9</span>
  21. <span class="item">item10</span>
  22. <span class="item">item11</span>
  23. </div>
  24. <hr style="height: 3px; background-color:green;">
  25. <h3>2. wrap: 换行, 弹性元素超出容器边界的换到下一行显示</h3>
  26. <div class="container flex row wrap">
  27. <span class="item">item1</span>
  28. <span class="item">item2</span>
  29. <span class="item">item3</span>
  30. <span class="item">item4</span>
  31. <span class="item">item5</span>
  32. <span class="item">item6</span>
  33. <span class="item">item7</span>
  34. <span class="item">item8</span>
  35. <span class="item">item9</span>
  36. <span class="item">item10</span>
  37. <span class="item">item11</span>
  38. </div>
  39. <hr style="height: 3px; background-color:green;">
  40. <h3>3. wrap-reverse: 换行, 弹性元素反转排列</h3>
  41. <div class="container flex row wrap-reverse">
  42. <span class="item">item1</span>
  43. <span class="item">item2</span>
  44. <span class="item">item3</span>
  45. <span class="item">item4</span>
  46. <span class="item">item5</span>
  47. <span class="item">item6</span>
  48. <span class="item">item7</span>
  49. <span class="item">item8</span>
  50. <span class="item">item9</span>
  51. <span class="item">item10</span>
  52. <span class="item">item11</span>
  53. </div>
  54. </body>
  55. </html>
  56. @import "public.css";
  57. .container {
  58. width: 500px;
  59. }
  60. .nowrap {
  61. flex-direction: row;
  62. flex-wrap: nowrap;
  63. }
  64. .wrap {
  65. flex-direction: row;
  66. flex-wrap: wrap;
  67. }
  68. .wrap-reverse {
  69. flex-direction: row;
  70. flex-wrap: wrap-reverse;
  71. }


6.弹性元素溢出与创建多行容器

以主轴水平方向为例进行演示

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>弹性元素溢出与创建多行容器</title>
  6. <link rel="stylesheet" href="css/style6.css">
  7. </head>
  8. <body>
  9. <h1>以主轴水平方向为例进行演示:</h1>
  10. <h3>1. nowrap: 默认不换行, 元素自动缩小填充容器</h3>
  11. <div class="container flex row nowrap">
  12. <span class="item">item1</span>
  13. <span class="item">item2</span>
  14. <span class="item">item3</span>
  15. <span class="item">item4</span>
  16. <span class="item">item5</span>
  17. <span class="item">item6</span>
  18. <span class="item">item7</span>
  19. <span class="item">item8</span>
  20. <span class="item">item9</span>
  21. <span class="item">item10</span>
  22. <span class="item">item11</span>
  23. </div>
  24. <hr style="height: 3px; background-color:green;">
  25. <h3>2. wrap: 换行, 弹性元素超出容器边界的换到下一行显示</h3>
  26. <div class="container flex row wrap">
  27. <span class="item">item1</span>
  28. <span class="item">item2</span>
  29. <span class="item">item3</span>
  30. <span class="item">item4</span>
  31. <span class="item">item5</span>
  32. <span class="item">item6</span>
  33. <span class="item">item7</span>
  34. <span class="item">item8</span>
  35. <span class="item">item9</span>
  36. <span class="item">item10</span>
  37. <span class="item">item11</span>
  38. </div>
  39. <hr style="height: 3px; background-color:green;">
  40. <h3>3. wrap-reverse: 换行, 弹性元素反转排列</h3>
  41. <div class="container flex row wrap-reverse">
  42. <span class="item">item1</span>
  43. <span class="item">item2</span>
  44. <span class="item">item3</span>
  45. <span class="item">item4</span>
  46. <span class="item">item5</span>
  47. <span class="item">item6</span>
  48. <span class="item">item7</span>
  49. <span class="item">item8</span>
  50. <span class="item">item9</span>
  51. <span class="item">item10</span>
  52. <span class="item">item11</span>
  53. </div>
  54. </body>
  55. </html>
  56. @import "public.css";
  57. .container {
  58. width: 500px;
  59. }
  60. .nowrap {
  61. flex-flow: row nowrap;
  62. }
  63. .wrap {
  64. flex-flow: row wrap;
  65. }
  66. .wrap-reverse {
  67. flex-flow: row wrap-reverse;
  68. }


弹性元素在主轴上如何分布

1. flex-start: 主轴起点开始排列

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>弹性元素在主轴上如何分布</title>
  6. <link rel="stylesheet" href="css/style7.css">
  7. </head>
  8. <body>
  9. <h1>弹性元素在主轴上如何分布</h1>
  10. <h3>1. flex-start: 主轴起点开始排列</h3>
  11. <p>单行</p>
  12. <div class="container flex flex-start">
  13. <span class="item">item1</span>
  14. <span class="item">item2</span>
  15. <span class="item">item3</span>
  16. </div>
  17. <p>多行</p>
  18. <div class="container flex flex-start wrap">
  19. <span class="item">item1</span>
  20. <span class="item">item2</span>
  21. <span class="item">item3</span>
  22. <span class="item">item4</span>
  23. <span class="item">item5</span>
  24. <span class="item">item6</span>
  25. </div>
  26. <hr style="height: 3px; background:green;">
  27. <h3>2. flex-end: 主轴终点开始排列</h3>
  28. <p>单行</p>
  29. <div class="container flex flex-end">
  30. <span class="item">item1</span>
  31. <span class="item">item2</span>
  32. <span class="item">item3</span>
  33. </div>
  34. <p>多行</p>
  35. <div class="container flex flex-end wrap">
  36. <span class="item">item1</span>
  37. <span class="item">item2</span>
  38. <span class="item">item3</span>
  39. <span class="item">item4</span>
  40. <span class="item">item5</span>
  41. <span class="item">item6</span>
  42. </div>
  43. <hr style="height: 3px; background:green;">
  44. <h3>3. center: 弹性元素做一个整体居中显示</h3>
  45. <p>单行</p>
  46. <div class="container flex center">
  47. <span class="item">item1</span>
  48. <span class="item">item2</span>
  49. <span class="item">item3</span>
  50. </div>
  51. <p>多行</p>
  52. <div class="container flex center wrap">
  53. <span class="item">item1</span>
  54. <span class="item">item2</span>
  55. <span class="item">item3</span>
  56. <span class="item">item4</span>
  57. <span class="item">item5</span>
  58. <span class="item">item6</span>
  59. </div>
  60. <hr style="height: 3px; background:green;">
  61. <h3>4. space-between:首尾元素紧贴起止点,其它元素平分剩余空间</h3>
  62. <p>单行</p>
  63. <div class="container flex space-between">
  64. <span class="item">item1</span>
  65. <span class="item">item2</span>
  66. <span class="item">item3</span>
  67. </div>
  68. <p>多行</p>
  69. <div class="container flex space-between wrap">
  70. <span class="item">item1</span>
  71. <span class="item">item2</span>
  72. <span class="item">item3</span>
  73. <span class="item">item4</span>
  74. <span class="item">item5</span>
  75. <span class="item">item6</span>
  76. </div>
  77. <hr style="height: 3px; background:green;">
  78. <h3>5. space-around:每个元素二边空间相等,相邻元素空间累加</h3>
  79. <p>单行</p>
  80. <div class="container flex space-around">
  81. <span class="item">item1</span>
  82. <span class="item">item2</span>
  83. <span class="item">item3</span>
  84. </div>
  85. <p>多行</p>
  86. <div class="container flex space-around wrap">
  87. <span class="item">item1</span>
  88. <span class="item">item2</span>
  89. <span class="item">item3</span>
  90. <span class="item">item4</span>
  91. <span class="item">item5</span>
  92. <span class="item">item6</span>
  93. </div>
  94. <hr style="height: 3px; background:green;">
  95. <h3>6. space-evenly:每个元素, 以及元素到与起止点的空间全部相等</h3>
  96. <p>单行</p>
  97. <div class="container flex space-evenly">
  98. <span class="item">item1</span>
  99. <span class="item">item2</span>
  100. <span class="item">item3</span>
  101. </div>
  102. <p>多行</p>
  103. <div class="container flex space-evenly wrap">
  104. <span class="item">item1</span>
  105. <span class="item">item2</span>
  106. <span class="item">item3</span>
  107. <span class="item">item4</span>
  108. <span class="item">item5</span>
  109. <span class="item">item6</span>
  110. </div>
  111. </body>
  112. </html>
  113. CSS
  114. @import "public.css";
  115. .container {
  116. width: 500px;
  117. }
  118. .wrap {
  119. flex-wrap: wrap;
  120. }
  121. .flex-start {
  122. justify-content: flex-start;
  123. }
  124. .flex-end {
  125. justify-content: flex-end;
  126. }
  127. .center {
  128. justify-content: center;
  129. }
  130. .space-between {
  131. justify-content: space-between;
  132. }
  133. .space-around {
  134. justify-content: space-around;
  135. }
  136. .space-evenly {
  137. justify-content: space-evenly;
  138. }




使用弹性元素主轴对齐来改写导航

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>使用弹性元素主轴对齐来改写导航</title>
  6. <link rel="stylesheet" href="css/style8.css">
  7. </head>
  8. <body>
  9. <nav>
  10. <a href="">首页</a>
  11. <a href="">教学视频</a>
  12. <a href="">社区问答</a>
  13. <a href="">软件下载</a>
  14. <a href="">联系我们</a>
  15. </nav>
  16. </body>
  17. </html>
  18. CSS:
  19. a {
  20. text-decoration: none;
  21. background-color: lightgreen;
  22. color: black;
  23. padding: 5px 10px;
  24. margin: 0 5px;
  25. border-radius: 5px 5px 0 0;
  26. }
  27. nav {
  28. display: flex;
  29. border-bottom: 1px solid gray;
  30. }
  31. a:hover, a:focus, a:active {
  32. background-color: orangered;
  33. color: white;
  34. }
  35. nav {
  36. justify-content: center;
  37. }


弹性元素在垂直方向(交叉轴)上的对齐方式

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>弹性元素在垂直方向(交叉轴)上的对齐方式</title>
  6. <link rel="stylesheet" href="css/style9.css">
  7. </head>
  8. <body>
  9. <h1>弹性元素在垂直轴上分布方式:</h1>
  10. <h3>1. stretch: 默认值, 元素高度自动拉伸充满整个容器</h3>
  11. <p>单行容器:</p>
  12. <div class="container flex stretch">
  13. <span class="item">item1</span>
  14. <span class="item">item2</span>
  15. <span class="item">item3</span>
  16. </div>
  17. <p>多行容器:</p>
  18. <div class="container flex wrap wrap-stretch">
  19. <span class="item">item1</span>
  20. <span class="item">item2</span>
  21. <span class="item">item3</span>
  22. <span class="item">item4</span>
  23. <span class="item">item5</span>
  24. <span class="item">item6</span>
  25. </div>
  26. <h3>2.flex-start: 元素紧贴容器起点</h3>
  27. <p>单行容器:</p>
  28. <div class="container flex flex-start">
  29. <span class="item">item1</span>
  30. <span class="item">item2</span>
  31. <span class="item">item3</span>
  32. </div>
  33. <p>多行容器:</p>
  34. <div class="container flex wrap wrap-flex-start">
  35. <span class="item">item1</span>
  36. <span class="item">item2</span>
  37. <span class="item">item3</span>
  38. <span class="item">item4</span>
  39. <span class="item">item5</span>
  40. <span class="item">item6</span>
  41. </div>
  42. <h3>3.flex-end: 元素紧贴容器终点</h3>
  43. <p>单行容器:</p>
  44. <div class="container flex flex-end">
  45. <span class="item">item1</span>
  46. <span class="item">item2</span>
  47. <span class="item">item3</span>
  48. </div>
  49. <p>多行容器:</p>
  50. <div class="container flex wrap wrap-flex-end">
  51. <span class="item">item1</span>
  52. <span class="item">item2</span>
  53. <span class="item">item3</span>
  54. <span class="item">item4</span>
  55. <span class="item">item5</span>
  56. <span class="item">item6</span>
  57. </div>
  58. <h3>4.center: 所有元素做为一个整体在容器垂直方向居中显示</h3>
  59. <p>单行容器:</p>
  60. <div class="container flex center">
  61. <span class="item">item1</span>
  62. <span class="item">item2</span>
  63. <span class="item">item3</span>
  64. </div>
  65. <p>多行容器:</p>
  66. <div class="container flex wrap wrap-center">
  67. <span class="item">item1</span>
  68. <span class="item">item2</span>
  69. <span class="item">item3</span>
  70. <span class="item">item4</span>
  71. <span class="item">item5</span>
  72. <span class="item">item6</span>
  73. </div>
  74. <h3>5.space-between: 垂直方向首尾行紧贴起止点,其它行平分剩余空间</h3>
  75. <p>多行容器:</p>
  76. <div class="container flex wrap wrap-space-between">
  77. <span class="item">item1</span>
  78. <span class="item">item2</span>
  79. <span class="item">item3</span>
  80. <span class="item">item4</span>
  81. <span class="item">item5</span>
  82. <span class="item">item6</span>
  83. <span class="item">item7</span>
  84. <span class="item">item8</span>
  85. <span class="item">item9</span>
  86. <span class="item">item10</span>
  87. <span class="item">item11</span>
  88. <span class="item">item12</span>
  89. </div>
  90. <h3>6.space-around: 每个元素二边空间相等,相邻元素空间累加</h3>
  91. <p>多行容器:</p>
  92. <div class="container flex wrap wrap-space-around">
  93. <span class="item">item1</span>
  94. <span class="item">item2</span>
  95. <span class="item">item3</span>
  96. <span class="item">item4</span>
  97. <span class="item">item5</span>
  98. <span class="item">item6</span>
  99. <span class="item">item7</span>
  100. <span class="item">item8</span>
  101. <span class="item">item9</span>
  102. <span class="item">item10</span>
  103. <span class="item">item11</span>
  104. <span class="item">item12</span>
  105. </div>
  106. <h3>7.space-evenly: 每个元素, 以及元素到与起止点的空间全部相等</h3>
  107. <p>多行容器:</p>
  108. <div class="container flex wrap wrap-space-evenly">
  109. <span class="item">item1</span>
  110. <span class="item">item2</span>
  111. <span class="item">item3</span>
  112. <span class="item">item4</span>
  113. <span class="item">item5</span>
  114. <span class="item">item6</span>
  115. <span class="item">item7</span>
  116. <span class="item">item8</span>
  117. <span class="item">item9</span>
  118. <span class="item">item10</span>
  119. <span class="item">item11</span>
  120. <span class="item">item12</span>
  121. </div>
  122. </body>
  123. </html>
  124. css:
  125. @import "public.css";
  126. .container {
  127. width: 500px;
  128. height: 300px;
  129. }
  130. .wrap {
  131. flex-wrap: wrap;
  132. }
  133. /*单行容器*/
  134. /*align-items: 设置单行容器中元素在垂直轴上的排列方式*/
  135. .stretch {
  136. align-items: stretch;
  137. }
  138. .flex-start {
  139. align-items: flex-start;
  140. }
  141. .flex-end {
  142. align-items: flex-end;
  143. }
  144. .center {
  145. align-items: center;
  146. }
  147. /*多行容器*/
  148. /*align-content: 设置多行容器中弹性元素的对齐方式和空间分配方案*/
  149. .wrap-stretch {
  150. align-content: stretch;
  151. }
  152. .wrap-flex-start {
  153. align-content: flex-start;
  154. }
  155. .wrap-flex-end {
  156. align-content: flex-end;
  157. }
  158. .wrap-center {
  159. align-content: center;
  160. }
  161. .wrap-space-between {
  162. align-content: space-between;
  163. }
  164. .wrap-space-around {
  165. align-content: space-around;
  166. }
  167. .wrap-space-evenly {
  168. align-content: space-evenly;
  169. }







总结

1.弹性容器为分二种:
display: flex: 块级弹性容器
display: inline-flex: 行内块级弹性容器
2.弹性元素(Flex item)必须是弹性容器的直接子元素
3.主轴/垂直轴
|a|flex-direction|row(默认), row-reverse, columne, column-reverse|弹性元素在主轴上排列方向|
|b|flex-wrap|norap(默认), wrap, wrap-reverse|弹性元素在主轴上是否换行|
|c|flex-flow|flex-direction, flex-wrap|定义主轴方向以及弹性元素是否换行,flex-directionflex-wrap简写|
|d|justify-content|flex-start(默认值),flex-end,center,space-between,space-around,space-evenly|弹性元素在主轴上的对齐方式与空间分布|
|e|align-items|stretch(默认),flex-start,flex-end,center|定义弹性元素在垂直轴上的对齐方式|
|f|align-content|stretch(默认),flex-start,flex-end,center,space-between,space-around,space-evenly|定义多行容器中弹性元素在垂直轴上的对齐方式与空间分配|

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议