Blogger Information
Blog 16
fans 2
comment 0
visits 19890
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
pc端布局通用模板和移动端布局
肖傲的博客
Original
1036 people have browsed it

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. <!-- 加载字体 -->
  7. <title>pc布局的通用解决方案</title>
  8. </head>
  9. <style>
  10. /* 初始化 */
  11. * {
  12. margin: 0;
  13. padding: 0;
  14. box-sizing: border-box;
  15. }
  16. /* 去除下划线和设置颜色 */
  17. a {
  18. color: #ccc;
  19. text-decoration: none;
  20. }
  21. body {
  22. min-width: 600px;
  23. /* 设置弹性盒子 */
  24. display: flex;
  25. /* 主轴为垂直且不换行显示 */
  26. flex-flow: column nowrap;
  27. }
  28. header,
  29. footer {
  30. height: 60px;
  31. border: 1px solid #000;
  32. }
  33. /* 页眉区 */
  34. header {
  35. /* 设置弹性盒子 */
  36. display: flex;
  37. /* 项目在交叉轴居中显示 */
  38. align-items: center;
  39. background-color: #3c3c3c;
  40. }
  41. header > a {
  42. /* 不放大 可以收缩 宽度为100px */
  43. flex: 0 1 100px;
  44. text-align: center;
  45. }
  46. /* logo区 */
  47. /* 给第一个A标签设置一个向右的50px外边距 */
  48. header > a:first-of-type {
  49. margin-right: 50px;
  50. }
  51. /* 设置最后一个A标签靠右居中 */
  52. header > a:last-of-type {
  53. margin-left: auto;
  54. }
  55. /* 设置除了第一个a标签以外其他a标签鼠标悬停后颜色改变 */
  56. header > a:hover:not(:first-of-type) {
  57. color: white;
  58. }
  59. /* 主体区 */
  60. .container {
  61. /* 设置弹性容器 */
  62. display: flex;
  63. /* 居中 上下10px的外边距 */
  64. margin: 10px auto;
  65. /* 在没有内容的时候设置最小高度撑开盒子 */
  66. min-height: 500px;
  67. /* 可视区居中 */
  68. justify-content: center;
  69. }
  70. .container > aside,
  71. .container > main {
  72. border: 1px solid #000;
  73. /* 给子元素设置10px的内边距 */
  74. padding: 10px;
  75. }
  76. .container > aside {
  77. /* 设置不放大、不收缩、宽度为200px */
  78. flex: 0 0 200px;
  79. background-color: rgb(243, 119, 119);
  80. }
  81. .container > main {
  82. /* 设置不放大、不收缩、宽度为600px */
  83. flex: 0 0 600px;
  84. /* 上下外边距为0 左右为10px */
  85. margin: 0 10px;
  86. background-color: skyblue;
  87. }
  88. /* 页脚区 */
  89. footer {
  90. /* 设置弹性盒子 */
  91. display: flex;
  92. /* 设置主轴为垂直 不换行 */
  93. flex-flow: column nowrap;
  94. /* 文本内容居中显示 */
  95. text-align: center;
  96. background-color: #3c3c3c;
  97. }
  98. </style>
  99. <body>
  100. <!-- 页眉 -->
  101. <header>
  102. <a href="">logo</a>
  103. <a href="">首页</a>
  104. <a href="">栏目1</a>
  105. <a href="">栏目2</a>
  106. <a href="">栏目3</a>
  107. <a href="">登录</a>
  108. </header>
  109. <!-- 主体 -->
  110. <div class="container">
  111. <!-- 左边栏 -->
  112. <aside></aside>
  113. <!-- 主体区域 -->
  114. <main></main>
  115. <!-- 右边栏 -->
  116. <aside></aside>
  117. </div>
  118. <!-- 页脚 -->
  119. <footer>
  120. <p>
  121. <a href="#">xxxxxxx</a>
  122. <a href="#">xxxxxxx</a>
  123. </p>
  124. </footer>
  125. </body>
  126. </html>

