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

什么是媒体查询?
媒体查询又叫媒介查询,主要目的是为不同尺寸的屏幕设定不同的CSS样式。

示例代码:

  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>媒体查询</title>
  8. <style>
  9. .div0{
  10. width:200px;
  11. height:150px;
  12. border:1px solid red;
  13. }
  14. @media screen and (min-device-width:100px) and (max-device-width:300px){
  15. .div0{
  16. background-color: blue;
  17. }
  18. }
  19. @media screen and (min-device-width:301px) and (max-device-width:500px){
  20. .div0{
  21. background-color: green;
  22. }
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div class="div0">
  28. <p>div</p>
  29. </div>
  30. </body>
  31. </html>

当屏幕尺寸在100-300之间,301-500之间,超过500,分别对其设置样式:


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