博客列表 >使用flex布局实现首页排版总结

使用flex布局实现首页排版总结

JKY辉哥
JKY辉哥原创
2020年07月01日 18:32:231224浏览

使用 flex 布局实现首页

1.html 结构代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>布局实例</title>
  7. <link rel="stylesheet" href="css/common.css" />
  8. <link rel="stylesheet" href="css/index.css" />
  9. </head>
  10. <body>
  11. <header id="mainHeader" class="flexDis">
  12. <div>LOGO</div>
  13. <ul>
  14. <li><a href="#">home</a></li>
  15. <li><a href="#">html</a></li>
  16. <li><a href="#">css</a></li>
  17. <li><a href="#">js</a></li>
  18. <li><a href="#">php</a></li>
  19. </ul>
  20. <div class="serchInfo">
  21. <input type="text" placeholder="请输入关键词" value="" />
  22. <a href=""></a>
  23. </div>
  24. <p><a href="#">login</a><a href="#">register</a></p>
  25. </header>
  26. <section class="contrain secCon1 flexDis">
  27. <ul>
  28. <li><a href="#">列表项1</a></li>
  29. <li><a href="#">列表项2</a></li>
  30. <li><a href="#">列表项3</a></li>
  31. <li><a href="#">列表项4</a></li>
  32. <li><a href="#">列表项5</a></li>
  33. <li><a href="#">列表项6</a></li>
  34. <li><a href="#">列表项7</a></li>
  35. </ul>
  36. <div class="preview applyFlex">
  37. <p><a href="#"></a><a href="#"></a></p>
  38. <div class="applyFlex">
  39. <img src="./images/1.jpg" alt="" />
  40. <img src="./images/2.jpg" alt="" />
  41. <img src="./images/3.jpg" alt="" />
  42. <img src="./images/4.jpg" alt="" />
  43. </div>
  44. <div class="controler">
  45. <span></span><span></span><span></span><span></span>
  46. </div>
  47. </div>
  48. </section>
  49. <section class="secCon2 contrain">
  50. <h1>大标题<small>标题内容提示信息</small></h1>
  51. <div class="flexDis flexContentB">
  52. <p>内容1</p>
  53. <p>内容2</p>
  54. <p>内容3</p>
  55. <p>内容4</p>
  56. </div>
  57. </section>
  58. <section class="secCon2 contrain">
  59. <h1>大标题<small>标题内容提示信息</small></h1>
  60. <div class="flexDis flexContentB">
  61. <p>内容1</p>
  62. <p>内容2</p>
  63. <p>内容3</p>
  64. <p>内容4</p>
  65. </div>
  66. </section>
  67. <section class="secCon2 contrain">
  68. <h1 class="flexDis flexAignC">
  69. 大标题<span
  70. ><a href="#">选项1</a><a href="#">选项2</a> <a href="#">选项3</a
  71. ><a href="#">选项4</a></span
  72. >
  73. <p><a href="">更多</a></p>
  74. </h1>
  75. <div class="flexDis con2 flexContentB">
  76. <div>
  77. <p>内容1</p>
  78. <span>内容文字介绍1</span>
  79. </div>
  80. <div>
  81. <p>内容2</p>
  82. <span>内容文字介绍2</span>
  83. </div>
  84. <div>
  85. <p>内容3</p>
  86. <span>内容文字介绍3</span>
  87. </div>
  88. <div>
  89. <p>内容4</p>
  90. <span>内容文字介绍4</span>
  91. </div>
  92. </div>
  93. <div class="flexDis con2 flexContentB">
  94. <div>
  95. <p>内容1</p>
  96. <span>内容文字介绍1内容文字介绍1内容文字介绍1内容文字介绍1</span>
  97. </div>
  98. <div>
  99. <p>内容2</p>
  100. <span
  101. >内容文字介绍2内容文字介绍2内容文字介绍2内容文字介绍2内容文字介绍2</span
  102. >
  103. </div>
  104. <div>
  105. <p>内容3</p>
  106. <span>内容文字介绍3内容文字介绍3内容文字介绍3内容文字介绍3</span>
  107. </div>
  108. <div>
  109. <p>内容4</p>
  110. <span
  111. >内容文字介绍4内容文字介绍4内容文字介绍4内容文字介绍4内容文字介绍4</span
  112. >
  113. </div>
  114. </div>
  115. </section>
  116. <footer class="mainfooter">
  117. <div class="contrain flexDis">
  118. <div class="applyFlex">
  119. <p>
  120. <a href="">链接1</a>
  121. <a href="">链接2</a>
  122. </p>
  123. <p class="copyrightInfo">copyright©京备xxx号</p>
  124. </div>
  125. <div>
  126. <img src="images/code.jpg" />
  127. </div>
  128. </div>
  129. </footer>
  130. </body>
  131. </html>

