Blogger Information
Blog 31
fans 2
comment 0
visits 27686
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
flex实例:PC端和移动端布局
霏梦
Original
927 people have browsed it

— 作者:霏梦

pc端通用布局解决方案flex

  • html代码如下:
    1. <div class="box">
    2. <div class="header">
    3. <a href="#">LOGO</a>
    4. <a href="">产品</a>
    5. <a href="">定价</a>
    6. <a href="">解决方案</a>
    7. <a href="">支持</a>
    8. <a href="">下载</a>
    9. <a href="">免费使用</a>
    10. </div>
    11. <div class="container">
    12. <div class="aside">我在左边</div>
    13. <div class="main">我是内容区</div>
    14. <div class="aside">我在右边</div>
    15. </div>
    16. <div class="footer">
    17. <p><a href="">使用条款</a> <a href="">隐私政策</&a></p>
    18. <p>霏梦公司 Copyright © 2020 京ICP备080000000号</p>
    19. </div>
    20. </div>
  • CSS代码如下:

    1. <style>
    2. /* 初始化 */
    3. * {
    4. margin: 0;
    5. padding: 0;
    6. box-sizing: border-box;
    7. }
    8. /* body转为flex */
    9. body{
    10. min-width: 800px;
    11. display: flex;
    12. /* 垂直不换行 */
    13. flex-flow: row nowrap;
    14. justify-content: center;
    15. }
    16. /* box转为flex */
    17. .box {
    18. min-width: 800px;
    19. display: flex;
    20. /* 垂直不换行 */
    21. flex-flow: column nowrap;
    22. }
    23. .header,
    24. .footer{
    25. height: 50px;
    26. border: 1px solid black;
    27. }
    28. .header{
    29. display: flex;
    30. /* 交叉轴上垂直居中显示 */
    31. align-items: center;
    32. }
    33. .header > a {
    34. color: coral;
    35. text-decoration: none;
    36. flex: 0 1 100px;
    37. /* 文本居中 */
    38. text-align: center;
    39. }
    40. .header > a:first-of-type{
    41. margin-right: 50px;
    42. }
    43. .header > a:last-of-type{
    44. /* 免费使用 往最右 */
    45. margin-left: auto;
    46. }
    47. /* 鼠标悬停时忽略logo */
    48. .header >a:hover:not(:first-of-type){
    49. background-color: chartreuse;
    50. color:darkblue;
    51. }
    52. .container {
    53. display: flex;
    54. min-height: 300px;
    55. margin: 10px auto;
    56. justify-content: center;
    57. text-align: center;
    58. }
    59. .container > .aside,
    60. .container>.main{
    61. border: 1px solid darkorange;
    62. padding: 10px;
    63. }
    64. .container >.aside{
    65. flex:0 0 200px;
    66. }
    67. .container >.main{
    68. flex: 0 0 380px;
    69. margin: 0 10px;
    70. }
    71. .footer{
    72. display: flex;
    73. /* 垂直方向不换行*/
    74. flex-flow: column nowrap;
    75. text-align: center;
    76. }
    77. .footer > p > a{
    78. text-decoration: none;
    79. }
    80. </style>
  • 如图所示:
    flex

