Blogger Information
Blog 128
fans 9
comment 5
visits 241252
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【案例】学习flex项目上的三个属性并尝试制作移动端京东首页
 一纸荒凉* Armani
Original
1033 people have browsed it

flex弹性盒模型

flex 属性众多,记住以下几个就够用了,尽可能使用简写

1. 术语

  1. 容器: 具有display:flex属性元素
  2. 项目: flex 容器的”子元素”
  3. 主轴: 项目排列的轴线
  4. 交叉轴: 与主轴垂直的轴线

2. 容器属性

序号 属性 描述
1 flex-flow 主轴方向与换行方式
2 justify-content 项目在主轴上的对齐方式
3 align-items 项目在交叉轴上的对齐方式
4 align-content 项目在多行容器中的对齐方式

3. 项目属性

序号 属性 描述
1 flex 项目的缩放比例与基准宽度
3 align-self 单个项目在交叉轴上的对齐方式
4 order 项目在主轴上排列顺序
  • flex项目属性
  1. /* flex: flex-grow flex-shrink flex-basis */
  2. /* flex: 放大因子 收缩因子 项目在主轴上的基准宽度 */
  3. /* flex-basis: 项目占据的主轴宽度,优先级大于width,小于min-width */
  4. /* 默认值,禁止放大,但允许自动收缩,项目宽度自动计算 */
  5. flex: 0 1 auto;
  6. /* 默认值使用关键字简写 */
  7. flex: initial;
  8. /* 允许放大和收缩 */
  9. flex: 1 1 auto;
  10. /* 简写 */
  11. flex: auto;
  12. /* 禁止放大和收缩 */
  13. flex: 0 0 auto;
  14. /* 简写 */
  15. flex: none;
  16. /* 如果只有一个数字,省掉后面二个参数,表示的放大因子 */
  17. flex: 1;
  18. /* 等于 flex: 1 1 auto; */
  19. /* flex通常不会用来设置所有项目的默认选项,通常用来设置某一个项目的特征 */
  1. /* 写一个案例,要求第2个,第3个项目的宽度是第1个和第四个2倍 */
  2. /* 选择第一个和第四个项目 */
  3. .container > .item:first-of-type,
  4. .container > .item:last-of-type {
  5. flex: 1;
  6. }
  7. /* 选择第二个和第三个项目 */
  8. .container > .item:nth-of-type(2),
  9. .container > .item:nth-of-type(2) + * {
  10. flex: 2;
  11. }

  • align-self属性 单个项目在交叉轴上的对齐方式
  1. /* 例如设置第2个项目与其它项目的对齐方式不一样 */
  2. .container > .item:nth-of-type(2) {
  3. align-self: stretch;
  4. align-self: flex-start;
  5. align-self: flex-end;
  6. align-self: center;
  7. }

  • order属性 项目在主轴上排列顺序
  1. /* 显示顺序:默认按书写的源码顺序排列 */
  2. /* 默认序号越小越靠前,越大越靠后 */
  3. .container > .item:first-of-type {
  4. order: 1;
  5. order: 5;
  6. background-color: yellow;
  7. }
  8. .container > .item:nth-of-type(2) {
  9. order: 2;
  10. background-color: lightgreen;
  11. }
  12. .container > .item:nth-of-type(3) {
  13. order: 3;
  14. order: 0;
  15. }
  16. .container > .item:last-of-type {
  17. order: 4;
  18. /* 支持负值 */
  19. order: -1;
  20. background-color: #ccc;
  21. }

4.移动端京东首页

