媒体查询相互覆盖-奇怪的情况

时间:2019-02-05 04:15:01

标签: css media-queries

我的iPhoneX媒体查询将覆盖我所有其他媒体查询。我所有其他媒体查询都使用最小/最大宽度来接受其coirreect屏幕尺寸。但是由于某些原因,IphoneX使所有其他媒体查询无效。有人可以帮忙吗?

/* images */

img{
    max-width: 250px;
    width: 100%;
  }

  /* Header image */

  .headerImg{
    max-width: 250px;
    margin: 0 auto;
    padding-top: 10px;
  }

  /* Title */

  .title{
    max-width: 250px;
    margin: 0 auto;
    padding-top: 20px;
  }

  /* voting buttons */

  .vote{
    position:relative;
    text-align: center;
    padding-top: 20px;
  }

  #vote1 .dot{
    width: 13px;
    height: 13px;
    background-color: white;
    border-radius: 50%;
    position: absolute;
    top: 28px;
    left: 15px;
    border: 2px solid white;
  }

  #vote2 .dot{
    width: 13px;
    height: 13px;
    background-color: white;
    border-radius: 50%;
    position: absolute;
    top: 30px;
    left: 24px;
    border: 2px solid white;
  }

  /* GLOBAL CLASSES */

  .flexCenter{
     display: flex;
     justify-content: center;
     align-items: center;
  }

  .paddingTopBottom{
     padding: 10px 0;
  }

<!-- iPhone4/4s -->
<link href="https://pub.s6.exacttarget.com/vaf4d25yesa" rel="stylesheet" media="screen and (min-device-width: 320px) and (max-device-width: 480px)">  
<!-- iPhone5s/5c/5se -->
<link href="https://pub.s6.exacttarget.com/asgp1bospyf" rel="stylesheet" media="screen and (min-device-width: 320px) and (max-device-width: 568px)">  
<!-- iPhone6/6s/7/8 -->
<link href="https://pub.s6.exacttarget.com/c3c1pf4b2me" rel="stylesheet" media="screen and (min-device-width: 375px) and (max-device-width: 667px)">
<!-- iPhone6+/7+/8+ -->
<link href="https://pub.s6.exacttarget.com/qjhe5zmjrys" rel="stylesheet" media="screen and (min-device-width: 414px) and (max-device-width: 736px)">
<!-- iPhoneX -->
<link href="https://pub.s6.exacttarget.com/ioeyyyvvogl" rel="stylesheet" media="screen and (min-device-width: 375px) and (max-device-width: 812px)">

这是链接的媒体查询。

/* Media queries for mobile sizes */

/* ----------- iPhone X ----------- */

/* Portrait and Landscape */
@media only screen and (min-device-width: 375px) and (max-device-width: 812px) { 
    .headerImg{max-width: 280px !important;padding-top: 50px !important}
    .title{max-width: 280px !important;padding-bottom: 50px !important;padding-top: 25px !important;}
    .deal{max-width: 115px !important;}
    .vote {padding-top: 0 !important;}
    #vote1 .dot {top: 11px !important;left: 15px !important;}
    #vote2 .dot {top: 11px !important;left: 9px !important;}
    .smartcapture-submit-button-wrapper{bottom: 120px !important;}
    .sc-button{height: 45px !important; width: 160px !important;}
}

/* ----------- iPhone 6+, 7+ and 8+ ----------- */

/* Portrait and Landscape */
@media only screen and (min-device-width: 414px) and (max-device-width: 736px) { 
    .headerImg{max-width: 280px !important;padding-top: 20px !important}
    .title{max-width: 280px !important;padding-bottom: 40px !important;}
    .deal{max-width: 115px !important;}
    .vote {padding-top: 0 !important;}
    #vote1 .dot {top: 13px !important;left: 17px !important;}
    #vote2 .dot {top: 13px !important;left: 11px !important;}
    .smartcapture-submit-button-wrapper{bottom: 80px !important;}
    .sc-button{height: 45px !important; width: 160px !important;}
}

/* ----------- iPhone 6, 6S, 7 and 8 ----------- */

