博客列表 >0325作业-flex项目上的三个属性及手机端布局

0325作业-flex项目上的三个属性及手机端布局

千山暮雪
千山暮雪原创
2021年03月29日 01:12:11541浏览

flex项目的三大基本属性

基本属性 属性说明 属性值 值说明
flex 项目的缩放比例与基准宽度 0 1 auto / initial 默认,禁止放大,允许收缩,宽度自动
1 1 auto / auto 允许放大和收缩
0 0 auto / none 禁止放大和收缩/PC布局
align-self 单个项目在交叉轴上的对齐方式 stretch 默认,拉伸
flex-start 起始线
flex-end 终止线
center 居中
order 项目在主轴上排列顺序 不写 显示顺序:默认按书写的源码顺序排列
【数字】 序号越小越靠前,越大越靠后,可以为负值

示例

项目的缩放比例与基准宽度flex

0 0 10rem1 0 10rem,同基准下允许放大对比
放大

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>flex项目属性</title>
  8. <style>
  9. main {
  10. display: flex;
  11. height: 10rem;
  12. flex-flow: row nowrap;
  13. }
  14. main div,.cols div {
  15. background-color: lightblue;
  16. border: 1px solid #0C0C0C;
  17. }
  18. main div{
  19. flex:0 0 10rem;
  20. }
  21. .cols {
  22. display: flex;
  23. height: 10rem;
  24. flex-flow: row nowrap;
  25. }
  26. .cols div {
  27. flex:1 0 10rem;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <main>
  33. <div>item1</div>
  34. <div>item2</div>
  35. <div>item3</div>
  36. <div>item4</div>
  37. </main>
  38. <div class="cols">
  39. <div>colm1</div>
  40. <div>colm2</div>
  41. <div>colm3</div>
  42. <div>colm4</div>
  43. </div>
  44. </body>
  45. </html>

1 0 10rem1 1 10rem,同基准下允许收缩对比
收缩

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>flex项目属性</title>
  8. <style>
  9. main {
  10. display: flex;
  11. height: 10rem;
  12. flex-flow: row nowrap;
  13. }
  14. main div,.cols div {
  15. background-color: lightblue;
  16. border: 1px solid #0C0C0C;
  17. }
  18. main div{
  19. flex:1 0 10rem;
  20. }
  21. .cols {
  22. display: flex;
  23. height: 10rem;
  24. flex-flow: row nowrap;
  25. }
  26. .cols div {
  27. flex:1 1 10rem;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <main>
  33. <div>item1</div>
  34. <div>item2</div>
  35. <div>item3</div>
  36. <div>item4</div>
  37. </main>
  38. <div class="cols">
  39. <div>colm1</div>
  40. <div>colm2</div>
  41. <div>colm3</div>
  42. <div>colm4</div>
  43. </div>
  44. </body>
  45. </html>

按比例伸缩

项目占比为fiex的值*1/1+2+3+4
比例伸缩

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>flex项目属性</title>
  8. <style>
  9. main {
  10. display: flex;
  11. height: 10rem;
  12. flex-flow: row nowrap;
  13. }
  14. main div {
  15. background-color: lightblue;
  16. border: 1px solid #0C0C0C;
  17. }
  18. main div:nth-of-type(1) {
  19. flex: 1;
  20. }
  21. main div:nth-of-type(2) {
  22. flex: 2;
  23. }
  24. main div:nth-of-type(3) {
  25. flex: 3;
  26. }
  27. main div:nth-of-type(4) {
  28. flex: 4;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <main>
  34. <div>item1</div>
  35. <div>item2</div>
  36. <div>item3</div>
  37. <div>item4</div>
  38. </main>
  39. </body>
  40. </html>
单个项目在交叉轴上的对齐方式align-self

align-self

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>flex项目属性</title>
  8. <style>
  9. main {
  10. display: flex;
  11. height: 20rem;
  12. width: 40rem;
  13. flex-flow: row nowrap;
  14. /*align-items: center;*/
  15. }
  16. main div {
  17. background-color: lightblue;
  18. }
  19. main :nth-of-type(1) {
  20. align-self: stretch;
  21. }
  22. main :nth-of-type(2) {
  23. align-self: flex-start;
  24. }
  25. main :nth-of-type(3) {
  26. align-self: center;
  27. }
  28. main :nth-of-type(4) {
  29. align-self: flex-end;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <main>
  35. <div>item1</div>
  36. <div>item2</div>
  37. <div>item3</div>
  38. <div>item4</div>
  39. <div>item5</div>
  40. <div>item6</div>
  41. <div>item7</div>
  42. <div>item8</div>
  43. <div>item9</div>
  44. <div>item10</div>
  45. <div>item11</div>
  46. <div>item12</div>
  47. </main>
  48. </body>
  49. </html>
项目在主轴上排列顺序order

order

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>flex项目属性</title>
  8. <style>
  9. main {
  10. display: flex;
  11. height: 10rem;
  12. width: 40rem;
  13. flex-flow: row nowrap;
  14. /*align-items: center;*/
  15. }
  16. .order {
  17. display: flex;
  18. height: 10rem;
  19. width: 40rem;
  20. flex-flow: row nowrap;
  21. }
  22. main div {
  23. background-color: lightblue;
  24. border: 1px solid #0C0C0C;
  25. }
  26. .order div {
  27. background-color: lightgrey;
  28. border: 1px solid #0C0C0C;
  29. }
  30. .order div:nth-of-type(2) {
  31. order: 100;
  32. }
  33. .order div:nth-of-type(4) {
  34. order: -1;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <main>
  40. <div>item1</div>
  41. <div>item2</div>
  42. <div>item3</div>
  43. <div>item4</div>
  44. </main>
  45. <div class="order">
  46. <div>order1</div>
  47. <div>order2</div>
  48. <div>order3</div>
  49. <div>order4</div>
  50. </div>
  51. </body>
  52. </html>

移动商城首页的页眉和页脚的布局

JD

index.html
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <link rel="stylesheet" href="static/css/index.css">
  8. <link rel="stylesheet" href="static/icon-font/iconfont.css">
  9. <link rel="stylesheet" href="static/css/header.css">
  10. <link rel="stylesheet" href="static/css/seckill.css">
  11. <link rel="stylesheet" href="static/css/footer.css">
  12. <link rel="stylesheet" href="static/css/nav.css">
  13. <link rel="stylesheet" href="static/css/love.css">
  14. <title>首页</title>
  15. </head>
  16. <body>
  17. <!-- 页眉 -->
  18. <header class="header">
  19. <div class="menu iconfont icon-menu"></div>
  20. <div class="search">
  21. <div class="logo">JD</div>
  22. <div class="iconfont icon-search"></div>
  23. <input class="words" type="text" value="php11">
  24. </div>
  25. <div class="login">登录</div>
  26. </header>
  27. <!-- 主体 -->
  28. <main class="main">
  29. <!-- 导航 -->
  30. <ul class="nav">
  31. <!-- 第一组 -->
  32. <li>
  33. <a href=""><img src="static/images/dh/nav-1.png" alt="" /></a>
  34. <a href="">京东超市</a>
  35. </li>
  36. <li>
  37. <a href=""><img src="static/images/dh/nav-2.png" alt="" /></a>
  38. <a href="">数码电器</a>
  39. </li>
  40. <li>
  41. <a href=""><img src="static/images/dh/nav-3.png" alt="" /></a>
  42. <a href="">京东服饰</a>
  43. </li>
  44. <li>
  45. <a href=""><img src="static/images/dh/nav-4.png" alt="" /></a>
  46. <a href="">京东生鲜</a>
  47. </li>
  48. <li>
  49. <a href=""><img src="static/images/dh/nav-5.png" alt="" /></a>
  50. <a href="">京东到家</a>
  51. </li>
  52. <!-- 第二组 -->
  53. <li>
  54. <a href=""><img src="static/images/dh/nav-6.png" alt="" /></a>
  55. <a href="">充值缴费</a>
  56. </li>
  57. <li>
  58. <a href=""><img src="static/images/dh/nav-7.png" alt="" /></a>
  59. <a href="">9.9元拼</a>
  60. </li>
  61. <li>
  62. <a href=""><img src="static/images/dh/nav-8.png" alt="" /></a>
  63. <a href="">领券</a>
  64. </li>
  65. <li>
  66. <a href=""><img src="static/images/dh/nav-9.png" alt="" /></a>
  67. <a href="">领金贴</a>
  68. </li>
  69. <li>
  70. <a href=""><img src="static/images/dh/nav-10.png" alt="" /></a>
  71. <a href="">PLUS会员</a>
  72. </li>
  73. </ul>
  74. <!-- 秒杀区 -->
  75. <div class="seckill">
  76. <!-- 头部 -->
  77. <div class="seckill-top">
  78. <div class="left">
  79. <div class="title">京东秒杀</div>
  80. <div class="notice">
  81. <div class="tips">23点专场</div>
  82. <div class="time">22:11:22</div>
  83. </div>
  84. </div>
  85. <div class="right">更多秒杀</div>
  86. </div>
  87. <!-- 秒杀主体区 -->
  88. <ul class="seckill-body">
  89. <li class="item">
  90. <a href=""><img src="static/images/ms/ms-1.jpg" alt="" /></a>
  91. <div class="iconfont icon-rmb">338</div>
  92. <div class="iconfont icon-rmb">558</div>
  93. </li>
  94. <li class="item">
  95. <a href=""><img src="static/images/ms/ms-2.jpg" alt="" /></a>
  96. <div class="iconfont icon-rmb">3456</div>
  97. <div class="iconfont icon-rmb">4567</div>
  98. </li>
  99. <li class="item">
  100. <a href=""><img src="static/images/ms/ms-3.jpg" alt="" /></a>
  101. <div class="iconfont icon-rmb">789</div>
  102. <div class="iconfont icon-rmb">999</div>
  103. </li>
  104. <li class="item">
  105. <a href=""><img src="static/images/ms/ms-4.jpg" alt="" /></a>
  106. <div class="iconfont icon-rmb">3213</div>
  107. <div class="iconfont icon-rmb">4532</div>
  108. </li>
  109. </ul>
  110. </div>
  111. <!-- 推荐区 -->
  112. <h2 class="title">猜你喜欢</h2>
  113. <ul class="love">
  114. <li class="item">
  115. <a href=""><img src="static/images/sp/sp-1.webp" alt="" /></a>
  116. <p>商品介绍商品介绍商品介绍商品介绍商品介绍商品介绍商品介绍商品介品介绍商品介绍</p>
  117. <div class="price">
  118. <div class="iconfont icon-rmb">203</div>
  119. <div>看相似</div>
  120. </div>
  121. </li>
  122. <li class="item">
  123. <a href=""><img src="static/images/sp/sp-2.webp" alt="" /></a>
  124. <p>商品介绍商品介绍商品介绍商品介绍商品介绍商品介绍商品介绍商品介品介绍商品介绍</p>
  125. <div class="price">
  126. <div class="iconfont icon-rmb">203</div>
  127. <div>看相似</div>
  128. </div>
  129. </li>
  130. <li class="item">
  131. <a href=""><img src="static/images/sp/sp-3.webp" alt="" /></a>
  132. <p>商品介绍商品介绍商品介绍商品介绍商品介绍商品介绍商品介绍商品介品介绍商品介绍</p>
  133. <div class="price">
  134. <div class="iconfont icon-rmb">203</div>
  135. <div>看相似</div>
  136. </div>
  137. </li>
  138. <li class="item">
  139. <a href=""><img src="static/images/sp/sp-4.webp" alt="" /></a>
  140. <p>商品介绍商品介绍商品介绍商品介绍商品介绍商品介绍商品介绍商品介品介绍商品介绍</p>
  141. <div class="price">
  142. <div class="iconfont icon-rmb">203</div>
  143. <div>看相似</div>
  144. </div>
  145. </li>
  146. <li class="item">
  147. <a href=""><img src="static/images/sp/sp-5.webp" alt="" /></a>
  148. <p>商品介绍商品介绍商品介绍商品介绍商品介绍商品介绍商品介绍商品介品介绍商品介绍</p>
  149. <div class="price">
  150. <div class="iconfont icon-rmb">203</div>
  151. <div>看相似</div>
  152. </div>
  153. </li>
  154. <li class="item">
  155. <a href=""><img src="static/images/sp/sp-6.webp" alt="" /></a>
  156. <p>商品介绍商品介绍商品介绍商品介绍商品介绍商品介绍商品介绍商品介品介绍商品介绍</p>
  157. <div class="price">
  158. <div class="iconfont icon-rmb">203</div>
  159. <div>看相似</div>
  160. </div>
  161. </li>
  162. </ul>
  163. </main>
  164. <!-- 页脚 -->
  165. <footer class="footer">
  166. <div>
  167. <div class="iconfont icon-home"></div>
  168. <span>主页</span>
  169. </div>
  170. <div>
  171. <div class="iconfont icon-layers"></div>
  172. <span>分类</span>
  173. </div>
  174. <div>
  175. <div class="iconfont icon-kehuguanli"></div>
  176. <span>京喜</span>
  177. </div>
  178. <div>
  179. <div class="iconfont icon-shopping-cart"></div>
  180. <span>购物车</span>
  181. </div>
  182. <div>
  183. <div class="iconfont icon-user"></div>
  184. <span>未登录</span>
  185. </div>
  186. </footer>
  187. </body>
  188. </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. text-decoration: none;
  11. color: #7b7b7b;
  12. }
  13. :root {
  14. font-size: 10px;
  15. }
  16. body {
  17. color: aliceblue;
  18. }
  19. @media screen and (min-width: 480px) {
  20. :root {
  21. font-size: 12px;
  22. }
  23. }
  24. @media screen and (min-width: 640px) {
  25. :root {
  26. font-size: 14px;
  27. }
  28. }
  29. @media screen and (min-width: 720px) {
  30. :root {
  31. font-size: 16px;
  32. }
  33. }
index.css
  1. @import url(reset.css);
  2. header {
  3. position: fixed;
  4. height: 4.4rem;
  5. left: 0;
  6. top: 0;
  7. right: 0;
  8. background-color: #e43130;
  9. z-index: 100;
  10. }
  11. main {
  12. position: absolute;
  13. left: 0;
  14. right: 0;
  15. top: 4.4rem;
  16. bottom: 4.4rem;
  17. }
  18. footer {
  19. position: fixed;
  20. height: 4.4rem;
  21. left: 0;
  22. right: 0;
  23. bottom: 0;
  24. background-color: #ffffff;
  25. z-index: 100;
  26. }
header.css
  1. .header {
  2. display: flex;
  3. flex-flow: row nowrap;
  4. align-items: center;
  5. }
  6. .header .menu {
  7. flex: 1;
  8. color: #ffecec;
  9. text-align: center;
  10. font-size: 2.5rem;
  11. }
  12. .header .login {
  13. flex: 1;
  14. color: #ffecec;
  15. text-align: center;
  16. font-size: 2rem;
  17. }
  18. .header .search {
  19. flex: 6;
  20. padding: 0.5rem;
  21. display: flex;
  22. align-items: center;
  23. background-color: #ffffff;
  24. border-radius: 3rem;
  25. }
  26. .header .search .logo {
  27. flex: 0 1 4rem;
  28. color: #e43130;
  29. font-size: 2rem;
  30. line-height: 2rem;
  31. text-align: center;
  32. }
  33. .header .search .icon-search {
  34. flex: 0 1 4rem;
  35. color: #7b7b7b;
  36. border-left: 1px solid #7b7b7b;
  37. line-height: 2rem;
  38. text-align: center;
  39. }
  40. .header .search :last-child {
  41. flex: auto;
  42. color: #7b7b7b;
  43. border: none;
  44. outline: none;
  45. }
  1. .footer {
  2. color: rgb(119, 119, 119);
  3. display: flex;
  4. justify-content: space-around;
  5. align-items: center;
  6. }
  7. .footer div {
  8. display: flex;
  9. flex-flow: column wrap;
  10. justify-content: center;
  11. align-items: center;
  12. font-size: 2rem;
  13. }
  14. .footer div span {
  15. font-size: 1rem;
  16. }
  17. .footer div:hover {
  18. cursor: pointer;
  19. }
nav.css
  1. .main .nav {
  2. display: flex;
  3. flex-flow: row wrap;
  4. justify-content: space-around;
  5. align-items: center;
  6. }
  7. main .nav li {
  8. flex: 1 1 20%;
  9. padding: 0.5rem;
  10. display: flex;
  11. flex-flow: column wrap;
  12. justify-content: center;
  13. align-items: center;
  14. }
  15. main .nav li a img {
  16. height: 4rem;
  17. }
seckill.css
  1. .main {
  2. background-color: #f6f6f6;
  3. }
  4. .main .seckill {
  5. display: flex;
  6. flex-flow: column;
  7. background-color: #fff;
  8. border-radius: 1rem;
  9. margin: 1rem;
  10. }
  11. .main .seckill .seckill-top {
  12. display: flex;
  13. flex-flow: row nowrap;
  14. justify-content: space-between;
  15. padding: 0.5rem;
  16. }
  17. .main .seckill .seckill-top .left {
  18. display: flex;
  19. }
  20. .main .seckill .seckill-top .left .title {
  21. font-size: 1.5rem;
  22. color: #979797;
  23. }
  24. .main .seckill .seckill-top .left .notice {
  25. display: flex;
  26. font-size: 1.2rem;
  27. color: #e43130;
  28. }
  29. .main .seckill .seckill-top .right {
  30. font-size: 1.2rem;
  31. color: #979797;
  32. }
  33. .main .seckill .seckill-body {
  34. display: flex;
  35. justify-content: space-between;
  36. }
  37. .main .seckill .seckill-body .item {
  38. display: flex;
  39. flex-flow: column wrap;
  40. justify-content: center;
  41. align-items: center;
  42. }
  43. .main .seckill .seckill-body .item img {
  44. height: 8rem;
  45. width: 8rem;
  46. }
  47. .main .seckill .seckill-body .item > :nth-child(2) {
  48. color: #e43130;
  49. font-size: small;
  50. }
  51. .main .seckill .seckill-body .item > :last-child {
  52. color: #979797;
  53. font-size: small;
  54. text-decoration: line-through;
  55. }
love.css
  1. .main > h2 {
  2. color: #777;
  3. text-align: center;
  4. font-size: 2rem;
  5. height: 3rem;
  6. line-height: 3rem;
  7. }
  8. .main .love {
  9. font-size: 1rem;
  10. display: flex;
  11. flex-flow: row wrap;
  12. justify-content: space-between;
  13. }
  14. .main .love .item {
  15. flex: 1 0 calc(50% - 2rem);
  16. background-color: #fff;
  17. overflow: hidden;
  18. display: flex;
  19. flex-flow: column wrap;
  20. margin-left: 1rem;
  21. margin-bottom: 1rem;
  22. border-radius: 1rem;
  23. padding-bottom: 1rem;
  24. padding: 1rem;
  25. color: #777;
  26. }
  27. .main .love .item img {
  28. width: 100%;
  29. height: 100%;
  30. }
  31. .main .love .item .price {
  32. display: flex;
  33. justify-content: space-between;
  34. align-items: center;
  35. }
  36. .main .love .item .price div:first-of-type {
  37. color: #e43130;
  38. padding: 0.2rem 1rem;
  39. }
  40. .main .love .item .price div:last-of-type {
  41. color: #666;
  42. background-color: #979797;
  43. border-radius: 1rem;
  44. padding: 0.2rem 1rem;
  45. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议