移动端布局方案

  • html代码如下:

    1. <body>
    2. <!-- 页眉 -->
    3. <header>
    4. <a href="">LOGO</a>
    5. <span class="iconfont">&#xe61f;</span>
    6. </header>
    7. <!-- 轮播区域 -->
    8. <div class="slider">
    9. <img src="static/images/banner.jpg" alt="" />
    10. </div>
    11. <!-- 主要导航区域 -->
    12. <div class="nav">
    13. <div>
    14. <a href=""><img src="static/images/link1.webp" alt="" /></a>
    15. <a href="">天猫超市</a>
    16. </div>
    17. <div>
    18. <a href=""><img src="static/images/link2.webp" alt="" /></a>
    19. <a href="">服装百货</a>
    20. </div>
    21. <div>
    22. <a href=""><img src="static/images/link3.webp" alt="" /></a>
    23. <a href="">数码电器</a>
    24. </div>
    25. <div>
    26. <a href=""><img src="static/images/link1.webp" alt="" /></a>
    27. <a href="">图书精品</a>
    28. </div>
    29. <div>
    30. <a href=""><img src="static/images/link1.webp" alt="" /></a>
    31. <a href="">天猫超市</a>
    32. </div>
    33. <div>
    34. <a href=""><img src="static/images/link2.webp" alt="" /></a>
    35. <a href="">服装百货</a>
    36. </div>
    37. <div>
    38. <a href=""><img src="static/images/link3.webp" alt="" /></a>
    39. <a href="">数码电器</a>
    40. </div>
    41. <div>
    42. <a href=""><img src="static/images/link1.webp" alt="" /></a>
    43. <a href="">图书精品</a>
    44. </div>
    45. </div>
    46. <h2>
    47. 商品热销区
    48. <span class="iconfont hot" style="color: coral;">&#xe60b;</span>
    49. </h2>
    50. <div class="goods-hot">
    51. <div class="goods-img">
    52. <a href=""><img src="static/images/goods1.jpg" alt="" /></a>
    53. <p>Apple iPhone 11 128G</p>
    54. <div>
    55. <span>6299&nbsp;元</span>
    56. <span class="iconfont hot">&#xe602;</span>
    57. </div>
    58. </div>
    59. <div class="goods-img">
    60. <a href=""><img src="static/images/goods1.jpg" alt="" /></a>
    61. <p>Apple iPhone X 512G</p>
    62. <div>
    63. <span>8299&nbsp;元</span>
    64. <span class="iconfont hot">&#xe602;</span>
    65. </div>
    66. </div>
    67. <div class="goods-img">
    68. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    69. <p>华为笔记本电脑</p>
    70. <div>
    71. <span>5699&nbsp;元</span>
    72. <span class="iconfont hot">&#xe602;</span>
    73. </div>
    74. </div>
    75. <div class="goods-img">
    76. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    77. <p>小米笔记本电脑</p>
    78. <div>
    79. <span>3999&nbsp;元</span>
    80. <span class="iconfont hot">&#xe602;</span>
    81. </div>
    82. </div>
    83. <div class="goods-img">
    84. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    85. <p>联想笔记本电脑</p>
    86. <div>
    87. <span>4399&nbsp;元</span>
    88. <span class="iconfont hot">&#xe602;</span>
    89. </div>
    90. </div>
    91. </div>
    92. <!-- 商品列表 -->
    93. <h2 class="title">
    94. 商品列表
    95. <span class="iconfont hot" style="color: tomato;">&#xe64b;</span>
    96. </h2>
    97. <div class="goods-list">
    98. <div class="goods-desc">
    99. <a href=""><img src="static/images/goods4.jpg" alt="" /></a>
    100. <a href=""
    101. >[白条24期免息]Apple苹果iPhone 11 手机 128G 全网通, 免费领取500元话费,
    102. 今天17:00下单,明晨12:00之前送达,7天无理由退货,官方提供售后,测试的</a
    103. >
    104. </div>
    105. <div class="goods-desc">
    106. <a href=""><img src="static/images/goods3.jpg" alt="" /></a>
    107. <a href=""
    108. >西门子泳衣机 免费领取500元话费,
    109. 今天17:00下单,明晨12:00之前送达,7天无理由退货,官方提供售后,测试的</a
    110. >
    111. </div>
    112. <div class="goods-desc">
    113. <a href=""><img src="static/images/goods5.png" alt="" /></a>
    114. <a href=""
    115. >这是一部全屏手机,价格只要1元,颜色很多,免费3年维修更换,快递免费,上门更换</a
    116. >
    117. </div>
    118. <div class="goods-desc">
    119. <a href=""><img src="static/images/goods2.jpg" alt="" /></a>
    120. <a href=""
    121. >ROG冰锐2 15.6英寸240Hz电竞屏高色域轻薄便携游戏本笔记本电脑(R9-4900HS
    122. 8核7nm 16G 1TSSD RTX2060MaxQ</a
    123. >
    124. </div>
    125. </div>
    126. <!-- 页脚 -->
    127. <footer>
    128. <a href="">
    129. <span class="iconfont hot">&#xe60c;</span>
    130. <span>首页</span>
    131. </a>
    132. <a href="">
    133. <span class="iconfont hot">&#xe60c;</span>
    134. <span>分类</span>
    135. </a>
    136. <a href="">
    137. <span class="iconfont hot">&#xe60c;</span>
    138. <span>购物车</span>
    139. </a>
    140. <a href="">
    141. <span class="iconfont hot">&#xe60c;</span>
    142. <span>未登录</span>
    143. </a>
    144. </footer>
    145. </body>
  • css代码如下 :

    1. <style>
    2. /* 初始化 */
    3. * {
    4. margin: 0;
    5. padding: 0;
    6. box-sizing: border-box;
    7. }
    8. a {
    9. text-decoration: none;
    10. color: grey;
    11. }
    12. html {
    13. /* 可视区域的宽和高 */
    14. width: 100vw;
    15. height: 100vh;
    16. font-size: 14px;
    17. /* font-family:Arial, Helvetica, sans-serif; */
    18. color: grey;
    19. }
    20. body {
    21. min-width: 360px;
    22. background-color: white;
    23. display: flex;
    24. flex-flow: column nowrap;
    25. }
    26. header {
    27. color: white;
    28. background-color: #333;
    29. height: 30px;
    30. display: flex;
    31. justify-content: space-between;
    32. align-items: center;
    33. position: fixed;
    34. width: 100vw;
    35. padding: 0 10px;
    36. }
    37. .slider {
    38. height: 180px;
    39. }
    40. .slider > img {
    41. height: 100%;
    42. }
    43. .nav {
    44. height: 200px;
    45. margin-bottom: 10px;
    46. display: flex;
    47. /* 转为多行容器 */
    48. flex-flow: row wrap;
    49. align-content: space-around;
    50. }
    51. .nav > div {
    52. width: 25vw;
    53. display: flex;
    54. flex-flow: column nowrap;
    55. text-align: center;
    56. }
    57. .nav > div > a img {
    58. width: 50%;
    59. }
    60. .title {
    61. text-align: center;
    62. font-size: 1.6rem;
    63. font-weight: normal;
    64. }
    65. .goods-hot {
    66. border-top: 1px solid #cdcdcd;
    67. margin-top: 10px;
    68. font-size: 0.8rem;
    69. display: flex;
    70. /* 水平多行容器 */
    71. flex-flow: row wrap;
    72. }
    73. .goods-hot img {
    74. width: 100%;
    75. }
    76. .goods-hot > .goods-img {
    77. /* 内边距并重置大小 */
    78. padding: 10px;
    79. box-sizing: border-box;
    80. /* 允许放大不允许缩小,否则项目不会换行,多行容器失效 */
    81. flex: 1 0 30vw;
    82. /* 转为flex */
    83. display: flex;
    84. /* 主轴垂直且不允许换行 */
    85. flex-flow: column nowrap;
    86. justify-content: center;
    87. }
    88. /* 转flex,主轴上排列对齐方式 */
    89. .goods-hot > .goods-img > div {
    90. display: flex;
    91. /* 分散对齐 */
    92. justify-content: space-around;
    93. }
    94. .goods-list {
    95. padding: 10px;
    96. border: 1px solid #cdcdcd;
    97. margin-top: 10px;
    98. font-size: 14px;
    99. display: flex;
    100. /* 主轴必须是垂直 */
    101. flex-flow: column nowrap;
    102. }
    103. .goods-list > .goods-desc {
    104. display: flex;
    105. margin: 10px 0;
    106. }
    107. .goods-list > .goods-desc > a {
    108. padding: 10px 0;
    109. box-sizing: border-box;
    110. }
    111. /* 图片全部适应项目空间 */
    112. .goods-list > .goods-desc > a > img {
    113. width: 100%;
    114. }
    115. .goods-list > .goods-desc > a:last-of-type:hover {
    116. color: seagreen;
    117. }
    118. footer {
    119. color: #666;
    120. height: 60px;
    121. background-color: #efefef;
    122. border-top: 1px solid #666;
    123. position: fixed;
    124. bottom: 0;
    125. width: 100vw;
    126. display: flex;
    127. justify-content: space-around;
    128. }
    129. footer > a {
    130. margin-top: 10px;
    131. display: flex;
    132. /* 垂直排列不换行 */
    133. flex-flow: column nowrap;
    134. /* 交叉轴项目居中显示 */
    135. align-items: center;
    136. }
    137. footer a > span:first-of-type {
    138. font-size: 1.6rem;
    139. }
    140. .hot {
    141. /* 底部图标的颜色 */
    142. color: coral;
    143. }
    144. </style>
  • 图标样式和图片,请自行替换,参考阿里图标,这里就不写了
  • 效果图
    flex移动端

- 总结

  1. 建议用firefox浏览器,并写并看效果,并调试
  2. 要清楚flex的容器和项目,特别要知道主轴和交叉轴的显示
  3. 要会阿里图标的应用
  4. 需要注意的是:当时设置 flex 布局之后,子元素的 float、clear、vertical-align 的属性将会失效
  5. 有下面六种属性可以设置在容器上,它们分别是:
    • flex-direction
    • flex-wrap
    • flex-flow
    • justify-content
    • align-items
    • align-content
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