/* Portrait and Landscape */
@media only screen and (min-device-width: 375px) and (max-device-width: 667px) { 
    .headerImg{max-width: 280px !important;padding-top: 20px !important}
    .title{max-width: 280px !important;padding: 10px 0 !important;}
    .deal{max-width: 115px !important;}
    .vote {padding-top: 0 !important;}
    #vote1 .dot {top: 11px !important;left: 15px !important;}
    #vote2 .dot {top: 11px !important;left: 9px !important;}
    .smartcapture-submit-button-wrapper{bottom: 80px !important;}
    .sc-button{height: 45px !important; width: 160px !important;}
}

/* ----------- iPhone 5, 5S, 5C and 5SE ----------- */

/* Portrait and Landscape */
@media only screen and (min-device-width: 320px) and (max-device-width: 568px) {
    .headerImg{max-width: 200px !important;padding-top: 20px !important}
    .title{max-width: 200px !important;padding: 10px 0 !important;}
    .deal{max-width: 100px !important;}
    .vote {padding-top: 0 !important;}
    #vote1 .dot {top: 7px !important;left: 15px !important;}
    #vote2 .dot {top: 7px !important;left: 9px !important;}
    .smartcapture-submit-button-wrapper{bottom: 60px !important;}
}

/* ----------- iPhone 4 and 4S ----------- */

@media only screen and (min-device-width: 320px) and (max-device-width: 480px) {
    .headerImg{max-width: 170px !important;}
    .title{max-width: 160px !important;padding: 10px 0 !important;}
    .deal{max-width: 110px !important;}
    .vote {padding-top: 0 !important;}
    #vote1 .dot {top: 7px !important;left: 15px !important;}
    #vote2 .dot {top: 7px !important;left: 9px !important;}
    .smartcapture-submit-button-wrapper{bottom: 60px !important;}
}

/* ----------- Galaxy S4, S5 and Note 3 ----------- */

/* Portrait and Landscape */
@media screen and (device-width: 320px) and (device-height: 640px) and {
    .headerImg{max-width: 240px !important;padding-top: 50px !important}
    .title{max-width: 240px !important;padding-bottom: 40px !important;padding-top: 15px !important;}
    .deal{max-width: 115px !important;}
    .vote {padding-top: 0 !important;}
    #vote1 .dot {top: 9px !important;left: 15px !important;width: 10px !important;; height: 10px !important;}
    #vote2 .dot {top: 9px !important;left: 9px !important;width: 10px !important;; height: 10px !important;}
    .smartcapture-submit-button-wrapper{bottom: 90px !important;}
    .sc-button{height: 45px !important; width: 160px !important;}
}

当我从媒体查询中删除!important时,不会选择任何内容,而是仅使用默认样式。

任何帮助都将不胜感激。

谢谢

2 个答案:

答案 0 :(得分:2)

Erayner,

媒体查询可能很棘手...

如果您在iPhone 6,7&8媒体查询语句下方有iPhone 4媒体查询语句,则它(实际上)将覆盖iPhone 6,7&8,因为它在下面。从头到尾读取CSS文件。例如:

<style>
.className {
  width: 50%;
}
.className {
  width: 100%;
}
</style>

宽度为.className的第二个100%将自最后一个开始(从上到下读取)。

它之所以被覆盖是另一个原因,因为iPhone 4媒体查询的范围是320像素到480像素,而iPhone 6,7&8的查询范围是在iPhone 4的范围内(375像素到667像素)。 375像素到480像素属于iPhone 4的范围...并且由于它低于iPhone 6,7&8的代码,因此优先级...

此外,不确定为什么会有如此多的!important呼叫-如果您提供整个CSS文件来了解ID和类的顺序,则可以帮助我们更好地了解您的特定情况。

您是否尝试过将iPhone 4的媒体查询代码放在iPhone 6,7&8之上?如果是这样,您是否遇到相同的问题?

答案 1 :(得分:0)

只需将iPhone 4 @media规则放在iPhone 6规则之前。因此,当宽度为375px或更高时,iPhone 6将覆盖iPhone 4。

当宽度在320像素到374像素之间时,将应用以下规则。

@media only screen and (min-device-width: 320px) and (max-device-width: 480px) and (-webkit-min-device-pixel-ratio: 2) {...}

当宽度在375px至667px之间时,以下规则将被覆盖;

@media only screen and (min-device-width: 375px) and (max-device-width: 667px) and (-webkit-min-device-pixel-ratio: 2){...}