当前位置:  首页>> 技术小册>> Web响应式布局入门到实战

布局特点:
确保一个页面在所有终端上,都能显示出令人满意的效果。

一句话:一套方案处处运行。

设计思路:
使用%或rem作为单位实现。

代码示例:

  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,user-scalable=no">
  7. <title>Document</title>
  8. <link rel="stylesheet" href="big.css" media="(min-device-width:1000px)">
  9. <link rel="stylesheet" href="small.css"
  10. media="(min-device-width:400px) and (max-device-width:600px) ">
  11. </head>
  12. <body>
  13. <div id="layout">
  14. <div id="top">
  15. </div>
  16. <div id="main">
  17. <div>
  18. <li>分类1</li>
  19. <li>分类2</li>
  20. <li>分类3</li>
  21. <li>分类4</li>
  22. <li>分类5</li>
  23. <li>分类6</li>
  24. </div>
  25. <div>
  26. <li>图片</li>
  27. <li>图片</li>
  28. <li>图片</li>
  29. <li>图片</li>
  30. <li>图片</li>
  31. <li>图片</li>
  32. <li>图片</li>
  33. <li>图片</li>
  34. </div>
  35. </div>
  36. </div>
  37. </body>
  38. </html>

big.css:

  1. *{
  2. margin: 0;
  3. padding: 0;
  4. background-color: #f5f5f5;
  5. }
  6. #layout{
  7. display:flex;
  8. flex-direction: column;
  9. width: 80%; /* pc端的宽度,可以设置为当前视口的百分之80或90 */
  10. margin: 0 auto;
  11. }
  12. #top{
  13. width:100%;
  14. flex:0 0 50px;
  15. margin: 0 auto;
  16. background-color: yellow;
  17. }
  18. #main{
  19. flex: 0 0 100%;
  20. display: flex;
  21. }
  22. /* 分类样式 */
  23. #main div:nth-child(1){
  24. flex: 0 0 10%;
  25. background-color: #f5f5f5;
  26. list-style-type: none;
  27. display: flex;
  28. flex-wrap:wrap;
  29. border-left:1px solid white;
  30. border-right:1px solid white;
  31. }
  32. #main div:nth-child(1) li{
  33. flex: 0 0 100%;
  34. height:40px;
  35. line-height: 40px;
  36. text-align: center;
  37. border-bottom:1px solid white;
  38. }
  39. /* 右边部分 */
  40. #main div:nth-child(2){
  41. flex: 0 0 90%;
  42. background-color: #f5f5f5;
  43. list-style-type: none;
  44. display: flex;
  45. flex-wrap:wrap;
  46. justify-content: space-around;
  47. }
  48. #main div:nth-child(2) li{
  49. flex: 0 0 30%;
  50. height:120px;
  51. text-align: center;
  52. border-bottom:1px solid white;
  53. background-color: yellow;
  54. margin-top:10px;
  55. }

实现效果:

移动端:

small.css:

  1. *{
  2. margin: 0;
  3. padding: 0;
  4. background-color: #f5f5f5;
  5. }
  6. #layout{
  7. display:flex;
  8. flex-direction: column;
  9. width: 100%; /* 移动端占100% */
  10. margin: 0 auto;
  11. }
  12. #top{
  13. width:100%;
  14. flex:0 0 50px;
  15. margin: 0 auto;
  16. background-color: yellow;
  17. }
  18. #main{
  19. flex: 0 0 100%;
  20. display: flex;
  21. flex-wrap: wrap;
  22. }
  23. /* 分类样式 */
  24. #main div:nth-child(1){
  25. flex: 0 0 100%; /* 分类占100% */
  26. background-color: #f5f5f5;
  27. list-style-type: none;
  28. display: flex;
  29. flex-wrap:wrap;
  30. border-left:1px solid white;
  31. border-right:1px solid white;
  32. align-content: flex-start;
  33. }
  34. #main div:nth-child(1) li{
  35. flex: 0 0 30%;
  36. height:40px;
  37. line-height: 40px;
  38. text-align: center;
  39. border-bottom:1px solid white;
  40. }
  41. /* 右边部分 */
  42. #main div:nth-child(2){
  43. flex: 0 0 100%;
  44. background-color: #f5f5f5;
  45. list-style-type: none;
  46. display: flex;
  47. flex-wrap:wrap;
  48. justify-content: space-around;
  49. }
  50. #main div:nth-child(2) li{
  51. flex: 0 0 30%;
  52. height:120px;
  53. text-align: center;
  54. border-bottom:1px solid white;
  55. background-color: yellow;
  56. margin-top:10px;
  57. }

实现效果:

优化方式:
将big.css和small.css中公共的代码分离出来为common.css。
而区分宽度的地方,分别写在big.css和small.css文件里。


该分类下的相关小册推荐: