CSS3 自适应的Flex

响应式的Flexbox

您从CSS媒体查询一章中了解到,可以使用媒体查询为不同的屏幕大小和设备创建不同的布局。

笔记本或台式电脑:

1
2
3

移动设备:

1
2
3

实例
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. * {
  6. box-sizing: border-box;
  7. }
  8. .flex-container {
  9. display: flex;
  10. flex-direction: row;
  11. font-size: 30px;
  12. text-align: center;
  13. }
  14. .flex-item-left {
  15. background-color: #f1f1f1;
  16. padding: 10px;
  17. flex: 50%;
  18. }
  19. .flex-item-right {
  20. background-color: dodgerblue;
  21. padding: 10px;
  22. flex: 50%;
  23. }
  24. /* Responsive layout - makes a one column-layout instead of two-column layout */
  25. @media (max-width: 800px) {
  26. .flex-container {
  27. flex-direction: column;
  28. }
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <h1>响应式的Flexbox</h1>
  34. <p>"flex-direction: row;" 水平(从左到右)堆叠 flex 项。</p>
  35. <p>"flex-direction: column;" 垂直(从上到下)堆叠 flex 项。</p>
  36. <p><b>调整浏览器窗口的大小,以查看屏幕尺寸为800px或更小。</b></p>
  37. <div class="flex-container">
  38. <div class="flex-item-left">1</div>
  39. <div class="flex-item-right">2</div>
  40. </div>
  41. </body>
  42. </html>

使用 Flexbox 的响应式图库

使用 flexbox 创建响应式图像库,该图像库根据屏幕大小在四幅、两幅或全宽图像之间变化:

实例
  1. <!DOCTYPE html>
  2. <html>
  3. <style>
  4. * {
  5. box-sizing: border-box;
  6. }
  7. body {
  8. margin: 0;
  9. font-family: Arial;
  10. }
  11. .header {
  12. text-align: center;
  13. padding: 32px;
  14. }
  15. .row {
  16. display: flex;
  17. flex-wrap: wrap;
  18. padding: 0 4px;
  19. }
  20. /* Create four equal columns that sits next to each other */
  21. .column {
  22. flex: 25%;
  23. max-width: 25%;
  24. padding: 0 4px;
  25. }
  26. .column img {
  27. margin-top: 8px;
  28. vertical-align: middle;
  29. }
  30. /* Responsive layout - makes a two column-layout instead of four columns */
  31. @media screen and (max-width: 800px) {
  32. .column {
  33. flex: 50%;
  34. max-width: 50%;
  35. }
  36. }
  37. /* Responsive layout - makes the two columns stack on top of each other instead of next to each other */
  38. @media screen and (max-width: 600px) {
  39. .column {
  40. flex: 100%;
  41. max-width: 100%;
  42. }
  43. }
  44. </style>
  45. <body>
  46. <!-- Header -->
  47. <div class="header">
  48. <h2>响应式图片画廊</h2>
  49. <p>调整浏览器窗口的大小以查看响应效果。</p>
  50. </div>
  51. <!-- Photo Grid -->
  52. <div class="row">
  53. <div class="column">
  54. <img src="/images/wedding.jpg" style="width:100%">
  55. <img src="/images/rocks.jpg" style="width:100%">
  56. <img src="/images/falls2.jpg" style="width:100%">
  57. <img src="/images/paris.jpg" style="width:100%">
  58. <img src="/images/nature.jpg" style="width:100%">
  59. <img src="/images/mist.jpg" style="width:100%">
  60. <img src="/images/paris.jpg" style="width:100%">
  61. </div>
  62. <div class="column">
  63. <img src="/images/underwater.jpg" style="width:100%">
  64. <img src="/images/ocean.jpg" style="width:100%">
  65. <img src="/images/wedding.jpg" style="width:100%">
  66. <img src="/images/mountainskies.jpg" style="width:100%">
  67. <img src="/images/rocks.jpg" style="width:100%">
  68. <img src="/images/underwater.jpg" style="width:100%">
  69. </div>
  70. <div class="column">
  71. <img src="/images/wedding.jpg" style="width:100%">
  72. <img src="/images/rocks.jpg" style="width:100%">
  73. <img src="/images/falls2.jpg" style="width:100%">
  74. <img src="/images/paris.jpg" style="width:100%">
  75. <img src="/images/nature.jpg" style="width:100%">
  76. <img src="/images/mist.jpg" style="width:100%">
  77. <img src="/images/paris.jpg" style="width:100%">
  78. </div>
  79. <div class="column">
  80. <img src="/images/underwater.jpg" style="width:100%">
  81. <img src="/images/ocean.jpg" style="width:100%">
  82. <img src="/images/wedding.jpg" style="width:100%">
  83. <img src="/images/mountainskies.jpg" style="width:100%">
  84. <img src="/images/rocks.jpg" style="width:100%">
  85. <img src="/images/underwater.jpg" style="width:100%">
  86. </div>
  87. </div>
  88. </body>
  89. </html>

使用 Flexbox 的响应式网站

使用 flexbox 创建响应式网站,其中包含弹性导航栏和弹性内容:

代码如下:

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=1">
  5. <style>
  6. * {
  7. box-sizing: border-box;
  8. }
  9. /* Style the body */
  10. body {
  11. font-family: Arial;
  12. margin: 0;
  13. }
  14. /* Header/logo Title */
  15. .header {
  16. padding: 60px;
  17. text-align: center;
  18. background: #1abc9c;
  19. color: white;
  20. }
  21. /* Style the top navigation bar */
  22. .navbar {
  23. display: -ms-flexbox; /* IE10 */
  24. display: flex;
  25. background-color: #333;
  26. }
  27. /* Style the navigation bar links */
  28. .navbar a {
  29. color: white;
  30. padding: 14px 20px;
  31. text-decoration: none;
  32. text-align: center;
  33. }
  34. /* Change color on hover */
  35. .navbar a:hover {
  36. background-color: #ddd;
  37. color: black;
  38. }
  39. /* Column container */
  40. .row {
  41. display: -ms-flexbox; /* IE10 */
  42. display: flex;
  43. -ms-flex-wrap: wrap; /* IE10 */
  44. flex-wrap: wrap;
  45. }
  46. /* Create two unequal columns that sits next to each other */
  47. /* Sidebar/left column */
  48. .side {
  49. -ms-flex: 30%; /* IE10 */
  50. flex: 30%;
  51. background-color: #f1f1f1;
  52. padding: 20px;
  53. }
  54. /* Main column */
  55. .main {
  56. -ms-flex: 70%; /* IE10 */
  57. flex: 70%;
  58. background-color: white;
  59. padding: 20px;
  60. }
  61. /* Fake image, just for this example */
  62. .fakeimg {
  63. background-color: #aaa;
  64. width: 100%;
  65. padding: 20px;
  66. }
  67. /* Footer */
  68. .footer {
  69. padding: 20px;
  70. text-align: center;
  71. background: #ddd;
  72. }
  73. /* 响应式布局-当屏幕宽度小于700px时,使两列堆叠在一起,而不是相邻 */
  74. @media screen and (max-width: 700px) {
  75. .row, .navbar {
  76. flex-direction: column;
  77. }
  78. }
  79. </style>
  80. </head>
  81. <body>
  82. <!-- Header -->
  83. <div class="header">
  84. <h2>我的网站</h2>
  85. <p>使用了弹性布局。</p>
  86. </div>
  87. <!-- Navigation Bar -->
  88. <div class="navbar">
  89. <a href="#">链接</a>
  90. <a href="#">链接</a>
  91. <a href="#">链接</a>
  92. <a href="#">链接</a>
  93. </div>
  94. <!-- The flexible grid (content) -->
  95. <div class="row">
  96. <div class="side">
  97. <h2>关于我</h2>
  98. <h5>我的照片:</h5>
  99. <div class="fakeimg" style="height:200px;">图片</div>
  100. <p>关于我的一些文本...</p>
  101. <h3>更多文本</h3>
  102. <p>文本文本文本文本...</p>
  103. <div class="fakeimg" style="height:60px;">图片</div><br>
  104. <div class="fakeimg" style="height:60px;">图片</div><br>
  105. <div class="fakeimg" style="height:60px;">图片</div>
  106. </div>
  107. <div class="main">
  108. <h2>标题</h2>
  109. <h5>标题描述,2021 年 7 月 7 日</h5>
  110. <div class="fakeimg" style="height:200px;">Image</div>
  111. <p>一些文本...</p>
  112. <p>文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本...</p>
  113. <br>
  114. <h2>TITLE HEADING</h2>
  115. <h5>标题描述,2021 年 107月 7 日</h5>
  116. <div class="fakeimg" style="height:200px;">图片</div>
  117. <p>一些文本...</p>
  118. <p>文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本...</p>
  119. </div>
  120. </div>
  121. <!-- Footer -->
  122. <div class="footer">
  123. <h2>页脚</h2>
  124. </div>
  125. </body>
  126. </html>

分类导航