项目结构

  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="static/css/header.css">
  8. <link rel="stylesheet" href="static/css/footer.css">
  9. <link rel="stylesheet" href="static/css/nav.css">
  10. <link rel="stylesheet" href="static/css/banner.css">
  11. <link rel="stylesheet" href="static/css/tj.css">
  12. <link rel="stylesheet" href="static/css/index.css">
  13. <!-- 引入字体图标 -->
  14. <link rel="stylesheet" href="static/icon-font/iconfont.css">
  15. </head>
  16. <body>
  17. <!-- 页眉头部 -->
  18. <div class="header">
  19. <!-- 菜单 -->
  20. <div class="menu iconfont icon-menu"></div>
  21. <!-- 搜索框 -->
  22. <div class="search">
  23. <div class="logo">JD</div>
  24. <div class="zoom ">
  25. <span class="iconfont icon-search"></span>
  26. </div>
  27. <input type="text" class="words" value="移动硬盘500G">
  28. </div>
  29. <!-- 登录按钮 -->
  30. <a href="http://" target="_blank" class="login">登录</a>
  31. </div>
  32. <!-- 主体 -->
  33. <div class="main">
  34. <!-- 轮播图 -->
  35. <!-- banner区 -->
  36. <div class="banner">
  37. <a href="https://pro.m.jd.com/mall/active/2d4Hw2SqrLkZ6K8wzqZJN2AoJWGA/index.html">
  38. <img src="https://m.360buyimg.com/mobilecms/s250x200_jfs/t1/171298/2/14126/63148/60585644Eca26db11/58967bace7177cb5.jpg!q70.jpg.dpg" alt="">
  39. </a>
  40. <a href="https://pro.m.jd.com/mall/active/3wpbRPaYiHTigHCyrGVDQKwy69NG/index.html">
  41. <img src="https://m.360buyimg.com/mobilecms/s250x200_jfs/t1/166211/4/14279/77377/605be5adEc517f1fa/1514376c9619dbcc.jpg!q70.jpg.dpg" alt="">
  42. </a>
  43. <a href="https://pro.m.jd.com/mall/active/bXhWtPcq4Vh9LHDzPyz1uyjyjyi/index.html?babelChannel=ttt3">
  44. <img src="https://m.360buyimg.com/mobilecms/s250x200_jfs/t1/140456/37/16741/69120/60585711E4cff0446/fb6f80ac35fb36da.jpg!q70.jpg.dpg" alt="">
  45. </a>
  46. </div>
  47. <!-- 主导航区 -->
  48. <ul class="nav">
  49. <li>
  50. <a href="#">
  51. <img src="static/img/dh/nav-1.png" alt="">
  52. <span>京东超市</span>
  53. </a>
  54. </li>
  55. <li>
  56. <a href="#">
  57. <img src="static/img/dh/nav-2.png" alt="">
  58. <span>数码电器</span>
  59. </a>
  60. </li>
  61. <li>
  62. <a href="#">
  63. <img src="static/img/dh/nav-3.png" alt="">
  64. <span>京东服饰</span>
  65. </a>
  66. </li>
  67. <li>
  68. <a href="#">
  69. <img src="static/img/dh/nav-4.png" alt="">
  70. <span>京东生鲜</span>
  71. </a>
  72. </li>
  73. <li>
  74. <a href="#">
  75. <img src="static/img/dh/nav-5.png" alt="">
  76. <span>京东到家</span>
  77. </a>
  78. </li>
  79. <li>
  80. <a href="#">
  81. <img src="static/img/dh/nav-6.png" alt="">
  82. <span>充值缴费</span>
  83. </a>
  84. </li>
  85. <li>
  86. <a href="#">
  87. <img src="static/img/dh/nav-7.png" alt="">
  88. <span>9.9元拼</span>
  89. </a>
  90. </li>
  91. <li>
  92. <a href="#">
  93. <img src="static/img/dh/nav-8.png" alt="">
  94. <span>领券</span>
  95. </a>
  96. </li>
  97. <li>
  98. <a href="#">
  99. <img src="static/img/dh/nav-9.png" alt="">
  100. <span>领金贴</span>
  101. </a>
  102. </li>
  103. <li>
  104. <a href="#">
  105. <img src="static/img/dh/nav-10.png" alt="">
  106. <span>PLUS会员</span>
  107. </a>
  108. </li>
  109. </ul>
  110. <!-- 秒杀区 -->
  111. <div class="ms">
  112. </div>
  113. <!-- 商品列表区 -->
  114. <div class="tj">
  115. <h2 class="title">猜你喜欢</h2>
  116. <ul>
  117. <li class="item">
  118. <a href="">
  119. <img src="static/img/sp/sp-1.webp" alt="">
  120. <p>美的(Midea)电水壶热水壶电热水壶304不锈钢1.7L容量暖水壶烧水壶开水壶智能断电1705b</p>
  121. <div class="price">
  122. <span class="iconfont icon-rmb">79</span>
  123. <div>看相似</div>
  124. </div>
  125. </a>
  126. </li>
  127. <li class="item">
  128. <a href="">
  129. <img src="static/img/sp/sp-2.webp" alt="">
  130. <p>美的(Midea)电水壶热水壶电热水壶304不锈钢1.7L容量暖水壶烧水壶开水壶智能断电1705b</p>
  131. <div class="price">
  132. <span class="iconfont icon-rmb">79</span>
  133. <div>看相似</div>
  134. </div>
  135. </a>
  136. </li>
  137. <li class="item">
  138. <a href="">
  139. <img src="static/img/sp/sp-3.webp" alt="">
  140. <p>美的(Midea)电水壶热水壶电热水壶304不锈钢1.7L容量暖水壶烧水壶开水壶智能断电1705b</p>
  141. <div class="price">
  142. <span class="iconfont icon-rmb">79</span>
  143. <div>看相似</div>
  144. </div>
  145. </a>
  146. </li>
  147. <li class="item">
  148. <a href="">
  149. <img src="static/img/sp/sp-4.webp" alt="">
  150. <p>美的(Midea)电水壶热水壶电热水壶304不锈钢1.7L容量暖水壶烧水壶开水壶智能断电1705b</p>
  151. <div class="price">
  152. <span class="iconfont icon-rmb">79</span>
  153. <div>看相似</div>
  154. </div>
  155. </a>
  156. </li>
  157. <li class="item">
  158. <a href="">
  159. <img src="static/img/sp/sp-5.webp" alt="">
  160. <p>美的(Midea)电水壶热水壶电热水壶304不锈钢1.7L容量暖水壶烧水壶开水壶智能断电1705b</p>
  161. <div class="price">
  162. <span class="iconfont icon-rmb">79</span>
  163. <div>看相似</div>
  164. </div>
  165. </a>
  166. </li>
  167. <li class="item">
  168. <a href="">
  169. <img src="static/img/sp/sp-6.webp" alt="">
  170. <p>美的(Midea)电水壶热水壶电热水壶304不锈钢1.7L容量暖水壶烧水壶开水壶智能断电1705b</p>
  171. <div class="price">
  172. <span class="iconfont icon-rmb">79</span>
  173. <div>看相似</div>
  174. </div>
  175. </a>
  176. </li>
  177. <li class="item">
  178. <a href="">
  179. <img src="static/img/sp/sp-7.webp" alt="">
  180. <p>美的(Midea)电水壶热水壶电热水壶304不锈钢1.7L容量暖水壶烧水壶开水壶智能断电1705b</p>
  181. <div class="price">
  182. <span class="iconfont icon-rmb">79</span>
  183. <div>看相似</div>
  184. </div>
  185. </a>
  186. </li>
  187. <li class="item">
  188. <a href="">
  189. <img src="static/img/sp/sp-8.webp" alt="">
  190. <p>美的(Midea)电水壶热水壶电热水壶304不锈钢1.7L容量暖水壶烧水壶开水壶智能断电1705b</p>
  191. <div class="price">
  192. <span class="iconfont icon-rmb">79</span>
  193. <div>看相似</div>
  194. </div>
  195. </a>
  196. </li>
  197. <li class="item">
  198. <a href="">
  199. <img src="static/img/sp/sp-1.webp" alt="">
  200. <p>美的(Midea)电水壶热水壶电热水壶304不锈钢1.7L容量暖水壶烧水壶开水壶智能断电1705b</p>
  201. <div class="price">
  202. <span class="iconfont icon-rmb">79</span>
  203. <div>看相似</div>
  204. </div>
  205. </a>
  206. </li>
  207. <li class="item">
  208. <a href="">
  209. <img src="static/img/sp/sp-2.webp" alt="">
  210. <p>美的(Midea)电水壶热水壶电热水壶304不锈钢1.7L容量暖水壶烧水壶开水壶智能断电1705b</p>
  211. <div class="price">
  212. <span class="iconfont icon-rmb">79</span>
  213. <div>看相似</div>
  214. </div>
  215. </a>
  216. </li>
  217. <li class="item">
  218. <a href="">
  219. <img src="static/img/sp/sp-3.webp" alt="">
  220. <p>美的(Midea)电水壶热水壶电热水壶304不锈钢1.7L容量暖水壶烧水壶开水壶智能断电1705b</p>
  221. <div class="price">
  222. <span class="iconfont icon-rmb">79</span>
  223. <div>看相似</div>
  224. </div>
  225. </a>
  226. </li>
  227. <li class="item">
  228. <a href="">
  229. <img src="static/img/sp/sp-4.webp" alt="">
  230. <p>美的(Midea)电水壶热水壶电热水壶304不锈钢1.7L容量暖水壶烧水壶开水壶智能断电1705b</p>
  231. <div class="price">
  232. <span class="iconfont icon-rmb">79</span>
  233. <div>看相似</div>
  234. </div>
  235. </a>
  236. </li>
  237. <li class="item">
  238. <a href="">
  239. <img src="static/img/sp/sp-5.webp" alt="">
  240. <p>美的(Midea)电水壶热水壶电热水壶304不锈钢1.7L容量暖水壶烧水壶开水壶智能断电1705b</p>
  241. <div class="price">
  242. <span class="iconfont icon-rmb">79</span>
  243. <div>看相似</div>
  244. </div>
  245. </a>
  246. </li>
  247. <li class="item">
  248. <a href="">
  249. <img src="static/img/sp/sp-7.webp" alt="">
  250. <p>美的(Midea)电水壶热水壶电热水壶304不锈钢1.7L容量暖水壶烧水壶开水壶智能断电1705b</p>
  251. <div class="price">
  252. <span class="iconfont icon-rmb">79</span>
  253. <div>看相似</div>
  254. </div>
  255. </a>
  256. </li>
  257. </ul>
  258. </div>
  259. </div>
  260. <!-- 页脚菜单栏 -->
  261. <div class="footer">
  262. <ul>
  263. <li>
  264. <span class="iconfont icon-home"></span>
  265. <span>首页</span>
  266. </li>
  267. <li>
  268. <span class="iconfont icon-layers"></span>
  269. <span>分类</span>
  270. </li>
  271. <li>
  272. <span class="iconfont icon-kehuguanli"></span>
  273. <span>京喜</span>
  274. </li>
  275. <li>
  276. <span class="iconfont icon-shopping-cart"></span>
  277. <span>登录</span>
  278. </li>
  279. </ul>
  280. </div>
  281. </body>
  282. </html>

