媒体查询的运用

1. html结构 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Media Queries</title>
  </style>
  <link rel="stylesheet" media="screen and (max-width:768px)" href="index.css">
</head>
<body>
  <div id="widescreen">
    <h1>Widescreen</h1>
  </div>
  <div id="normal">
      <h1>normal</h1>
  </div>
  <div id="table">
      <h1>Table</h1>
  </div>
  <div id="samrtphone">
      <h1>Samrtphone</h1>
  </div>
  <div id="landscape">
      <h1>Landscape</h1>
  </div>
</body>
</html>

 

2. css 样式

    /* smartphone */
    
    body {
      font-family: Arial, Helvetica, sans-serif;
      background-color: gray;
      color: white;
      text-align: center;
      padding-top: 100px;
    }
    h1 {
      display: none;
    }
    @media(max-width:100px) {
      /* 当所用设备的宽超过500px样式不改变 当所用设备的宽不超过500px时 默认为手机端 */
      body {
          background: red;
      }
      #samrtphone h1 {
          display: block;
      }
    }

    @media (min-width: 101px) and (max-width:300px) {
      /* 当所用设备的宽超过500px样式不改变 当所用设备的宽不超过500px时 默认为手机端 */
      body {
          background: green;
      }
      #table h1 {
          display: block;
      }
    }

    @media (min-width: 301px) and (max-width:500px) {
      /* 当所用设备的宽超过500px样式不改变 当所用设备的宽不超过500px时 默认为手机端 */
      body {
          background: saddlebrown;
      }
      #normal h1 {
          display: block;
      }
    }

    @media (min-width: 501px) and (max-width:700px) {
      /* 当所用设备的宽超过500px样式不改变 当所用设备的宽不超过500px时 默认为手机端 */
      body {
          background: blue;
      }
      #widescreen h1 {
          display: block;
      }
    }

    @media (max-height:500px) {
      /* 当所用设备的宽超过500px样式不改变 当所用设备的宽不超过500px时 默认为手机端 */
      body {
          background: olive;
      }
      #widescreen h1 {
          display: block;
      }
    }

3. 重点

满足 这个条件执行 (max-width:100px)
@media(max-width:100px) {
      /* 当所用设备的宽超过500px样式不改变 当所用设备的宽不超过500px时 默认为手机端 */
      body {
          background: red;
      }
      #samrtphone h1 {
          display: block;
      }
    }
可以设置多个条件
@media screen (min-height: 400px) and (max-width:500px) {
      /* 当所用设备的宽超过500px样式不改变 当所用设备的宽不超过500px时 默认为手机端 */
      body {
          background: red;
      }
      #samrtphone h1 {
          display: block;
      }
    }
media="screen and (max-width:768px) 满足这个条件执行这个css样式里的代码
  <link rel="stylesheet" media="screen and (max-width:768px)" href="index.css">
posted @ 2022-03-25 09:59  会前端的洋  阅读(73)  评论(0编辑  收藏  举报