思路:
1.先写出HTML的结构 页眉、主体(内容区)、页脚
2.然后初始化样式通常用margin: 0; padding: 0;box-sizing: border-box;
3.然后考虑去除一些公共样式 去除<li>的序列号 和 <a>标签的下划线
4.将页眉、内容区、页脚设置弹性容器,并为主轴垂直排列。将内容区三部分设置为主轴为水平排列的弹性容器
5.页眉部分 :将页眉部分的子元素也设置为弹性容器 ,然后再交叉轴上居中排列 。然后让里面的a标签以及内容设置 字体大小、颜色以及文本效果 (文本居中,一般通过text-align: center实现)还可以设置margin值来调整靠左或者靠右距离。设置flex的放大缩小和宽度控制a标签之间的间距。
6.最后还可以通过hover来设置鼠标悬停后的样式
7.主体部分:设置弹性容器后,可以先设置最小高度撑开内容区。然后设置调整上与页眉和页脚的外边距。然后让内容区子元素水平方向居中排列。然后通过flex值来设置内容区三部分的宽度、放大和收缩。
8页脚部分:设置弹性容器后,设置主轴垂直排列 ,然后实现文本居中。

移动端布局

  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. <!-- 加载字体图标 -->
  7. <link rel="stylesheet" href="static/css/font-icon.css" />
  8. <title>移动端的解决方案</title>
  9. <style>
  10. * {
  11. /* 样式初始化 */
  12. margin: 0;
  13. padding: 0;
  14. box-sizing: border-box;
  15. }
  16. a {
  17. /* 去除下划线 */
  18. text-decoration: none;
  19. color: #666;
  20. }
  21. html {
  22. /* vw: 可视区宽度*/
  23. /* vh:可视区的高度*/
  24. width: 100vw;
  25. height: 100vh;
  26. font-size: 14px;
  27. color: #666;
  28. }
  29. body {
  30. min-width: 360px;
  31. background-color: #fff;
  32. /* 设置弹性容器 */
  33. display: flex;
  34. /* 主轴为垂直 不换行 */
  35. flex-flow: column nowrap;
  36. }
  37. /* 头部区域 */
  38. body > header {
  39. color: white;
  40. background-color: #333;
  41. height: 30px;
  42. /* 设置弹性容器 */
  43. display: flex;
  44. /* 辅轴居中排列 */
  45. align-items: center;
  46. /* 紧靠父盒子两端,中间留空且均等排列 */
  47. justify-content: space-between;
  48. /* 相对于浏览器窗口固定定位 */
  49. position: fixed;
  50. width: 100vw;
  51. /* 设置上下0 ,左右15px内边距 */
  52. padding: 0 15px;
  53. }
  54. body > .slider {
  55. height: 180px;
  56. }
  57. body > .slider > img {
  58. /* width: 100%; 让图片充满盒子*/
  59. height: 100%;
  60. }
  61. /* 主导航区 */
  62. nav {
  63. height: 200px;
  64. /* 下外边距10px */
  65. margin-bottom: 10px;
  66. display: flex;
  67. /* 转为多行容器 */
  68. flex-flow: row wrap;
  69. /* 辅轴方向紧靠父盒子两端中间留空均等 */
  70. align-content: space-around;
  71. }
  72. nav > div {
  73. width: 25vw;
  74. /* 设置弹性容器 */
  75. display: flex;
  76. /* 设置主轴为垂直方向 不换行 */
  77. flex-flow: column nowrap;
  78. /* 辅轴居中排列 */
  79. align-items: center;
  80. }
  81. nav > div > a:first-of-type {
  82. /* 文本内容居中对齐 */
  83. text-align: center;
  84. }
  85. nav > div img {
  86. width: 50%;
  87. }
  88. /* 每个区域的标题样式 */
  89. .title {
  90. /* 上外边距10Px */
  91. margin-top: 10px;
  92. /* 1.2倍的正常字体大小 */
  93. font-size: 1.2rem;
  94. /* 正常粗细 */
  95. font-weight: normal;
  96. /* 文本居中对齐 */
  97. text-align: center;
  98. }
  99. /* 热销商品区 */
  100. .hot-goods {
  101. border-top: 1px solid #cdcdcd;
  102. margin-top: 10px;
  103. font-size: 0.8rem;
  104. /* 设置弹性盒子 */
  105. display: flex;
  106. /* 主轴为水平方向 换行 */
  107. flex-flow: row wrap;
  108. }
  109. .hot-goods img {
  110. width: 100%;
  111. }
  112. .hot-goods > .goods-img {
  113. /* 内边距并重置大小 */
  114. padding: 10px;
  115. box-sizing: border-box;
  116. /* 允许放大不允许缩小,否则项目不会换行,多行容器失效 */
  117. flex: 1 0 30vw;
  118. /* 再将每个商品描述转为flex */
  119. display: flex;
  120. /* 主轴方向垂直且不允许换行 */
  121. flex-flow: column nowrap;
  122. /* 居中排列 */
  123. justify-content: center;
  124. }
  125. /* 商品描述的最后一个区域转flex,并设置项目在主轴上排列对齐方式 */
  126. .hot-goods > .goods-img > div {
  127. /* 设置弹性盒子 */
  128. display: flex;
  129. /* 分散对齐 */
  130. justify-content: space-around;
  131. }
  132. /* 热销样式 */
  133. .hot {
  134. color: coral;
  135. }
  136. /* 商品列表区 */
  137. .list-goods {
  138. /* 设置10px内边距 */
  139. padding: 10px;
  140. border-top: 1px solid #cdcdcd;
  141. margin-top: 10px;
  142. /* 设置弹性容器 */
  143. display: flex;
  144. /* 主轴为水平 换行 */
  145. flex-flow: row wrap;
  146. /* 居中排列 */
  147. justify-content: center;
  148. }
  149. /* 每个项目也转为flex */
  150. .list-goods > .goods-desc {
  151. display: flex;
  152. /* 主轴为垂直 换行 */
  153. flex-flow: column wrap;
  154. /* 居中排列 */
  155. align-items: center;
  156. /* 文本居中 */
  157. text-align: center;
  158. padding: 10px;
  159. font-size: 0.8rem;
  160. color: lightseagreen;
  161. }
  162. /* 设置文字区域的宽度 */
  163. .list-goods > .goods-desc > a:last-of-type {
  164. width: 30vw;
  165. }
  166. /* 图片缩放为空间的80%*/
  167. .list-goods img {
  168. width: 80%;
  169. }
  170. body > footer {
  171. color: #666;
  172. background-color: #efefef;
  173. border-top: 1px solid #ccc;
  174. height: 55px;
  175. /* 相对于浏览器窗口固定 */
  176. position: fixed;
  177. bottom: 0;
  178. /* 100%视口宽度 */
  179. width: 100vw;
  180. /* 设置弹性容器 */
  181. display: flex;
  182. /* 间隙均等排列 */
  183. justify-content: space-evenly;
  184. }
  185. body > footer a {
  186. /* 上外边距10px */
  187. margin-top: 10px;
  188. /* 0.8倍字体大小 */
  189. font-size: 0.8rem;
  190. /* 设置弹性容器 */
  191. display: flex;
  192. /* 垂直排列不换行 */
  193. flex-flow: column nowrap;
  194. /* 交叉轴项目居中显示 */
  195. align-items: center;
  196. }
  197. /* 选择第一个span */
  198. body > footer a > span:first-of-type {
  199. /* 图标字体应该大一些 */
  200. font-size: 1.6rem;
  201. }
  202. </style>
  203. </head>
  204. <body>
  205. <!-- 页眉 -->
  206. <header>
  207. <a href="">LOGO</a>
  208. <!-- 图标引用 -->
  209. <span class="iconfont">&#xe61f;</span>
  210. </header>
  211. <!-- 轮播图 -->
  212. <div class="slider">
  213. <img src="static/images/banner.jpg" alt="" />
  214. </div>
  215. <!-- 主导航区 -->
  216. <nav>
  217. <div>
  218. <a href=""><img src="static/images/link1.webp" alt="" /></a>
  219. <a href="">京东超市</a>
  220. </div>
  221. <div>
  222. <a href=""><img src="static/images/link2.webp" alt="" /></a>
  223. <a href="">服装百货</a>
  224. </div>
  225. <div>
  226. <a href=""><img src="static/images/link3.webp" alt="" /></a>
  227. <a href="">数码精品</a>
  228. </div>
  229. <div>
  230. <a href=""><img src="static/images/link4.webp" alt="" /></a>
  231. <a href="">优惠劵</a>
  232. </div>
  233. <div>
  234. <a href=""><img src="static/images/link1.webp" alt="" /></a>
  235. <a href="">超市精选</a>
  236. </div>
  237. <div>
  238. <a href=""><img src="static/images/link2.webp" alt="" /></a>
  239. <a href="">服装百货</a>
  240. </div>
  241. <div>
  242. <a href=""><img src="static/images/link3.webp" alt="" /></a>
  243. <a href="">数码精品</a>
  244. </div>
  245. <div>
  246. <a href=""><img src="static/images/link4.webp" alt="" /></a>
  247. <a href="">优惠劵</a>
  248. </div>
  249. </nav>
  250. <!-- 热销商品 -->
  251. <h2>
  252. <!-- 图标引用 -->
  253. 热销商品<span class="iconfont hot" style="color: coral;">&#xe60b;</span>
  254. </h2>
  255. <div class="hot-goods">
  256. <div class="goods-img">
  257. <a href=""><img src="static/images/goods1.jpg" alt="" /></a>
  258. <p>Apple iPhone 11 128G</p>
  259. <div>
  260. <span>6299&nbsp;元</span>
  261. <!-- 图标引用 -->
  262. <span class="iconfont hot">&#xe602;</span>
  263. </div>
  264. </div>
  265. <div class="goods-img">
  266. <a href=""><img src="static/images/goods1.jpg" alt="" /></a>
  267. <p>Apple iPhone X 512G</p>
  268. <div>
  269. <span>8299&nbsp;元</span>
  270. <!-- 图标引用 -->
  271. <span class="iconfont hot">&#xe602;</span>
  272. </div>
  273. </div>
  274. <div class="goods-img">
  275. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
  276. <p>华为笔记本电脑</p>
  277. <div>
  278. <span>5699&nbsp;元</span>
  279. <!-- 图标引用 -->
  280. <span class="iconfont hot">&#xe602;</span>
  281. </div>
  282. </div>
  283. <div class="goods-img">
  284. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
  285. <p>小米笔记本电脑</p>
  286. <div>
  287. <span>3999&nbsp;元</span>
  288. <!-- 图标引用 -->
  289. <span class="iconfont hot">&#xe602;</span>
  290. </div>
  291. </div>
  292. <div class="goods-img">
  293. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
  294. <p>联想笔记本电脑</p>
  295. <div>
  296. <span>4399&nbsp;元</span>
  297. <!-- 图标引用 -->
  298. <span class="iconfont hot">&#xe602;</span>
  299. </div>
  300. </div>
  301. <div class="goods-img">
  302. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
  303. <p>宏基笔记本电脑</p>
  304. <div>
  305. <span>4399&nbsp;元</span>
  306. <!-- 图标引用 -->
  307. <span class="iconfont hot">&#xe602;</span>
  308. </div>
  309. </div>
  310. </div>
  311. <!-- 商品列表区 -->
  312. <h2 class="title">
  313. <!-- 图标引用 -->
  314. 商品列表<span class="iconfont hot" style="color: coral;">&#xe64b;</span>
  315. </h2>
  316. <div class="list-goods">
  317. <div class="goods-desc">
  318. <a href=""><img src="static/images/goods3.jpg" alt="" /></a>
  319. <!-- 图像对齐 -->
  320. <a href=""
  321. >西门子(SIEMENS) 10公斤 变频滚筒洗衣机 防过敏程序 高温筒清洁 快洗15'
  322. 羽绒服专业洗涤<span
  323. class="iconfont hot"
  324. style="vertical-align: middle;"
  325. >&#xe675;</span
  326. ></a
  327. >
  328. </div>
  329. <div class="goods-desc">
  330. <a href=""><img src="static/images/goods3.jpg" alt="" /></a>
  331. <!-- 图像对齐 -->
  332. <a href=""
  333. >西门子(SIEMENS) 10公斤 变频滚筒洗衣机 防过敏程序 高温筒清洁 快洗15'
  334. 羽绒服专业洗涤<span
  335. class="iconfont hot"
  336. style="vertical-align: middle;"
  337. >&#xe675;</span
  338. ></a
  339. >
  340. </div>
  341. <div class="goods-desc">
  342. <a href=""><img src="static/images/goods3.jpg" alt="" /></a>
  343. <!-- 图像对齐 -->
  344. <a href=""
  345. >西门子(SIEMENS) 10公斤 变频滚筒洗衣机 防过敏程序 高温筒清洁 快洗15'
  346. 羽绒服专业洗涤<span
  347. class="iconfont hot"
  348. style="vertical-align: middle;"
  349. >&#xe675;</span
  350. ></a
  351. >
  352. </div>
  353. <div class="goods-desc">
  354. <a href=""><img src="static/images/goods3.jpg" alt="" /></a>
  355. <!-- 图像对齐 -->
  356. <a href=""
  357. >西门子(SIEMENS) 10公斤 变频滚筒洗衣机 防过敏程序 高温筒清洁 快洗15'
  358. 羽绒服专业洗涤<span
  359. class="iconfont hot"
  360. style="vertical-align: middle;"
  361. >&#xe675;</span
  362. ></a
  363. >
  364. </div>
  365. <div class="goods-desc">
  366. <a href=""><img src="static/images/goods3.jpg" alt="" /></a>
  367. <!-- 图像对齐 -->
  368. <a href=""
  369. >西门子(SIEMENS) 10公斤 变频滚筒洗衣机 防过敏程序 高温筒清洁 快洗15'
  370. 羽绒服专业洗涤<span
  371. class="iconfont hot"
  372. style="vertical-align: middle;"
  373. >&#xe675;</span
  374. ></a
  375. >
  376. </div>
  377. <div class="goods-desc">
  378. <a href=""><img src="static/images/goods3.jpg" alt="" /></a>
  379. <!-- 图像对齐 -->
  380. <a href=""
  381. >西门子(SIEMENS) 10公斤 变频滚筒洗衣机 防过敏程序 高温筒清洁 快洗15'
  382. 羽绒服专业洗涤<span
  383. class="iconfont hot"
  384. style="vertical-align: middle;"
  385. >&#xe675;</span
  386. ></a
  387. >
  388. </div>
  389. </div>
  390. <footer>
  391. <a href="">
  392. <!-- 图标引用 -->
  393. <span class="iconfont hot">&#xe60c;</span>
  394. <span>首页</span>
  395. </a>
  396. <a href="">
  397. <!-- 图标引用 -->
  398. <span class="iconfont hot">&#xe64b;</span>
  399. <span>分类</span>
  400. </a>
  401. <a href="">
  402. <!-- 图标引用 -->
  403. <span class="iconfont hot">&#xe602;</span>
  404. <span>购物车</span>
  405. </a>
  406. <a href="">
  407. <!-- 图标引用 -->
  408. <span class="iconfont hot">&#xe65b;</span>
  409. <span>未登录</span>
  410. </a>
  411. </footer>
  412. </body>
  413. </html>

总结: flex移动端布局,当调整手机型号时,需要重新调整设置。垂直和水平容易搞混淆。还需要多练多写熟悉flex的详细属性。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:你把你的页面乱箭射死了.... 写得不错, 继续
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!