reset.css

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. li {
  7. list-style: none;
  8. }
  9. a {
  10. color: white;
  11. text-decoration: none;
  12. }
  13. body {
  14. background-color: #f6f6f6;
  15. }
  16. /* 媒体查询 */
  17. @media screen and (max-width: 480px) {
  18. html {
  19. font-size: 10px;
  20. }
  21. }
  22. @media screen and (min-width: 480px) {
  23. html {
  24. font-size: 12px;
  25. }
  26. }
  27. @media screen and (min-width: 640px) {
  28. html {
  29. font-size: 14px;
  30. }
  31. }
  32. @media screen and (min-width: 720px) {
  33. html {
  34. font-size: 16px;
  35. }
  36. }

index.css

  1. /* 引入初始化样式文件 */
  2. @import 'reset.css';
  3. /* 页眉 */
  4. .header {
  5. position: fixed;
  6. top: 0;
  7. left: 0;
  8. right: 0;
  9. font-size: 1.4rem;
  10. color: white;
  11. height: 4.4rem;
  12. background-color: #e43130;
  13. z-index: 100;
  14. }
  15. /* 页脚*/
  16. .footer {
  17. position: fixed;
  18. bottom: 0;
  19. left: 0;
  20. right: 0;
  21. font-size: 1.4rem;
  22. color: #666;
  23. height: 4.4rem;
  24. background-color: #fafafa;
  25. box-shadow: 0 0 3px #999;
  26. z-index: 100;
  27. }
  28. /* 主体 */
  29. .main {
  30. position: absolute;
  31. top: 4.4rem;
  32. bottom: 4.4rem;
  33. left: 0;
  34. right: 0;
  35. font-size: 1.4rem;
  36. min-width: 320px;
  37. }

