博客列表 >PC端与移动端的布局案例-web前端第10章6.23

PC端与移动端的布局案例-web前端第10章6.23

希望
希望原创
2020年07月02日 15:22:361004浏览

一.PC端布局解决方案

  1. 头部,主体,脚部,按照垂直排列
  2. 主体三列布局,做水平居中
  3. logo放左边,与其它栏目拉开距离,不给鼠标悬停效果
  4. 登录放右边
  5. 脚部居中
  6. 注意都要转为弹性盒子
  • 如下图:
    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>pc端布局</title>
    7. <style>
    8. * {
    9. margin: 0;
    10. padding: 0;
    11. box-sizing: border-box;
    12. }
    13. a {
    14. color: #666;
    15. text-decoration: none;
    16. }
    17. body {
    18. min-width: 680;
    19. display: flex;
    20. /* 主轴垂直方向,不换行 */
    21. flex-flow: column nowrap;
    22. }
    23. header,
    24. footer {
    25. height: 50px;
    26. border: 1px solid#000;
    27. }
    28. /* 把页眉转为弹性盒子 */
    29. header {
    30. display: flex;
    31. /* 所有栏目在交叉轴上居中 */
    32. align-items: center;
    33. }
    34. header > a {
    35. flex: 0 1 100px;
    36. /* 文本在当前a标签的栏目上居中 */
    37. text-align: center;
    38. }
    39. /* logo是当前第一个,把它和栏目稍拉开些距离 */
    40. header > a:first-of-type {
    41. margin-right: 50px;
    42. }
    43. /* 登录要放最右边去,它是最后一个 */
    44. header > a:last-of-type {
    45. margin-left: auto;
    46. }
    47. /* 鼠标悬停效果,排除logo */
    48. header > a:hover:not(:first-of-type) {
    49. color: coral;
    50. }
    51. /* 把主体变成水平排列 */
    52. .container {
    53. min-height: 800px;
    54. /* 居中起来,给个边框 */
    55. margin: 10px auto;
    56. display: flex;
    57. justify-content: center;
    58. }
    59. .container > aside,
    60. .container > main {
    61. border: 1px solid #000;
    62. padding: 10px;
    63. }
    64. .container > aside {
    65. flex: 0 0 200px;
    66. }
    67. .container > main {
    68. flex: 0 0 600px;
    69. margin: 0 10px;
    70. }
    71. footer {
    72. display: flex;
    73. flex-flow: column nowrap;
    74. text-align: center;
    75. }
    76. </style>
    77. </head>
    78. <body>
    79. <!-- 页眉 -->
    80. <header>
    81. <a href="">LOGO</a>
    82. <a href="">首页</a>
    83. <a href="">产品</a>
    84. <a href="">新闻</a>
    85. <a href="">我们</a>
    86. <a href="">登录</a>
    87. </header>
    88. <!-- 主体 -->
    89. <div class="container">
    90. <aside>左边栏</aside>
    91. <main>主体内容</main>
    92. <aside>右边栏</aside>
    93. </div>
    94. <!-- 页脚 -->
    95. <footer>
    96. <p>
    97. php中文网©版权所有 (2018-2020)| 备案号:
    98. <a href="">皖ICP123456**</a>
    99. </p>
    100. <p>
    101. 合肥市政务新区999号 | 电话:0551-12345***
    102. </p>
    103. </footer>
    104. </body>
    105. </html>

    二.移动端小商城

  1. 左上角一般放logo,右上角是下拉导航
  2. 轮播图
  3. 分类导航,小图片+文字,上下排列
  4. 热销商品推荐,图片+商品名称+价格+购物车,上下排列
  5. 商品列表,商品标题及相关描述,左右排列
  6. 脚部:首页,分类,购物车,登录,阿里图标+文字,上下排列
  • 如下图:
    移动端布局方案
  • 代码如下:

    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. <link rel="stylesheet" href="static/css/font-icon.css" />
    7. <title>移动端小商城</title>
    8. <style>
    9. * {
    10. margin: 0;
    11. padding: 0;
    12. box-sizing: border-box;
    13. }
    14. a {
    15. /* a链接下划线去掉 */
    16. text-decoration: none;
    17. color: #666;
    18. }
    19. html {
    20. /* vw当前可视区宽度 */
    21. /* vh当前可视区宽度 */
    22. width: 100vw;
    23. height: 100vh;
    24. font-size: 14px;
    25. color: #666;
    26. }
    27. body {
    28. min-width: 360px;
    29. background-color: #fff;
    30. display: flex;
    31. /* 主轴方向,列为主 */
    32. flex-flow: column nowrap;
    33. }
    34. /* 头部样式 */
    35. body > header {
    36. color: white;
    37. background-color: #333;
    38. height: 30px;
    39. display: flex;
    40. align-items: center;
    41. justify-content: space-between;
    42. position: fixed;
    43. width: 100vw;
    44. padding: 0 15px;
    45. }
    46. /* 轮播图 */
    47. body > .slider {
    48. height: 180px;
    49. }
    50. body > .slider > img {
    51. height: 100%;
    52. }
    53. /* 导航区 */
    54. nav {
    55. height: 200px;
    56. margin-bottom: 10px;
    57. display: flex;
    58. /* 水平排列要换行 */
    59. flex-flow: row wrap;
    60. align-content: space-around;
    61. }
    62. nav > div {
    63. width: 25vw;
    64. display: flex;
    65. /* 图片加文字垂直排 */
    66. flex-flow: column nowrap;
    67. align-items: center;
    68. }
    69. /* 导航区图片变小居中处理 */
    70. nav > div > a:first-of-type {
    71. text-align: center;
    72. }
    73. nav > div img {
    74. width: 50%;
    75. }
    76. /* 每个区域的标题样式 */
    77. .title {
    78. margin-top: 10px;
    79. font-size: 1.2rem;
    80. font-weight: normal;
    81. text-align: center;
    82. }
    83. /* 热销商品区 */
    84. .hot-goods {
    85. border-top: 1px solid #cdcdcd;
    86. margin-top: 10px;
    87. font-size: 0.8rem;
    88. display: flex;
    89. /* 水平多行容器 */
    90. flex-flow: row wrap;
    91. }
    92. .hot-goods img {
    93. width: 100%;
    94. }
    95. .hot-goods > .goods-img {
    96. /* 内边距并重置大小 */
    97. padding: 10px;
    98. box-sizing: border-box;
    99. /* 允许放大不允许缩小,否则项目不会换行,多行容器失效 */
    100. flex: 1 0 30vw;
    101. /* 再将每个商品描述转为flex */
    102. display: flex;
    103. /* 主轴垂直且不允许换行 */
    104. flex-flow: column nowrap;
    105. justify-content: center;
    106. }
    107. /* 商品描述的最后一个区域转flex,并设置项目在主轴上排列对齐方式 */
    108. .hot-goods > .goods-img > div {
    109. display: flex;
    110. /* 分散对齐 */
    111. justify-content: space-around;
    112. }
    113. /* 热销样式 */
    114. .hot {
    115. color: coral;
    116. }
    117. /* 商品列表区 */
    118. .list-goods {
    119. padding: 10px;
    120. border-top: 1px solid #cdcdcd;
    121. margin-top: 10px;
    122. font-size: 0.8rem;
    123. display: flex;
    124. /* 主轴必须是垂直 */
    125. flex-flow: column nowrap;
    126. }
    127. /* 每个项目也转为flex */
    128. .list-goods > .goods-desc {
    129. margin: 10px 0;
    130. display: flex;
    131. }
    132. /* 列表中每个项目的样式,加些间距 */
    133. .list-goods > .goods-desc > a {
    134. padding: 10px;
    135. box-sizing: border-box;
    136. }
    137. .list-goods > .goods-desc > a:last-of-type:hover {
    138. color: lightseagreen;
    139. }
    140. /* 图片全部适应项目空间 */
    141. .list-goods img {
    142. width: 100%;
    143. }
    144. body > footer {
    145. color: #666;
    146. background-color: #efefef;
    147. border-top: 1px solid #ccc;
    148. height: 55px;
    149. position: fixed;
    150. bottom: 0;
    151. width: 100vw;
    152. display: flex;
    153. justify-content: space-evenly;
    154. }
    155. body > footer a {
    156. margin-top: 10px;
    157. font-size: 0.8rem;
    158. display: flex;
    159. /* 垂直排列不换行 */
    160. flex-flow: column nowrap;
    161. /* 交叉轴项目居中显示 */
    162. align-items: center;
    163. }
    164. body > footer a > span:first-of-type {
    165. /* 图标字体应该大一些 */
    166. font-size: 1.6rem;
    167. }
    168. /* 使用九宫格来演示: grid布局实现 */
    169. .container {
    170. width: 100px;
    171. height: 100px;
    172. display: grid;
    173. grid-template-columns: repeat(3, 1fr);
    174. gap: 5px;
    175. }
    176. .item {
    177. width: 118px;
    178. height: 118px;
    179. display: flex;
    180. justify-content: center;
    181. align-items: center;
    182. }
    183. </style>
    184. </head>
    185. <body>
    186. <!-- 页眉 -->
    187. <header>
    188. <a href="">LOGO</a>
    189. <span class="iconfont"></span>
    190. </header>
    191. <!-- 轮播图 -->
    192. <div class="slider">
    193. <img src="static/images/banner.jpg" alt="" />
    194. </div>
    195. <!-- 主导航 -->
    196. <nav>
    197. <div>
    198. <a herf=""><img src="static/images/link1.webp"</a>
    199. <a href="">京东超市</a>
    200. </div>
    201. <div>
    202. <a herf=""><img src="static/images/link2.webp"</a>
    203. <a href="">服装百货</a>
    204. </div>
    205. <div>
    206. <a herf=""><img src="static/images/link3.webp"</a>
    207. <a href="">数码精品</a>
    208. </div>
    209. <div>
    210. <a herf=""><img src="static/images/link4.webp"</a>
    211. <a href="">优惠劵</a>
    212. </div>
    213. <div>
    214. <a herf=""><img src="static/images/link1.webp"</a>
    215. <a href="">超市精选</a>
    216. </div>
    217. <div>
    218. <a herf=""><img src="static/images/link2.webp"</a>
    219. <a href="">服装百货</a>
    220. </div>
    221. <div>
    222. <a herf=""><img src="static/images/link3.webp"</a>
    223. <a href="">数码精品</a>
    224. </div>
    225. <div>
    226. <a herf=""><img src="static/images/link4.webp"</a>
    227. <a href="">优惠劵</a>
    228. </div>
    229. </nav>
    230. <!-- 热销商品 -->
    231. <h2>
    232. 热销商品<span class="iconfont hot" style="color: coral;"
    233. ></span
    234. >
    235. </h2>
    236. <!-- 商品 -->
    237. <div class="hot-goods">
    238. <div class="goods-img">
    239. <a href=""><img src="static/images/goods1.jpg" alt="" /></a>
    240. <p>Apple iPhone 11 128G</p>
    241. <div>
    242. <span>6299 元</span>
    243. <span class="iconfont hot"></span>
    244. </div>
    245. </div>
    246. <div class="goods-img">
    247. <a href=""><img src="static/images/goods1.jpg" alt="" /></a>
    248. <p>Apple iPhone X 512G</p>
    249. <div>
    250. <span>8299 元</span>
    251. <span class="iconfont hot"></span>
    252. </div>
    253. </div>
    254. <div class="goods-img">
    255. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    256. <p>华为笔记本电脑</p>
    257. <div>
    258. <span>5699 元</span>
    259. <span class="iconfont hot"></span>
    260. </div>
    261. </div>
    262. <div class="goods-img">
    263. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    264. <p>小米笔记本电脑</p>
    265. <div>
    266. <span>3999 元</span>
    267. <span class="iconfont hot"></span>
    268. </div>
    269. </div>
    270. <div class="goods-img">
    271. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    272. <p>联想笔记本电脑</p>
    273. <div>
    274. <span>4399 元</span>
    275. <span class="iconfont hot"></span>
    276. </div>
    277. </div>
    278. </div>
    279. <!-- 商品列表区 -->
    280. <h2 class="title">
    281. 商品列表<span class="iconfont hot" style="color: coral;"></span>
    282. </h2>
    283. <div class="container">
    284. <div class="item">
    285. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    286. </div>
    287. <div class="item">
    288. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    289. </div>
    290. <div class="item">
    291. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    292. </div>
    293. <span class="item">
    294. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    295. </span>
    296. <span class="item">
    297. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    298. </span>
    299. <span class="item">
    300. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    301. </span>
    302. <span class="item">
    303. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    304. </span>
    305. <span class="item">
    306. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    307. </span>
    308. <span class="item">
    309. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    310. </span>
    311. </div>
    312. <!-- 页脚 -->
    313. <footer>
    314. <a href="">
    315. <span class="iconfont hot"></span>
    316. <span>首页</span>
    317. </a>
    318. <a href="">
    319. <span class="iconfont hot"></span>
    320. <span>导航</span>
    321. </a>
    322. <a href="">
    323. <span class="iconfont hot"></span>
    324. <span>购物车</span>
    325. </a>
    326. <a href="">
    327. <span class="iconfont hot"></span>
    328. <span>登录</span>
    329. </a>
    330. </footer>
    331. </body>
    332. </html>
  • 备注:九宫格插入商品的图片,标题,描述,价格等,还不会如何实现,继续学习中
  • 2020.7.2 补上九宫格商品区,demo.html代码如下:
    1. <h2>
    2. 商品列表
    3. </h2>
    4. <div class="container">
    5. <div class="item">
    6. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    7. <p>惠普笔记本电脑-256G-16寸</p>
    8. <span>价格:8000元</span>
    9. </div>
    10. <div class="item">
    11. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    12. <p>惠普笔记本电脑-256G-16寸</p>
    13. <span>价格:8000元</span>
    14. </div>
    15. <div class="item">
    16. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    17. <p>惠普笔记本电脑-256G-16寸</p>
    18. <span>价格:8000元</span>
    19. </div>
    20. <div class="item">
    21. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    22. <p>惠普笔记本电脑-256G-16寸</p>
    23. <span>价格:8000元</span>
    24. </div>
    25. <div class="item">
    26. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    27. <p>惠普笔记本电脑-256G-16寸</p>
    28. <span>价格:8000元</span>
    29. </div>
    30. <div class="item">
    31. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    32. <p>惠普笔记本电脑-256G-16寸</p>
    33. <span>价格:8000元</span>
    34. </div>
    35. <div class="item">
    36. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    37. <p>惠普笔记本电脑-256G-16寸</p>
    38. <span>价格:8000元</span>
    39. </div>
    40. <div class="item">
    41. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    42. <p>惠普笔记本电脑-256G-16寸</p>
    43. <span>价格:8000元</span>
    44. </div>
    45. <div class="item">
    46. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    47. <p>惠普笔记本电脑-256G-16寸</p>
    48. <span>价格:8000元</span>
    49. </div>
    50. </div>
  • 九宫格demo.css代码如下:
    1. .container {
    2. width: 350px;
    3. height: 350px;
    4. margin: auto;
    5. background-color: rgb(241, 238, 238);
    6. display: grid;
    7. grid-template-columns: repeat(3, 1fr);
    8. gap: 2px;
    9. }
    10. .item {
    11. width: 100%;
    12. height: 100%;
    13. background-color: rgb(246, 252, 252);
    14. display: grid;
    15. justify-content: center;
    16. align-items: center;
    17. }
    18. .container > .item img {
    19. width: 100%;
    20. height: 100%;
    21. }
    22. .container > .item p {
    23. font-size: 10px;
    24. margin-left: 5px;
    25. }
    26. .container > .item span {
    27. font-size: 10px;
    28. margin-left: 5px;
    29. margin: auto;
    30. }
  • 效果图如下:

九客格

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