2.公共样式文件 common.css

  1. #mainHeader {
  2. background: white;
  3. padding: 1.5em;
  4. box-shadow: 0 0.2em 0.4em rgba(0, 0, 0, 0.2);
  5. }
  6. #mainHeader ul {
  7. padding: 0 1em;
  8. }
  9. #mainHeader ul li {
  10. display: inline-block;
  11. padding: 0 0.8em;
  12. }
  13. #mainHeader ul li a {
  14. color: #38e;
  15. }
  16. #mainHeader ul li a:hover {
  17. color: black;
  18. }
  19. #mainHeader .serchInfo {
  20. padding: 0 1em;
  21. }
  22. #mainHeader .serchInfo a {
  23. vertical-align: middle;
  24. background: #38e;
  25. padding: 0.3em 0.6em;
  26. color: white;
  27. }
  28. #mainHeader .serchInfo a::after {
  29. content: "\f001";
  30. font-family: icon;
  31. }
  32. #mainHeader .serchInfo input {
  33. border: solid 1px silver;
  34. padding: 0.5em;
  35. outline: none;
  36. vertical-align: middle;
  37. border-radius: 0.3em 0 0 0.3em;
  38. }
  39. #mainHeader > p {
  40. margin-left: auto;
  41. }
  42. #mainHeader > p a {
  43. color: #38e;
  44. margin: 0 0.6em;
  45. }
  46. .secCon1 {
  47. height: 480px;
  48. }
  49. .secCon1 ul {
  50. width: 200px;
  51. background-color: gray;
  52. }
  53. .secCon1 ul li {
  54. background-color: gray;
  55. list-style: none;
  56. }
  57. .secCon1 ul li a {
  58. display: block;
  59. padding: 0.8em 1.5em;
  60. color: white;
  61. }
  62. .secCon1 ul li a:hover {
  63. background-color: rgba(255, 255, 255, 0.2);
  64. }
  65. .secCon1 .preview {
  66. position: relative;
  67. height: 100%;
  68. }
  69. .secCon1 .preview .applyFlex {
  70. position: relative;
  71. height: 100%;
  72. }
  73. .secCon1 .preview img {
  74. position: absolute;
  75. left: 0;
  76. top: 0;
  77. width: 100%;
  78. height: 100%;
  79. object-fit: cover;
  80. }
  81. .secCon1 .preview p {
  82. position: absolute;
  83. width: 100%;
  84. top: 50%;
  85. left: 0;
  86. z-index: 1;
  87. }
  88. .secCon1 .preview p a {
  89. position: absolute;
  90. width: 30px;
  91. height: 30px;
  92. }
  93. .secCon1 .preview p a:first-of-type {
  94. border: solid 2px white;
  95. border-style: solid none none solid;
  96. transform: rotate(-45deg);
  97. left: 1em;
  98. }
  99. .secCon1 .preview p a:last-of-type {
  100. border: solid 2px white;
  101. border-style: none solid solid none;
  102. transform: rotate(-45deg);
  103. right: 1em;
  104. }
  105. .secCon1 .controler {
  106. position: absolute;
  107. right: 1em;
  108. bottom: 1em;
  109. }
  110. .secCon1 .controler span {
  111. display: inline-block;
  112. width: 9px;
  113. height: 9px;
  114. background: white;
  115. border-radius: 1em;
  116. margin: 0 1em;
  117. cursor: pointer;
  118. opacity: 0.5;
  119. }
  120. .secCon1 .controler span:hover {
  121. opacity: 1;
  122. }
  123. .secCon2 {
  124. }
  125. .secCon2 h1 {
  126. font-size: 26px;
  127. padding: 0.4em 0;
  128. }
  129. .secCon2 h1 small {
  130. font-size: 14px;
  131. color: gray;
  132. }
  133. .secCon2 h1 span {
  134. }
  135. .secCon2 h1 small {
  136. font-size: 16px;
  137. color: gray;
  138. }
  139. .secCon2 h1 span {
  140. font-size: 14px;
  141. color: gray;
  142. }
  143. .secCon2 h1 span a {
  144. margin: 0 0.6em;
  145. }
  146. .secCon2 h1 > p {
  147. margin-left: auto;
  148. font-size: 14px;
  149. }
  150. .secCon2 > div {
  151. text-align: center;
  152. }
  153. .secCon2 h1 > p a {
  154. color: gray;
  155. }
  156. .secCon2 > div {
  157. text-align: center;
  158. }
  159. .secCon2 > div p {
  160. background: gray;
  161. width: 20%;
  162. padding: 3em 0;
  163. border-radius: 0.4em;
  164. }
  165. .secCon2 .con2 {
  166. margin: 1em auto;
  167. }
  168. .secCon2 .con2 > div {
  169. width: 210px;
  170. }
  171. .secCon2 .con2 span {
  172. padding: 0.4em 0 0;
  173. line-height: 2;
  174. }
  175. .secCon2 .con2 > div p {
  176. width: 100%;
  177. }
  178. .mainfooter {
  179. background-color: gray;
  180. padding: 2em;
  181. }
  182. .mainfooter p a {
  183. color: #000;
  184. }
  185. .mainfooter .copyrightInfo {
  186. padding-top: 2em;
  187. }