header.css

  1. .header {
  2. display: flex;
  3. align-items: center;
  4. }
  5. /* 页眉中的三个部分比例 1:6:1 */
  6. .header .login {
  7. color: #fff;
  8. text-align: center;
  9. flex: 1;
  10. }
  11. .header .menu {
  12. text-align: center;
  13. flex: 1;
  14. font-size: 2.5rem;
  15. }
  16. .header .search {
  17. flex: 6;
  18. padding: 0.5rem;
  19. background-color: #fff;
  20. border-radius: 3rem;
  21. display: flex;
  22. }
  23. /* logo */
  24. .header .search .logo {
  25. color: #e43130;
  26. flex: 0 1 4rem;
  27. font-size: 2rem;
  28. /* 水平垂直居中 */
  29. text-align: center;
  30. line-height: 2rem;
  31. }
  32. /* 放大镜 */
  33. .header .search .zoom {
  34. color: #ccc;
  35. flex: 0 1 4rem;
  36. border-left: 1px solid;
  37. /* 水平垂直居中 */
  38. text-align: center;
  39. line-height: 2rem;
  40. }
  41. /* 搜索文本框 */
  42. .header .search .words {
  43. flex: auto;
  44. border: none;
  45. outline: none;
  46. color: #aaa;
  47. }

