博客列表 >Flex PC端通用布局和移动端通用布局方案

Flex PC端通用布局和移动端通用布局方案

知行合一
知行合一原创
2020年07月01日 11:22:111409浏览

今天我们通过使用FLex布局实现PC端和移动端网页布局

Flex PC端通用布局方案


代码如下:

  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 PC端布局</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. body {
  14. display: flex;
  15. flex-flow: column nowrap;
  16. }
  17. header,
  18. footer {
  19. height: 50px;
  20. border: 1px salmon solid;
  21. width: 1000px;
  22. margin: 0 auto;
  23. }
  24. header {
  25. display: flex;
  26. align-items: center;
  27. }
  28. header>a {
  29. flex: 0 1 100px;
  30. text-align: center;
  31. }
  32. header>a:first-of-type {
  33. margin-right: 50px;
  34. }
  35. header>a:last-of-type {
  36. margin-left: auto;
  37. }
  38. header>a:hover:not(:first-of-type) {
  39. color: seagreen;
  40. }
  41. .container {
  42. display: flex;
  43. flex-flow: row nowrap;
  44. min-height: 400px;
  45. margin: 10px auto;
  46. justify-content: center;
  47. }
  48. .container>aside {
  49. background-color: seagreen;
  50. border: 1px salmon solid;
  51. padding: 10px;
  52. flex: 0 0 200px;
  53. }
  54. .container>main {
  55. background-color: tan;
  56. border: 1px salmon solid;
  57. flex: 0 0 580px;
  58. padding: 10px;
  59. margin: 0 10px;
  60. }
  61. footer {
  62. display: flex;
  63. flex-flow: column nowrap;
  64. align-items: center;
  65. }
  66. </style>
  67. </head>
  68. <body>
  69. <header>
  70. <a href="">网站首页</a>
  71. <a href="">栏目1</a>
  72. <a href="">栏目2</a>
  73. <a href="">栏目3</a>
  74. <a href="">栏目4</a>
  75. <a href="">栏目5</a>
  76. <a href="">登陆/注册</a>
  77. </header>
  78. <div class="container">
  79. <aside>左侧</aside>
  80. <main>据6月27日美媒报道,加州大学旧金山分校研究人员首次用显微镜拍摄到新冠病毒感染健康细胞的过程。专家称新冠病毒传播方式“十分邪恶” ,利用丝状伪足提高感染效率。</main>
  81. <aside>右侧</aside>
  82. </div>
  83. <footer>
  84. <p>PHP中文网版权所有</p>
  85. <p>地址:河南省郑州市</p>
  86. </footer>
  87. </body>
  88. </html>

flex 移动端通用布局方案

请忽略截图中 底部的内容在图片中间显示哦!

  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>flex 移动端布局</title>
  6. <link type="text/css" rel="stylesheet" href="//at.alicdn.com/t/font_1915214_vgr1grfl9jc.css" />
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. li {
  14. list-style: none;
  15. }
  16. html {
  17. width: 100vw;
  18. height: 100vh;
  19. font-size: 14px;
  20. color: #666;
  21. }
  22. a {
  23. color: #666;
  24. text-decoration: none;
  25. }
  26. a:hover {
  27. color: #666;
  28. }
  29. header {
  30. height: 30px;
  31. background-color: #333;
  32. display: flex;
  33. justify-content: space-between;
  34. padding: 0 10px;
  35. align-items: center;
  36. position: fixed;
  37. top: 0;
  38. left: 0;
  39. width: 100vw;
  40. }
  41. header>a {
  42. color: white;
  43. font-size: 18px;
  44. }
  45. header>a:hover {
  46. color: white;
  47. }
  48. header>span {
  49. color: white;
  50. }
  51. .slider>img {
  52. width: 100%;
  53. }
  54. .nav>ul {
  55. display: flex;
  56. flex-flow: row wrap;
  57. }
  58. .nav>ul>li {
  59. width: 20%;
  60. display: flex;
  61. flex-direction: column;
  62. margin: 10px 0;
  63. align-items: center;
  64. }
  65. .nav>ul>li a:first-of-type {
  66. font-size: 50px;
  67. color: tomato;
  68. }
  69. .hotpro {
  70. margin: 20px 0;
  71. padding: 0 10px;
  72. }
  73. .hotpro>.h2 {
  74. display: flex;
  75. padding-bottom: 5px;
  76. border-bottom: 1px dotted #333;
  77. }
  78. .hotpro>.h2 a {
  79. margin-left: auto;
  80. }
  81. .hotpro>ul {
  82. display: flex;
  83. flex-wrap: wrap;
  84. margin-top: 20px;
  85. }
  86. .hotpro>ul>li {
  87. width: 50%;
  88. display: flex;
  89. flex-direction: column;
  90. text-align: center;
  91. margin: 15px 0;
  92. }
  93. .hotpro>ul>li>a>img {
  94. max-width: 100%;
  95. display: block;
  96. }
  97. .hotpro>ul>li>div {
  98. display: flex;
  99. justify-content: space-around;
  100. }
  101. .hotpro>ul>li>div span:last-of-type {
  102. color: brown;
  103. }
  104. .newslist {
  105. margin-bottom: 50px;
  106. }
  107. .newslist h2 {
  108. text-align: center;
  109. height: 30px;
  110. line-height: 30px;
  111. }
  112. .newslist>ul {
  113. margin: 20px 0;
  114. }
  115. .newslist>ul>li {
  116. width: 100%;
  117. display: flex;
  118. border-bottom: 1px dotted #ccc;
  119. margin-bottom: 15px;
  120. }
  121. .newslist>ul>li a {
  122. display: block;
  123. }
  124. .newslist>ul>li p {}
  125. .newslist>ul>li img {
  126. max-width: 100%;
  127. }
  128. footer {
  129. display: flex;
  130. justify-content: space-around;
  131. position: fixed;
  132. bottom: 0;
  133. left: 0;
  134. width: 100vw;
  135. background-color: lightgrey;
  136. }
  137. footer>a {
  138. display: flex;
  139. flex-direction: column;
  140. text-align: center;
  141. }
  142. footer>a>i {
  143. font-size: 25px !important;
  144. color: brown;
  145. }
  146. </style>
  147. </head>
  148. <body>
  149. <header>
  150. <a href="">logo</a>
  151. <span class="iconfont icon-fenlei"></span>
  152. </header>
  153. <section class="slider"><img src="images/banner.jpg"></section>
  154. <section class="nav">
  155. <ul>
  156. <li><a href="" class="iconfont icon-shiwu"></a>
  157. <a href="">美味食物</a></li>
  158. <li><a href="" class="iconfont icon-_fuzhi-"></a>
  159. <a href="">精美水杯</a></li>
  160. <li><a href="" class="iconfont icon-yinliao"></a>
  161. <a href="">可口饮料</a></li>
  162. <li><a href="" class="iconfont icon--"></a>
  163. <a href="">新鲜蔬菜</a></li>
  164. <li><a href="" class="iconfont icon-danfan"></a>
  165. <a href="">高档数码</a></li>
  166. <li><a href="" class="iconfont icon-jiaju"></a>
  167. <a href="">古典家具</a></li>
  168. <li><a href="" class="iconfont icon-gongju"></a>
  169. <a href="">精品工具</a></li>
  170. <li><a href="" class="iconfont icon-huabancopy"></a>
  171. <a href="">设计软件</a></li>
  172. <li><a href="" class="iconfont icon-jixie"></a>
  173. <a href="">机械加工</a></li>
  174. <li><a href="" class="iconfont icon-wenju"></a>
  175. <a href="">学习文具</a></li>
  176. </ul>
  177. </section>
  178. <section class="hotpro">
  179. <div class="h2">
  180. <h2>热销商品</h2>
  181. <a href="">更多</a>
  182. </div>
  183. <ul>
  184. <li>
  185. <a href=""><img src="images/iphone.jpg"></a>
  186. <p>苹果手机ipone11 128G</p>
  187. <div><span>4999元</span><span class="iconfont icon-gouwuche"></span></div>
  188. </li>
  189. <li>
  190. <a href=""><img src="images/iphone.jpg"></a>
  191. <p>苹果手机ipone11 128G</p>
  192. <div><span>4999元</span><span class="iconfont icon-gouwuche"></span></div>
  193. </li>
  194. <li>
  195. <a href=""><img src="images/iphone.jpg"></a>
  196. <p>苹果手机ipone11 128G</p>
  197. <div><span>4999元</span><span class="iconfont icon-gouwuche"></span></div>
  198. </li>
  199. <li>
  200. <a href=""><img src="images/iphone.jpg"></a>
  201. <p>苹果手机ipone11 128G</p>
  202. <div><span>4999元</span><span class="iconfont icon-gouwuche"></span></div>
  203. </li>
  204. <li>
  205. <a href=""><img src="images/iphone.jpg"></a>
  206. <p>苹果手机ipone11 128G</p>
  207. <div><span>4999元</span><span class="iconfont icon-gouwuche"></span></div>
  208. </li>
  209. <li>
  210. <a href=""><img src="images/iphone.jpg"></a>
  211. <p>苹果手机ipone11 128G</p>
  212. <div><span>4999元</span><span class="iconfont icon-gouwuche"></span></div>
  213. </li>
  214. </ul>
  215. </section>
  216. <section class="newslist">
  217. <h2>新闻资讯</h2>
  218. <ul>
  219. <li><a href=""><img src="images/newsimg.jpg"></a>
  220. <p>[白条24期免息]Apple苹果iPhone 11 手机 128G 全网通, 免费领取500元话费, 今天17:00下单,明晨12:00之前送达,7天无理由退货,官方提供售后,
  221. 以上都是我瞎编</p>
  222. </li>
  223. <li><a href=""><img src="images/newsimg.jpg"></a>
  224. <p>[白条24期免息]Apple苹果iPhone 11 手机 128G 全网通, 免费领取500元话费, 今天17:00下单,明晨12:00之前送达,7天无理由退货,官方提供售后,
  225. 以上都是我瞎编</p>
  226. </li>
  227. <li><a href=""><img src="images/newsimg.jpg"></a>
  228. <p>[白条24期免息]Apple苹果iPhone 11 手机 128G 全网通, 免费领取500元话费, 今天17:00下单,明晨12:00之前送达,7天无理由退货,官方提供售后,
  229. 以上都是我瞎编</p>
  230. </li>
  231. </ul>
  232. </section>
  233. <footer>
  234. <a href=""><i class="iconfont icon-shouye"></i>
  235. <pan>首页</pan>
  236. </a>
  237. <a href=""><i class="iconfont icon-fenlei1"></i>
  238. <pan>分类</pan>
  239. </a>
  240. <a href=""><i class="iconfont icon-gouwuche1"></i>
  241. <pan>购物车</pan>
  242. </a>
  243. <a href=""><i class="iconfont icon-gongzhonghao"></i>
  244. <pan>微信</pan>
  245. </a>
  246. </footer>
  247. </body>
  248. </html>

总结

Flex布局比浮动和定位来布局,代码简洁,书写方便 推荐大家使用。

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