为什么 @media screen 无法在我的网站上运行?

我的 HTML 代码--


<div class="Card">

<div class="FestivalPhoto"><img src="Image/test1.jpg" width="300px" alt="MusicFestivalImage"></div>

<div class="FestivalDetails">Hello World</div>

</div>


<div class="Card">

<div class="FestivalPhoto"><img src="Image/test1.jpg" width="300px" alt="MusicFestivalImage"></div>

<div class="FestivalDetails">Hello World</div>

</div>


<div class="Card">

<div class="FestivalPhoto"><img src="Image/test1.jpg" width="300px" alt="MusicFestivalImage"></div>

<div class="FestivalDetails">Hello World</div>

</div>

这是我的 CSS——


.Card {

  background: #fff;

  border-radius: 2px;

  display: inline-block;

  height: 500px;

  margin: 1rem;

  position: relative;

  width: 300px;

  box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);

  transition: all 0.3s cubic-bezier(.25,.8,.25,1);

  padding: 30px auto;

  overflow: hidden;

  justify-content: center;

  }


 .FestivalDetails {

   position: absolute;

   width:auto;

   max-width: 500px;

   height: auto;

   display: block;

   padding: 20px 20px;

   }


 .FestivalPhoto {

   height: 200px;

   width: auto;

   position: relative;

   top: 0;

   display: block;

   overflow: hidden;

   margin: auto;

   background-size: initial;

  }


  @media(max-width: 700) {


   .Card {

     background: #000;

     border-radius: 2px;

     display: block;

     height: 500px;

     margin: auto;

     position: relative;

     width: 80%;

     box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);

     transition: all 0.3s cubic-bezier(.25,.8,.25,1);

     padding: 30px auto;

     overflow: hidden;

     }

    }

我正在尝试将 Card div 设置为“margin: auto;” 在移动设备上但它不起作用。我还添加了 head 标签。请建议我。现在我正在 Chrome 和 Firefox 开发者控制台上测试响应式。有没有更好的测试方法?谢谢。


慕沐林林
浏览 50回答 3
3回答

慕勒3428872

将其添加到您的 head 标签中&nbsp;<meta&nbsp;name="viewport"&nbsp;content="width=device-width,&nbsp;initial-scale=1">还将@media(最大宽度:700)更改为@media(最大宽度:700px)

波斯汪

@media(max-width: 700)用。。。来代替@media screen and (max-width: 700)

至尊宝的传说

媒体查询看起来不错,您可以切换到@media screen 和(最大宽度:680px)在 Chrome 中,当您检查元素时,“切换设备工具栏”选项可用。您可以查看应用程序在不同设备上的外观以及应用程序是否响应。div.logo-inline-div {&nbsp; text-align: center;&nbsp; margin: auto;&nbsp; padding: 16px 24px 10px 10px;&nbsp; border-radius: 8px;&nbsp; background-color: white;&nbsp; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.15), 0 2px 3px 0 rgba(0, 0, 0, 0.1);}.row {&nbsp; width: 100%;&nbsp; display: flex;&nbsp; flex-wrap: wrap;&nbsp; align-items: center;}.col-1 {&nbsp; width: 12.4%;}img {&nbsp; width: 100px;&nbsp; height: 200px;&nbsp; object-fit: cover;}.Card {&nbsp; background: #fff;&nbsp; border-radius: 2px;&nbsp; display: inline-block;&nbsp; height: 500px;&nbsp; margin: 1rem;&nbsp; position: relative;&nbsp; width: 300px;&nbsp; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);&nbsp; transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);&nbsp; padding: 30px auto;&nbsp; overflow: hidden;&nbsp; justify-content: center;}.FestivalDetails {&nbsp; position: absolute;&nbsp; width: auto;&nbsp; max-width: 500px;&nbsp; height: auto;&nbsp; display: block;&nbsp; padding: 20px 20px;}.FestivalPhoto {&nbsp; height: 200px;&nbsp; width: auto;&nbsp; position: relative;&nbsp; top: 0;&nbsp; display: block;&nbsp; overflow: hidden;&nbsp; margin: auto;&nbsp; background-size: initial;}/*@media screen and (max-width: 680px) {*/@media (max-width: 700) {&nbsp; .col-1 {&nbsp; &nbsp; width: 100%;&nbsp; &nbsp; margin: 0;&nbsp; }&nbsp; div.logo-inline-div {&nbsp; &nbsp; padding: 5px;&nbsp; &nbsp; display: block;&nbsp; }&nbsp; .Card {&nbsp; &nbsp; background: #000;&nbsp; &nbsp; border-radius: 2px;&nbsp; &nbsp; display: block;&nbsp; &nbsp; height: 500px;&nbsp; &nbsp; position: relative;&nbsp; &nbsp; width: 80%;&nbsp; &nbsp; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);&nbsp; &nbsp; transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);&nbsp; &nbsp; padding: 30px auto;&nbsp; &nbsp; overflow: hidden;&nbsp; }}<!DOCTYPE html><html>&nbsp; <head>&nbsp; &nbsp; <meta charset="UTF-8" />&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0" />&nbsp; &nbsp; <link rel="stylesheet" href="EightImages.css" />&nbsp; &nbsp; <link rel="images" href="images" />&nbsp; &nbsp; <title>Document</title>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <div class="container">&nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-12 col-1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="logo-inline-div">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="images/sunset1.png" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Build an Empire</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-12 col-1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="logo-inline-div">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="images/sunset1.png" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Build an Empire</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-12 col-1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="logo-inline-div">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="images/sunset1.png" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Build an Empire</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-12 col-1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="logo-inline-div">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="images/sunset1.png" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Build an Empire</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-12 col-1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="logo-inline-div">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="images/sunset1.png" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Build an Empire</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-12 col-1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="logo-inline-div">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="images/sunset1.png" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Build an Empire</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-12 col-1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="logo-inline-div">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="images/sunset1.png" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Build an Empire</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-12 col-1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="logo-inline-div">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="images/sunset1.png" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Build an Empire</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="Card">&nbsp; &nbsp; &nbsp; <div class="FestivalPhoto">&nbsp; &nbsp; &nbsp; &nbsp; <img src="images/sunset1.png" width="300px" alt="MusicFestivalImage" />&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="FestivalDetails">Hello World</div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="Card">&nbsp; &nbsp; &nbsp; <div class="FestivalPhoto">&nbsp; &nbsp; &nbsp; &nbsp; <img src="images/sunset1.png" width="300px" alt="MusicFestivalImage" />&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="FestivalDetails">Hello World</div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="Card">&nbsp; &nbsp; &nbsp; <div class="FestivalPhoto">&nbsp; &nbsp; &nbsp; &nbsp; <img src="images/sunset1.png" width="300px" alt="MusicFestivalImage" />&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="FestivalDetails">Hello World</div>&nbsp; &nbsp; </div>&nbsp; </body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5