nav.css

  1. .main .nav {
  2. padding: 1rem;
  3. display: flex;
  4. flex-flow: row wrap;
  5. justify-content: space-between;
  6. background: rgb(255, 255, 255);
  7. border-radius: 1rem;
  8. background-color: white;
  9. margin: 0 0.6rem;
  10. }
  11. .main .nav li {
  12. flex: 1 1 20%;
  13. }
  14. .main .nav li a {
  15. color: #7b7b7b;
  16. display: flex;
  17. flex-flow: column nowrap;
  18. align-items: center;
  19. }
  20. .main .nav li a img {
  21. height: 4rem;
  22. width: 4rem;
  23. }

tj.css

  1. .tj .title {
  2. height: 4rem;
  3. line-height: 4rem;
  4. color: #555;
  5. font-weight: 500;
  6. text-align: center;
  7. margin: 1rem 0;
  8. }
  9. .tj ul {
  10. font-size: 1rem;
  11. display: flex;
  12. flex-wrap: wrap;
  13. padding-bottom: 4.4rem;
  14. }
  15. .tj ul .item {
  16. padding: 1rem;
  17. flex: 1 1 calc(50% - 2rem);
  18. background-color: #fff;
  19. margin-left: 1rem;
  20. margin-bottom: 1rem;
  21. }
  22. .tj ul .item a img {
  23. width: 100%;
  24. }
  25. .tj ul .item a p {
  26. color: #555;
  27. line-height: 2rem;
  28. overflow: hidden;
  29. text-overflow: ellipsis;
  30. display: -webkit-box;
  31. -webkit-line-clamp: 2;
  32. -webkit-box-orient: vertical;
  33. }
  34. .tj ul .item a .price {
  35. display: flex;
  36. justify-content: space-between;
  37. color: red;
  38. }
  39. .tj ul .item a .price div {
  40. color: grey;
  41. background-color: #f0f2f5;
  42. border-radius: .55rem;
  43. text-align: center;
  44. padding: 0.6rem;
  45. line-height: 1rem;
  46. }

footer.css

  1. .footer>ul {
  2. padding: 0.5rem;
  3. display: flex;
  4. justify-content: space-around;
  5. align-items: center;
  6. }
  7. .footer>ul>li {
  8. width: 20%;
  9. display: flex;
  10. flex-direction: column;
  11. justify-content: center;
  12. align-items: center;
  13. font-size: 1rem;
  14. cursor: pointer;
  15. }
  16. .footer>ul>li .iconfont {
  17. font-size: 2rem;
  18. }

5.效果图

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