Blogger Information
Blog 53
fans 3
comment 0
visits 46957
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
前端Flex实现的PC端布局和移动端布局的基本方案
emagic
Original
959 people have browsed it

0623作业

一、 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. * {
  10. padding: 0;
  11. margin: 0;
  12. box-sizing: border-box;
  13. }
  14. a {
  15. /* 常规操作是去掉a标签的下划线,然后改一下字体颜色 */
  16. text-decoration: none;
  17. color: #555;
  18. }
  19. body {
  20. /* 将body转为flex */
  21. display: flex;
  22. min-width: 480px;
  23. /* 设置主轴垂直 ,不换行*/
  24. flex-flow: column nowrap;
  25. }
  26. header,
  27. footer {
  28. height: 50px;
  29. border: 1px solid #000;
  30. }
  31. /* 将页眉转为盒子 */
  32. nav {
  33. display: flex;
  34. background-color: skyblue;
  35. /* 让所有项目在j交叉轴方向(垂直轴)方向居中显示 */
  36. align-items: center;
  37. }
  38. nav a {
  39. flex: 0 1 100px;
  40. /* 文本居中 */
  41. text-align: center;
  42. }
  43. /* 设置logo的边距 */
  44. nav a:first-of-type {
  45. margin-right: 60px;
  46. }
  47. nav a:last-of-type {
  48. margin-left: auto;
  49. }
  50. nav a:not(:first-of-type):hover {
  51. /* 导航文字悬停效果,logo无需变色 */
  52. color: white;
  53. }
  54. .container {
  55. display: flex;
  56. min-height: 500px;
  57. margin: 10px auto;
  58. justify-content: center;
  59. }
  60. .container > section,
  61. .container > main {
  62. border: 1px solid #000;
  63. padding: 10px;
  64. }
  65. .container > section {
  66. flex: 0 0 200px;
  67. }
  68. .container > main {
  69. flex: 0 0 400px;
  70. margin: 0 10px;
  71. }
  72. footer {
  73. display: flex;
  74. flex-flow: column nowrap;
  75. text-align: center;
  76. }
  77. </style>
  78. </head>
  79. <body>
  80. <!-- 页眉 -->
  81. <header>
  82. <nav>
  83. <a href="">LOGO MY HOUSE</a>
  84. <a href="">首页</a>
  85. <a href="">客厅玄关</a>
  86. <a href="">卫生间</a>
  87. <a href="">卧室布局</a>
  88. <a href="">厨房空间</a>
  89. <a href="">购物车</a>
  90. <a href="">注册/登录</a>
  91. </nav>
  92. </header>
  93. <!-- 主体 -->
  94. <div class="container">
  95. <!-- 左边栏 -->
  96. <section>左边商品分类栏</section>
  97. <!-- 主体内容区 -->
  98. <main>主体内容区</main>
  99. <!-- 右边栏 -->
  100. <section>右边广告投放栏</section>
  101. </div>
  102. <!-- 页脚 -->
  103. <footer>
  104. <p>
  105. Emagic社区论坛 © 版权所有 (2020)| 备案号:
  106. <a href="">粤IPC-20xxxxxxxx</a>
  107. </p>
  108. <p>中国 广东佛山市南海区999号|电话:0757-123456789</p>
  109. </footer>
  110. </body>
  111. </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>flex弹性盒子实现移动端的布局</title>
  7. <link rel="stylesheet" href="static/css/iconfont.css" />
  8. <link rel="stylesheet" href="static/css/mobliestyle.css" />
  9. </head>
  10. <body>
  11. <!-- 页眉 -->
  12. <header>
  13. <!-- logo一般放在一个链接里面 -->
  14. <a href="">LOGO</a>
  15. <!-- 阿里字体图标引用后,用一个class="iconfont"以16进制字体样式使用 -->
  16. <span class="iconfont">&#xe6e9;</span>
  17. </header>
  18. <!-- 轮播图 -->
  19. <div class="slider">
  20. <img src="static/image/banner.jpg" alt="" />
  21. </div>
  22. <!-- 主导航 -->
  23. <nav>
  24. <div>
  25. <!-- 注意,图片是可以点击的,所以要放到a标签里面去 -->
  26. <a href=""><img src="static/image/img1.png" alt="" /></a>
  27. <a href="">特优超市</a>
  28. </div>
  29. <div>
  30. <a href=""><img src="static/image/img2.png" alt="" /></a>
  31. <a href="">数码商城</a>
  32. </div>
  33. <div>
  34. <a href=""><img src="static/image/img3.png" alt="" /></a>
  35. <a href="">时尚服饰</a>
  36. </div>
  37. <div>
  38. <a href=""><img src="static/image/img4.png" alt="" /></a>
  39. <a href="">生鲜超市</a>
  40. </div>
  41. <div>
  42. <a href=""><img src="static/image/img5.png" alt="" /></a>
  43. <a href="">送货配送</a>
  44. </div>
  45. <div>
  46. <a href=""><img src="static/image/img6.png" alt="" /></a>
  47. <a href="">会员充值</a>
  48. </div>
  49. <div>
  50. <a href=""><img src="static/image/img7.png" alt="" /></a>
  51. <a href="">品牌闪购</a>
  52. </div>
  53. <div>
  54. <a href=""><img src="static/image/img8.png" alt="" /></a>
  55. <a href="">优惠券</a>
  56. </div>
  57. <div>
  58. <a href=""><img src="static/image/img9.png" alt="" /></a>
  59. <a href="">VIP会员</a>
  60. </div>
  61. <div>
  62. <a href=""><img src="static/image/img10.png" alt="" /></a>
  63. <a href="">生鲜超市</a>
  64. </div>
  65. </nav>
  66. <!-- 热销商品 -->
  67. <h2>
  68. 热销商品
  69. <span class="iconfont hotboom" style="color: tomato;">&#xe54c;</span>
  70. </h2>
  71. <div class="hot-goods">
  72. <div class="goods-img">
  73. <a href=""><img src="static/image/i1.jpg" alt="" /></a>
  74. <p>华为Nova6 5G手机</p>
  75. <div>
  76. <span>2388&nbsp;元</span>
  77. <span class="iconfont" style="color: red;">&#xe50b;</span>
  78. </div>
  79. </div>
  80. <div class="goods-img">
  81. <a href=""><img src="static/image/i2.jpg" alt="" /></a>
  82. <p>华为笔记本电脑i7独显</p>
  83. <div>
  84. <span>6699&nbsp;元</span>
  85. <span class="iconfont" style="color: red;">&#xe50b;</span>
  86. </div>
  87. </div>
  88. <div class="goods-img">
  89. <a href=""><img src="static/image/i3.jpg" alt="" /></a>
  90. <p>B&O 蓝牙便携音响A2</p>
  91. <div>
  92. <span>998&nbsp;元</span>
  93. <span class="iconfont" style="color: red;">&#xe50b;</span>
  94. </div>
  95. </div>
  96. <div class="goods-img">
  97. <a href=""><img src="static/image/i4.jpg" alt="" /></a>
  98. <p>云端摄像头</p>
  99. <div>
  100. <span>158&nbsp;元</span>
  101. <span class="iconfont" style="color: red;">&#xe50b;</span>
  102. </div>
  103. </div>
  104. <div class="goods-img">
  105. <a href=""><img src="static/image/i5.jpg" alt="" /></a>
  106. <p>电脑扩展接口,支持多种转换</p>
  107. <div>
  108. <span>88&nbsp;元</span>
  109. <span class="iconfont" style="color: red;">&#xe50b;</span>
  110. </div>
  111. </div>
  112. <div class="goods-img">
  113. <a href=""><img src="static/image/i6.jpg" alt="" /></a>
  114. <p>国家地理画册,科普图书优惠</p>
  115. <div>
  116. <span>138&nbsp;元</span>
  117. <span class="iconfont" style="color: red;">&#xe50b;</span>
  118. </div>
  119. </div>
  120. </div>
  121. <!-- 商品列表区 -->
  122. <h2>
  123. 商品列表
  124. <span class="iconfont" style="color: tomato;">&#xe788;</span>
  125. </h2>
  126. <div class="list-goods">
  127. <div class="goods-desc">
  128. <a href=""><img src="static/image/goods4.jpg" alt="" /></a>
  129. <a href=""
  130. >[白条24期免息]Apple苹果iPhone 11 手机 128G 全网通, 免费领取500元话费,
  131. 今天17:00下单,明晨12:00之前送达,7天无理由退货,官方提供售后,
  132. 以上都是我瞎编的<span
  133. class="iconfont hotboom"
  134. style="vertical-align: middle;"
  135. >&#xe50b;</span
  136. ></a
  137. >
  138. </div>
  139. <div class="goods-desc">
  140. <a href=""><img src="static/image/goods3.jpg" alt="" /></a>
  141. <a href=""
  142. >[白条24期免息]Apple洗衣机,专业清洗苹果手机,
  143. 苹果电脑,iPad,洗好保证不能用, 免费领取500元保险费,
  144. 今天17:00下单,明晨12:00之前送达,7天无理由退货,官方提供售后,
  145. 以上都是我瞎编的<span
  146. class="iconfont hotboom"
  147. style="vertical-align: middle;"
  148. >&#xe50b;</span
  149. ></a
  150. >
  151. </div>
  152. <div class="goods-desc">
  153. <a href=""><img src="static/image/goods5.png" alt="" /></a>
  154. <a href=""
  155. >[白条24期免息]Apple苹果iPhone 11 手机 128G 全网通, 免费领取500元话费,
  156. 今天17:00下单,明晨12:00之前送达,7天无理由退货,官方提供售后,
  157. 以上都是我瞎编的<span
  158. class="iconfont hotboom"
  159. style="vertical-align: middle;"
  160. >&#xe50b;</span
  161. ></a
  162. >
  163. </div>
  164. <div class="goods-desc">
  165. <a href=""><img src="static/image/goods2.jpg" alt="" /></a>
  166. <a href=""
  167. >[白条24期免息]华为笔记本MateBook 14 全面屏轻薄性能笔记本电脑
  168. 十代酷睿(i5 16G 512G MX250 触控屏 多屏协同)灰, 以上都是我瞎编的<span
  169. class="iconfont hotboom"
  170. style="vertical-align: middle;"
  171. >&#xe50b;</span
  172. ></a
  173. >
  174. </div>
  175. </div>
  176. <footer>
  177. <a href="">
  178. <!-- 因为图片是可以点进去的,所以要放入a标签内 -->
  179. <span class="iconfont hotboom">&#xe687;</span>
  180. <span>首页</span>
  181. </a>
  182. <a href="">
  183. <span class="iconfont hotboom">&#xe511;</span>
  184. <span>分类</span>
  185. </a>
  186. <a href="">
  187. <span class="iconfont hotboom">&#xe627;</span>
  188. <span>购物车</span>
  189. </a>
  190. <a href="">
  191. <span class="iconfont hotboom">&#xe501;</span>
  192. <span>抽奖</span>
  193. </a>
  194. <a href="">
  195. <span class="iconfont hotboom">&#xe628;</span>
  196. <span>登录</span>
  197. </a>
  198. </footer>
  199. </body>
  200. </html>
  1. /* 样式初始化三板斧 */
  2. * {
  3. padding: 0;
  4. margin: 0;
  5. box-sizing: border-box;
  6. }
  7. /* a链接初始化双雄 */
  8. a {
  9. text-decoration: none;
  10. color: #666;
  11. }
  12. /* html中要设置宽度自适应视口 */
  13. html {
  14. /* vw:可视区宽度,100指的是100份 ,vh是可视区的高度*/
  15. width: 100vw;
  16. height: 100vh;
  17. font-size: 15px;
  18. color: #666;
  19. }
  20. body {
  21. display: flex;
  22. flex-flow: column nowrap;
  23. background-color: white;
  24. min-width: 360px;
  25. }
  26. body > header {
  27. /* 文本设置为白色 */
  28. color: white;
  29. background-color: #333;
  30. height: 30px;
  31. display: flex;
  32. align-items: center;
  33. justify-content: space-between;
  34. /* 头部滚动条滚动的时候不要动 */
  35. position: fixed;
  36. /* 注意,固定定位后会缩回去,所以要设置一下宽度 */
  37. width: 100vw;
  38. padding: 0 15px;
  39. }
  40. body > .slider {
  41. height: 180px;
  42. }
  43. body > .slider > img {
  44. width: 100%;
  45. }
  46. /* 主导航区 */
  47. nav {
  48. height: 200px;
  49. margin-bottom: 10px;
  50. display: flex;
  51. /* 允许换行,转为多行容器 */
  52. flex-flow: row wrap;
  53. align-content: space-evently;
  54. }
  55. nav div {
  56. /* 每排5个,一个就是20% */
  57. width: 20vw;
  58. display: flex;
  59. flex-flow: column nowrap;
  60. align-items: center;
  61. }
  62. /* 图片是第一个a标签 */
  63. nav > div > a:first-of-type {
  64. text-align: center;
  65. }
  66. nav > div img {
  67. width: 50%;
  68. }
  69. /* 每个区域的标题样式 */
  70. .title {
  71. margin-top: 10px;
  72. font-size: 1.2rem;
  73. font-weight: normal;
  74. text-align: center;
  75. }
  76. /* 热销商品区 */
  77. .hot-goods {
  78. border-top: 1px solid #cdcdcd;
  79. margin-top: 10px;
  80. font-size: 0.8rem;
  81. display: flex;
  82. /* 水平多行容器 */
  83. flex-flow: row wrap;
  84. }
  85. .hot-goods img {
  86. width: 100%;
  87. }
  88. .hot-goods > .goods-img {
  89. /* 内边距并重置大小 */
  90. padding: 10px;
  91. box-sizing: border-box;
  92. /* 允许放大不允许缩小,否则项目不会换行,多行容器失效 */
  93. flex: 1 0 30vw;
  94. /* 一行三个,所以是30vw */
  95. /* 再将每个商品描述转为flex */
  96. display: flex;
  97. /* 主轴垂直且不允许换行 */
  98. flex-flow: column nowrap;
  99. justify-content: center;
  100. }
  101. /* 商品描述的最后一个区域转flex,并设置项目在主轴上排列对齐方式 */
  102. .hot-goods > .goods-img > div {
  103. display: flex;
  104. /* 分散对齐 */
  105. justify-content: space-around;
  106. }
  107. /* 热销样式 */
  108. .hotboom {
  109. color: coral;
  110. }
  111. /* 商品列表区 */
  112. .list-goods {
  113. padding: 10px;
  114. border-top: 1px solid #cdcdcd;
  115. margin-top: 10px;
  116. font-size: 0.8rem;
  117. display: flex;
  118. /* 主轴是垂直的,上面是图片,下面是介绍 */
  119. flex-flow: column nowrap;
  120. }
  121. /* 每个项目也转为flex */
  122. .list-goods > .goods-desc {
  123. margin: 10px 0;
  124. display: flex;
  125. }
  126. /* 列表中每个项目的样式,加些间距 */
  127. .list-goods > .goods-desc > a {
  128. padding: 10px;
  129. box-sizing: border-box;
  130. }
  131. .list-goods > .goods-desc > a:last-of-type:hover {
  132. color: lightseagreen;
  133. }
  134. /* 图片全部适应项目空间 */
  135. .list-goods img {
  136. width: 100%;
  137. }
  138. body > footer {
  139. color: #666;
  140. background-color: #efefef;
  141. border-top: 1px solid #ccc;
  142. height: 55px;
  143. /* 页脚要固定显示,否则滚动下来就不见了 */
  144. position: fixed;
  145. bottom: 0;
  146. width: 100vw;
  147. display: flex;
  148. justify-content: space-around;
  149. /* 这里用space-evently也行 */
  150. }
  151. body > footer a {
  152. margin-top: 10px;
  153. /* font-size: 0.8rem; */
  154. display: flex;
  155. /* 垂直排列不换行 */
  156. flex-flow: column nowrap;
  157. /* 交叉轴项目居中显示 */
  158. align-items: center;
  159. }
  160. body > footer a > span:first-of-type {
  161. font-size: 1.6rem;
  162. }

小结:

将要布局的内容转换为盒子,盒子里面的项目也可以转变为子盒子,关键点在于:

1.主轴方向flex-direction是垂直(column)还是水平(row)。

2.盒子中的项目是否换行(wrap/nowrap),根据需要灵活变换。

  • justify-content  主轴单行
  • align-content    主轴多行
  • align-items        交叉轴
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:flex嵌套使用很常见的
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post