经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
清晰的CSS 3媒体查询响应式布局,bootstrap 框架原理实战
来源:cnblogs  作者:陈莺莺呀  时间:2020/11/16 10:07:56  对本文有异议

媒体设备类型使用详解:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
  7. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  8. <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>
  9.  
  10. <!-- 屏幕设备 -->
  11. <style media="screen">
  12. h1{
  13. color:red;
  14. }
  15. </style>
  16.  
  17. <!-- 打印机设备 -->
  18. <style media="print">
  19. h1{
  20. color:green;
  21. }
  22. </style>
  23.  
  24. <!-- 屏幕设备和打印机设备 -->
  25. <style media="screen,print">
  26. h1{
  27. font-weight:normal;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <h1>cyy</h1>
  33. </body>
  34. </html>

 

使用link标签设置媒体类型:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
  7. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  8. <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>
  9.  
  10. <!-- 默认media为all,所有设备 -->
  11. <link rel="stylesheet" href="css/commob.css" media="all">
  12. <link rel="stylesheet" href="css/screen.css" media="screen">
  13. <link rel="stylesheet" href="css/print.css" media="print">
  14. </head>
  15. <body>
  16. <h1>cyy</h1>
  17. </body>
  18. </html>

 

使用@import简化页面多文件引用:

这是推荐的做法:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
  7. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  8. <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>
  9.  
  10. <link rel="stylesheet" href="css/style.css">
  11. </head>
  12. <body>
  13. <h1>cyy</h1>
  14. </body>
  15. </html>

style.css

  1. @import url(common.css);
  2. @import url(screen.css) screen;
  3. @import url(print.css) print;

 

样式表中使用@media局部定义响应查询:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
  7. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  8. <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>
  9.  
  10. <link rel="stylesheet" href="css/style.css">
  11. </head>
  12. <body>
  13. <div class="navbar">
  14. <a href="">cyy</a>
  15. <ul>
  16. <li>cyy1</li>
  17. <li>cyy2</li>
  18. <li>cyy3</li>
  19. </ul>
  20. </div>
  21. </body>
  22. </html>

相关less

  1. .navbar{
  2. height:60px;
  3. width:900px;
  4. display:flex;
  5. align-items:center;
  6. background:#f3f3f3;
  7. margin:0 auto;
  8. ul{
  9. list-style:none;
  10. display:flex;
  11. }
  12. }
  13. @media screen and (max-width:600px){
  14. .navbar{
  15. ul{
  16. display:none;
  17. }
  18. }
  19. }

 

and条件判断响应式应用:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
  7. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  8. <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>
  9.  
  10. <style media="screen and (min-width:768px) and (max-width:1000px)">
  11. h1{
  12. color:red;
  13. }
  14. </style>
  15.  
  16. <style media="screen and (max-width:768px)">
  17. h1{
  18. color:blue;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <h1>CYY</h1>
  24. </body>
  25. </html>

 

逻辑或使用技巧操作:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
  7. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  8. <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>
  9.  
  10. <!-- 当设备为横屏展示,或者设备宽度大于768px不论横竖屏时 -->
  11. <style media="screen and (orientation:landscape),screen and (min-width:768px)">
  12. h1{
  13. color:red;
  14. }
  15. </style>
  16.  
  17. </head>
  18. <body>
  19. <h1>CYY</h1>
  20. </body>
  21. </html>

 

not关键词使用注意要点:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
  7. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  8. <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>
  9.  
  10. <style>
  11. /* 如果满足条件,则不应用该样式 */
  12. @media not screen and (min-width:500px) and (max-width:768px){
  13. h1{
  14. color:red;
  15. }
  16. }
  17. </style>
  18.  
  19. </head>
  20. <body>
  21. <h1>CYY</h1>
  22. </body>
  23. </html>

 

使用only排除低端浏览器:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
  7. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  8. <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>
  9.  
  10. <style>
  11. /* 加上only之后,低端浏览器会忽略该句语法 */
  12. @media only screen and (min-width:500px){
  13. h1{
  14. color:red;
  15. }
  16. }
  17. </style>
  18.  
  19. </head>
  20. <body>
  21. <h1>CYY</h1>
  22. </body>
  23. </html>

 

实战案例操作之文件结构:

html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
  7. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  8. <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>
  9.  
  10. <link rel="stylesheet" href="css/style.css">
  11.  
  12. </head>
  13. <body>
  14. <header class="mb-2">
  15. <div class="container">
  16. <div class="navbar">
  17. <a href="" class="logo">CYY</a>
  18. <label for="toggle-nav"><i class="fa fa-tasks" aria-hidden="true"></i></label>
  19. <input type="checkbox" name="" id="toggle-nav">
  20. <div class="collapse">
  21. <ul class="links">
  22. <li><a href="">系统学习</a></li>
  23. <li><a href="">实战课程</a></li>
  24. <li><a href="">话题讨论</a></li>
  25. <li><a href="">签到打卡</a></li>
  26. </ul>
  27. <div class="form">
  28. <a href="">登录</a>
  29. <a href="" class="form-bg">注册</a>
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34. </header>
  35.  
  36. <div class="container">
  37. <div class="row">
  38. <div class="col-6 col-lg-9 col-xs-12">
  39. <div class="card">
  40. <div class="card-header">
  41. <h2>最新更新</h2>
  42. </div>
  43. <div class="card-body">
  44. <ul class="list-group">
  45. <li>
  46. <a href="">cyy开始学习响应式布局</a>
  47. <span>2020-11-13</span>
  48. </li>
  49. <li>
  50. <a href="">cyy开始学习响应式布局</a>
  51. <span>2020-11-13</span>
  52. </li>
  53. <li>
  54. <a href="">cyy开始学习响应式布局</a>
  55. <span>2020-11-13</span>
  56. </li>
  57. <li>
  58. <a href="">cyy开始学习响应式布局</a>
  59. <span>2020-11-13</span>
  60. </li>
  61. <li>
  62. <a href="">cyy开始学习响应式布局</a>
  63. <span>2020-11-13</span>
  64. </li>
  65. <li>
  66. <a href="">cyy开始学习响应式布局</a>
  67. <span>2020-11-13</span>
  68. </li>
  69. <li>
  70. <a href="">cyy开始学习响应式布局</a>
  71. <span>2020-11-13</span>
  72. </li>
  73. <li>
  74. <a href="">cyy开始学习响应式布局</a>
  75. <span>2020-11-13</span>
  76. </li>
  77. </ul>
  78. </div>
  79. <div class="card-footer">
  80. <div class="page">
  81. <a href=""><</a>
  82. <a href="">1</a>
  83. <a href="">2</a>
  84. <a href="" class="current">3</a>
  85. <a href="">4</a>
  86. <a href="">5</a>
  87. <a href="">></a>
  88. </div>
  89. </div>
  90. </div>
  91. </div>
  92. <div class="col-6 col-lg-3 col-xs-12">
  93. <div class="card">
  94. <div class="card-header">
  95. <h3>社区小帖</h3>
  96. </div>
  97. <div class="card-body"></div>
  98. <div class="card-footer"></div>
  99. </div>
  100. </div>
  101. </div>
  102. </div>
  103. </body>
  104. </html>

引入统一控制的style.css

  1. @import url(common.css);
  2. @import url(navbar.css);
  3. @import url(card.css);
  4. @import url(title.css);
  5. @import url(page.css);
  6. @import url(margin.css);
  7. @import url(list-group.css);
  8. @import url(small-x.css) only screen and (max-width:768px);
  9. @import url(small.css) only screen and (min-width:768px);
  10. @import url(medium.css) only screen and (min-width:960px);
  11. @import url(big.css) only screen and (min-width:1200px);
common.less
  1. *{
  2. margin:0;
  3. padding:0;
  4. }
  5. :root{
  6. font-size:15px;
  7. }
  8. a{
  9. text-decoration:none;
  10. }
  11. ul{
  12. list-style:none;
  13. }
  14. .row{
  15. display:grid;
  16. grid-template-columns:repeat(12,1fr);
  17. }
  18. .col-12{
  19. grid-column:span 12;
  20. }
  21. .col-11{
  22. grid-column:span 11;
  23. }
  24. .col-10{
  25. grid-column:span 10;
  26. }
  27. .col-9{
  28. grid-column:span 9;
  29. }
  30. .col-8{
  31. grid-column:span 8;
  32. }
  33. .col-7{
  34. grid-column:span 7;
  35. }
  36. .col-6{
  37. grid-column:span 6;
  38. }
  39. .col-5{
  40. grid-column:span 5;
  41. }
  42. .col-4{
  43. grid-column:span 4;
  44. }
  45. .col-3{
  46. grid-column:span 3;
  47. }
  48. .col-2{
  49. grid-column:span 2;
  50. }
  51. .col-1{
  52. grid-column:span 1;
  53. }
  54. h2{
  55. font-weight:normal;
  56. }

 

导航组件navbar.less

  1. header{
  2. border-bottom:5px solid #009688;
  3. box-shadow:0 5px 5px rgba(0,0,0,.2);
  4. .navbar{
  5. display:flex;
  6. padding:1rem 0;
  7. align-items:center;
  8. .logo{
  9. color:#009688;
  10. margin-right:20px;
  11. font-weight:bold;
  12. font-size:1.3rem;
  13. &+label{
  14. display:none;
  15. &+input{
  16. display:none;
  17. }
  18. }
  19. }
  20. .collapse{
  21. display:flex;
  22. flex-grow:1;
  23. .links{
  24. display:flex;
  25. // 占满剩余空间
  26. margin-right:auto;
  27. li{
  28. margin:0 10px;
  29. a{
  30. color:#777;
  31. &:hover{
  32. color:#333;
  33. font-weight:bold;
  34. }
  35. }
  36. }
  37. }
  38. .form{
  39. a{
  40. border:1px solid #009688;
  41. color:#009688;
  42. padding:.3rem 1rem;
  43. border-radius:.3rem;
  44. &.form-bg{
  45. background:#009688;
  46. color:white;
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. @media screen and (max-width:960px){
  54. header{
  55. .navbar{
  56. // flex-flow 属性是 flex-direction 和 flex-wrap 属性的复合属性。
  57. flex-flow:row wrap;
  58. .logo{
  59. margin-right:auto;
  60. &+label{
  61. display:block;
  62. border:1px solid #ddd;
  63. padding:.5rem 1rem;
  64. color:#555;
  65. cursor:pointer;
  66. &+input{
  67. display:none;
  68. }
  69. &+input:checked{
  70. &+.collapse{
  71. display:block;
  72. }
  73. }
  74. }
  75. }
  76. .collapse{
  77. display:none;
  78. flex-flow:column;
  79. width:100%;
  80. .links{
  81. flex-direction:column;
  82. margin-bottom:1.5rem;
  83. li{
  84. margin:.5rem 0;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }

 

卡片组件card.less

  1. .card{
  2. border:1px solid #ddd;
  3. box-shadow:0 0 5px rgba(0,0,0,.1);
  4. border-radius:.2rem;
  5. .card-header{
  6. padding:.5rem 1rem;
  7. border-bottom:1px solid #ddd;
  8. }
  9. .card-body{
  10. padding:1rem;
  11. }
  12. .card-footer{
  13. padding:.5rem 1rem;
  14. border-top:1px solid #ddd;
  15. }
  16. }

 

文本组件title.less

  1. h2 {
  2. font-size: 1rem;
  3. }
  4. h3 {
  5. font-size: 0.8rem;
  6. }
  7. h2,
  8. h3,
  9. h4 {
  10. color: #555;
  11. }

分页组件page.less

  1. .page{
  2. display:flex;
  3. a{
  4. display:block;
  5. padding:.3rem .8rem;
  6. border:1px solid #ddd;
  7. // 让重叠的两个边框线叠加在一起
  8. margin-left:-1px;
  9. color:#555;
  10. &:first-child{
  11. border-top-left-radius:.3rem;
  12. border-bottom-left-radius:.3rem;
  13. }
  14. &:last-child{
  15. border-top-right-radius:.3rem;
  16. border-bottom-right-radius:.3rem;
  17. }
  18. &.current{
  19. background:#009688;
  20. color:white;
  21. border:1px solid #009688;
  22. }
  23. }
  24. }

 

边距组件margin.less

  1. .mb-1 {
  2. margin-bottom: 1rem;
  3. }
  4. .mb-2 {
  5. margin-bottom: 2rem;
  6. }
  7. .mb-3 {
  8. margin-bottom: 3rem;
  9. }

列表组件list-group.less

  1. .list-group{
  2. li{
  3. display:flex;
  4. justify-content:space-between;
  5. padding:.8rem 0;
  6. border-bottom:1px solid #ddd;
  7. font-size:.9rem;
  8. &:last-child{
  9. border-bottom:none;
  10. }
  11. a{
  12. color:#777;
  13. }
  14. span{
  15. color:#888;
  16. font-size:.6rem;
  17. }
  18. }
  19. }

 

超小屏适配 small-x.less

  1. body {
  2. background: white;
  3. }
  4. .container {
  5. width: 95%;
  6. margin: 0 auto;
  7. }
  8. .col-xs-12 {
  9. grid-column: span 12;
  10. }
  11. .col-xs-11 {
  12. grid-column: span 11;
  13. }
  14. .col-xs-10 {
  15. grid-column: span 10;
  16. }
  17. .col-xs-9 {
  18. grid-column: span 9;
  19. }
  20. .col-xs-8 {
  21. grid-column: span 8;
  22. }
  23. .col-xs-7 {
  24. grid-column: span 7;
  25. }
  26. .col-xs-6 {
  27. grid-column: span 6;
  28. }
  29. .col-xs-5 {
  30. grid-column: span 5;
  31. }
  32. .col-xs-4 {
  33. grid-column: span 4;
  34. }
  35. .col-xs-3 {
  36. grid-column: span 3;
  37. }
  38. .col-xs-2 {
  39. grid-column: span 2;
  40. }
  41. .col-xs-1 {
  42. grid-column: span 1;
  43. }

小屏适配small.less

  1. body{
  2. background:white;
  3. }
  4. .container{
  5. width:750px;
  6. margin:0 auto;
  7.  
  8. }
  9. .col-sm-12{
  10. grid-column:span 12;
  11. }
  12. .col-sm-11{
  13. grid-column:span 11;
  14. }
  15. .col-sm-10{
  16. grid-column:span 10;
  17. }
  18. .col-sm-9{
  19. grid-column:span 9;
  20. }
  21. .col-sm-8{
  22. grid-column:span 8;
  23. }
  24. .col-sm-7{
  25. grid-column:span 7;
  26. }
  27. .col-sm-6{
  28. grid-column:span 6;
  29. }
  30. .col-sm-5{
  31. grid-column:span 5;
  32. }
  33. .col-sm-4{
  34. grid-column:span 4;
  35. }
  36. .col-sm-3{
  37. grid-column:span 3;
  38. }
  39. .col-sm-2{
  40. grid-column:span 2;
  41. }
  42. .col-sm-1{
  43. grid-column:span 1;
  44. }

中屏适配medium.less

  1. body{
  2. background:white;
  3. }
  4. .container{
  5. width:950px;
  6. margin:0 auto;
  7.  
  8. }
  9. .col-md-12{
  10. grid-column:span 12;
  11. }
  12. .col-md-11{
  13. grid-column:span 11;
  14. }
  15. .col-md-10{
  16. grid-column:span 10;
  17. }
  18. .col-md-9{
  19. grid-column:span 9;
  20. }
  21. .col-md-8{
  22. grid-column:span 8;
  23. }
  24. .col-md-7{
  25. grid-column:span 7;
  26. }
  27. .col-md-6{
  28. grid-column:span 6;
  29. }
  30. .col-md-5{
  31. grid-column:span 5;
  32. }
  33. .col-md-4{
  34. grid-column:span 4;
  35. }
  36. .col-md-3{
  37. grid-column:span 3;
  38. }
  39. .col-md-2{
  40. grid-column:span 2;
  41. }
  42. .col-md-1{
  43. grid-column:span 1;
  44. }

大屏适配big.less

  1. body{
  2. background:transparent;
  3. }
  4. .container{
  5. width:1180px;
  6. margin:0 auto;
  7.  
  8. }
  9. .col-lg-12{
  10. grid-column:span 12;
  11. }
  12. .col-lg-11{
  13. grid-column:span 11;
  14. }
  15. .col-lg-10{
  16. grid-column:span 10;
  17. }
  18. .col-lg-9{
  19. grid-column:span 9;
  20. }
  21. .col-lg-8{
  22. grid-column:span 8;
  23. }
  24. .col-lg-7{
  25. grid-column:span 7;
  26. }
  27. .col-lg-6{
  28. grid-column:span 6;
  29. }
  30. .col-lg-5{
  31. grid-column:span 5;
  32. }
  33. .col-lg-4{
  34. grid-column:span 4;
  35. }
  36. .col-lg-3{
  37. grid-column:span 3;
  38. }
  39. .col-lg-2{
  40. grid-column:span 2;
  41. }
  42. .col-lg-1{
  43. grid-column:span 1;
  44. }

效果图

 

使用rem单位操作尺寸响应处理

推荐使用自动化构建工具插件。

 

原文链接:http://www.cnblogs.com/chenyingying0/p/13968929.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号