3.首页样式文件 index.css

  1. #mainHeader {
  2. background: white;
  3. padding: 1.5em;
  4. box-shadow: 0 0.2em 0.4em rgba(0, 0, 0, 0.2);
  5. }
  6. #mainHeader ul {
  7. padding: 0 1em;
  8. }
  9. #mainHeader ul li {
  10. display: inline-block;
  11. padding: 0 0.8em;
  12. }
  13. #mainHeader ul li a {
  14. color: #38e;
  15. }
  16. #mainHeader ul li a:hover {
  17. color: black;
  18. }
  19. #mainHeader .serchInfo {
  20. padding: 0 1em;
  21. }
  22. #mainHeader .serchInfo a {
  23. vertical-align: middle;
  24. background: #38e;
  25. padding: 0.3em 0.6em;
  26. color: white;
  27. }
  28. #mainHeader .serchInfo a::after {
  29. content: "\f001";
  30. font-family: icon;
  31. }
  32. #mainHeader .serchInfo input {
  33. border: solid 1px silver;
  34. padding: 0.5em;
  35. outline: none;
  36. vertical-align: middle;
  37. border-radius: 0.3em 0 0 0.3em;
  38. }
  39. #mainHeader > p {
  40. margin-left: auto;
  41. }
  42. #mainHeader > p a {
  43. color: #38e;
  44. margin: 0 0.6em;
  45. }
  46. .secCon1 {
  47. height: 480px;
  48. }
  49. .secCon1 ul {
  50. width: 200px;
  51. background-color: gray;
  52. }
  53. .secCon1 ul li {
  54. background-color: gray;
  55. list-style: none;
  56. }
  57. .secCon1 ul li a {
  58. display: block;
  59. padding: 0.8em 1.5em;
  60. color: white;
  61. }
  62. .secCon1 ul li a:hover {
  63. background-color: rgba(255, 255, 255, 0.2);
  64. }
  65. .secCon1 .preview {
  66. position: relative;
  67. height: 100%;
  68. }
  69. .secCon1 .preview .applyFlex {
  70. position: relative;
  71. height: 100%;
  72. }
  73. .secCon1 .preview img {
  74. position: absolute;
  75. left: 0;
  76. top: 0;
  77. width: 100%;
  78. height: 100%;
  79. object-fit: cover;
  80. }
  81. .secCon1 .preview p {
  82. position: absolute;
  83. width: 100%;
  84. top: 50%;
  85. left: 0;
  86. z-index: 1;
  87. }
  88. .secCon1 .preview p a {
  89. position: absolute;
  90. width: 30px;
  91. height: 30px;
  92. }
  93. .secCon1 .preview p a:first-of-type {
  94. border: solid 2px white;
  95. border-style: solid none none solid;
  96. transform: rotate(-45deg);
  97. left: 1em;
  98. }
  99. .secCon1 .preview p a:last-of-type {
  100. border: solid 2px white;
  101. border-style: none solid solid none;
  102. transform: rotate(-45deg);
  103. right: 1em;
  104. }
  105. .secCon1 .controler {
  106. position: absolute;
  107. right: 1em;
  108. bottom: 1em;
  109. }
  110. .secCon1 .controler span {
  111. display: inline-block;
  112. width: 9px;
  113. height: 9px;
  114. background: white;
  115. border-radius: 1em;
  116. margin: 0 1em;
  117. cursor: pointer;
  118. opacity: 0.5;
  119. }
  120. .secCon1 .controler span:hover {
  121. opacity: 1;
  122. }
  123. .secCon2 {
  124. }
  125. .secCon2 h1 {
  126. font-size: 26px;
  127. padding: 0.4em 0;
  128. }
  129. .secCon2 h1 small {
  130. font-size: 14px;
  131. color: gray;
  132. }
  133. .secCon2 h1 span {
  134. }
  135. .secCon2 h1 small {
  136. font-size: 16px;
  137. color: gray;
  138. }
  139. .secCon2 h1 span {
  140. font-size: 14px;
  141. color: gray;
  142. }
  143. .secCon2 h1 span a {
  144. margin: 0 0.6em;
  145. }
  146. .secCon2 h1 > p {
  147. margin-left: auto;
  148. font-size: 14px;
  149. }
  150. .secCon2 > div {
  151. text-align: center;
  152. }
  153. .secCon2 h1 > p a {
  154. color: gray;
  155. }
  156. .secCon2 > div {
  157. text-align: center;
  158. }
  159. .secCon2 > div p {
  160. background: gray;
  161. width: 20%;
  162. padding: 3em 0;
  163. border-radius: 0.4em;
  164. }
  165. .secCon2 .con2 {
  166. margin: 1em auto;
  167. }
  168. .secCon2 .con2 > div {
  169. width: 210px;
  170. }
  171. .secCon2 .con2 span {
  172. padding: 0.4em 0 0;
  173. line-height: 2;
  174. }
  175. .secCon2 .con2 > div p {
  176. width: 100%;
  177. }
  178. .mainfooter {
  179. background-color: gray;
  180. padding: 2em;
  181. }
  182. .mainfooter p a {
  183. color: #000;
  184. }
  185. .mainfooter .copyrightInfo {
  186. padding-top: 2em;
  187. }

flex 布局网站首页 运行截图

flex

5. flex 媒体查询

  • 演示代码:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>flex媒体查询</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. }
  12. /* flex媒体查询:根据用户设备,跨终端显示页面 */
  13. .container {
  14. display: flex;
  15. /* 第一:必须允许创建多行容器 */
  16. flex-flow: row wrap;
  17. }
  18. /* flexBox项目 */
  19. .container > .item {
  20. /* flex:是否允许放大,是否允许收缩,基础尺寸大小 */
  21. flex: 1 1 150px;
  22. height: 40px;
  23. background-color: lightslategray;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="container">
  29. <div class="item">1</div>
  30. <div class="item">2</div>
  31. <div class="item">3</div>
  32. <div class="item">4</div>
  33. </div>
  34. </body>
  35. </html>

运行截图
媒体查询

6.总结

  • flex 媒体查询:根据用户设备,跨终端显示页面,非常适合布局手机端页面实现自适应
  • flex 布局也能布局 PC 页面,主要是一维排版,主要考虑的是横向或竖向排版,有自身局限性
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议