简体   繁体   中英

CSS media query not working when resizing browser

I'm trying to use a media query to make a div responsive on a site (using IE11).

This is my CSS:

@media screen and (max-width: 400px) { // Tried with "only screen" also

  .divStyle {
      background-image:none;
      background-color:red;
      background-position:right;
      color:#fff!important;
      height:140px;
      position:relative;
      top:-6px;

    }

}

.divStyle {
  background-image:url('/images/image.jpg');
  background-position:right;
  color:#fff!important;
  padding:20px;
  height:190px;
  position:relative;
  top:-6px;

}

I've also added the metatag in the Head section.

<meta name="viewport" content="width=device-width, initial-scale=1" /> 

But it still doesn't respond when I'm resizing the window.

In your case the rule without the media query follows after the one with the media query. So it overwrites the rule.

Change their order to this.

.divStyle {
  background-image:url('/images/image.jpg');
  background-position:right;
  color:#fff!important;
  padding:20px;
  height:190px;
  position:relative;
  top:-6px;

} 

@media screen and (max-width: 400px) { 
  .divStyle {
      background-image:none;
      background-color:red;
      background-position:right;
      color:#fff!important;
      height:140px;
      position:relative;
      top:-6px;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM