css 没有介质查询无法工作!重要

dtcbnfnu  于 2023-05-08  发布在  其他
关注(0)|答案(8)|浏览(336)

我的媒体查询不会覆盖常规CSS,除非我一直使用!important。什么错误?在这里你可以看到带有!important s的CSS。如果我删除它们,样式会回到常规的CSS,即使屏幕小于850px。你能帮帮忙吗?

@media all and (min-width: 200px) and (max-width: 850px) {
        html body {
        position: absolute;
        height: 100%;
        width: 100% !important;
        margin: 0 auto;
        text-align:center;
        }
        #header {
        background-color: white;
        position: fixed;
        top: 0px;
        box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
        width: 95%;
        height: 120px;
        z-index:9999;
        }
        #header-inner{
        width: 100% !important;
        float: right;
        }
        div#title   {
        margin: 0px !important;
        position: absolute; 
        width: 100% !important;
        height: 70px;
        background: url('mobillogo.png'); 
        background-repeat: no-repeat;
        background-size:  auto 70px;

        }
        .wrapper {
        width: 100% !important;
        overflow:auto;
        margin-left: auto;
        margin-right: auto;
        }
        #first {
        padding-top: 150px;
        padding-bottom: 30px;
        overflow:auto;
        width: 95%;
        background-color: #f7fdff;
        height: ;
        }
        #second {
        margin-top: 65px;
        overflow: auto;
        width: 95% !important;
        margin-bottom: 65px;
        background-color: white;

        }
        #third {
        overflow:auto;
        width: 95% !important;
        background-color: #f3efef;
        padding-bottom: 4em;
        }
        #footer
        {
        height: 115px;
        width:100% !important;
        box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23);
        background-color: white;
        padding-top: 1em;
        padding-bottom: 0.5em;
        }
        #footbox {
        height: 70px;
        width: 100% !important;
        margin-top: 0px;
        margin-bottom: 10px;
        margin-left: auto;
        margin-right: auto;
        }
        #contains {
        width: 100% !important;
        margin-top: 50px;

        }
        h1 {
        float: left;
        width: 100% !important;
        margin-top: 2em;
        font-family: Jersey;
        font-size: 3.5em;
        text-align: center; 
        color: #f44336;         
        }

        #logo {
        margin-top:70px !important;
        width: 100% !important;
        height: 70px;
        }
        div#logo img {
        position: static !important;
        height: 13vw !important;

        }
        #footbox {
        height: 70px;
        width: 95%;
        margin-top: 0px;
        margin-bottom: 10px;
        margin-left: auto;
        margin-right: auto;
        }
        #checklist {
        overflow: auto;
        position: relative;
        float: left;
        width: 100%  !important;
        font-family: 'Open Sans', sans-serif;
        padding-bottom: 8vh;
        padding-top: 15px;
        text-align:left;

        }
        #short {
            width: 100%  !important;
            color: black;
            font-weight: bold;
            font-size: 1.3em;
            margin: 0px !important;
        }
        #form {
        overflow: auto;
        border-radius: 5px;
        padding: 0px !important;
        position: relative;
        width: 100% !important;
        display: inline-block;
        -webkit-transition:background 0.3s;
        -moz-transition:background 0.3s;
        -o-transition:background 0.3s;
        transition:background 0.3s;
        transition-timing-function: ease-in-out;
        }
        ul.inline {
        width: 100% !important;
        list-style-type: none;
        columns: 1 !important;
        -webkit-columns: 1 !important;
        -moz-columns: 1 !important;
        list-style-position: inside;
        padding: 0px;
        margin: 0px;
    }
    #fulltext {
    width: 90%;
    padding-right: 30px;
    }
    .text {
    width: 75% !important;
    }

}
html body {
position: absolute;
height: 100%;
width: 100%;
margin: 0 auto;
 text-align:center;

}
@font-face { 
font-family: Jersey; src: url('Jersey M54.ttf'); 
} 

p {
font-family: 'Open Sans', sans-serif;
 text-align:left;

}
.lightup {
background-color: #ffc107;
}
button {
    float: right;
    margin-right: 10px;
    font-size: 1.2em;
    background: #5fbd5f;
    color: white;
    padding: 5px;
    font-family: 'Open Sans', sans-serif;
    color: white;
    padding: 5px;
  padding-left: 10px;
  padding-right: 10px;
  border: 0;
 outline: none;
box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);

}
button:hover {
cursor: pointer; 
cursor: hand;
}
 button:active{
   border: 0;
outline: none;
        box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);ű
    }
a, u {
text-decoration: none;
}
#header-inner{
width: 1100px;
    float: right;

}
#header {
background-color: white;
position: fixed;
top: 0px;
box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
width: 100%;
height: 120px;
z-index:9999;
}
.wrapper {
overflow:auto;
width: 1100px;
margin-left: auto;
margin-right: auto;
}
#first {

padding-top: 150px;
padding-bottom: 30px;
overflow:auto;
width: 100%;
background-color: #f7fdff;
height: ;
}
#second {
margin-top: 65px;
overflow: auto;
width: 100%;
margin-bottom: 65px;
background-color: white;

}
#third
{
overflow:auto;
width: 100%;
background-color: #f3efef;
padding-bottom: 4em;
}
#footer
{
height: 115px;

width:100%;
box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23);
background-color: white;
padding-top: 1em;
padding-bottom: 0.5em;
}
#footbox {
height: 70px;
width: 1100px;
margin-top: 0px;
margin-bottom: 10px;
margin-left: auto;
margin-right: auto;
}
#contains {
width: 100%;
margin-top: 50px;

}
#toTop {
    clear:both;
    width: 600px;
    box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
    font-family: 'Bree Serif', serif;
    color: white;
    border-radius: 5px;
    background: #f44336;
    padding-left: 15px;
    padding-right: 15px;
    font-size: 70px;
}
#logo {
float:left;
margin-top:1em;
}
h1 {
    position: relative;
 vertical-align: text-top;
    margin-top: -12px;
font-family: Jersey;
    font-size: 3.5em;
    text-align: right; 
    color: #f44336;
}
h4 {
margin-top: 0px;
margin-bottom: 0;
font-family: Jersey;
font-size: 2em;
text-align: center; 
color: white;
}
h5 {
position:relative;
float:left;
font-family: 'Open Sans', sans-serif;
color: #f44336;
font-size: 37px;    
padding:5px;
width:250px;
margin-top: -15px;
margin-bottom: 0px;
margin-left: auto;
margin-right: auto;
text-align: right;

}
#purple {
    text-align: center;
      vertical-align: middle;
    margin-top: 10px;
position:relative;
    padding-right: 15px;
}
.caltxt {
    padding-top: 0px;
    padding-left: 15px;
    font-size: 1em;
    color: white;
}
#calendar {
        margin-bottom: 5em;
margin-left: 80px;
    float: left;
    width: 600px;
    height: 400px;
}
#sept {
    float: left;
    position: relative;
    width: 25%;
    height: 100%;
    background-color: #4caf50;  
}
#oct {
    float: left;
    position: relative;
    width: 25%;
    height: 75%;
    background-color: #f44336;  
}
#nov {
    float: left;
    position: relative;
    width: 25%;
    height: 75%;
    background-color: #266fea;  
}
#dec {
    float: left;
    position: relative;
    width: 25%;
    height: 75%;
    background-color: #ffc107;  
}
#plus {
    float: left;
    position: relative;
    width: 75%;
    height: 25%;
    background-color: #9c27b0;  
    z-index: 0;
}
.calendar {
     -webkit-transition: all 250ms;
      -moz-transition: all 250ms;
      transition: all 250ms;
}
.calendar:hover {
    z-index: 9998;

        box-shadow: 0 19px 38px rgba(0,0,0,0.30), 0 15px 12px rgba(0,0,0,0.22);
         -webkit-transform: translateY(-5px);
         -moz-transform: translateY(-5px);
         transform: translateY(-5px);
}
div#title   {
    margin-top: 50px;
    position: absolute; 
    width: 1100px;
    height: 70px;
      background: url('weblogo.png'); 
      background-repeat: no-repeat;
      background-position: 0px;
      background-size:  auto 70px;

}
#checklist {
    overflow: auto;
    position: relative;
    float: left;
    width: 750px;

    font-family: 'Open Sans', sans-serif;
    padding-bottom: 8vh;
    padding-top: 15px;
 text-align:left;

}
#short {
    width: 750px;
    color: black;
    font-weight: bold;
    font-size: 1.3em;
}
#form {
overflow: auto;
border-radius: 5px;
padding: 15px;
    position: relative;
    width: 260px;
display: inline-block;
-webkit-transition:background 0.3s;
-moz-transition:background 0.3s;
-o-transition:background 0.3s;
transition:background 0.3s;
transition-timing-function: ease-in-out;
}
.text {
width: 90%; 
    text-align: left;
    padding: 5px;
    margin-top: 0px;
    margin-bottom: 5px;
    font-family: 'Open Sans', sans-serif;
    border: 1px solid grey;
    border-radius: 5px;

}
#fulltext {
    width: 90%;
    padding-right: 30px;

}
#name {
    background: url(profile.png) no-repeat scroll 5px 5px;
    background-color: white;
    background-size: 20px;
    margin-top: auto;
    margin-bottom: auto;
    padding-left:30px;
    margin-bottom: 10px;
}
#email {
    background: url(message.png) no-repeat scroll 5px 8px;
    background-color: white;
    background-size: 20px;
    margin-top: auto;
    margin-bottom: auto;
    padding-left:30px;
    margin-bottom: 10px;

}
#university {
    background: url(university.png) no-repeat scroll 5px 4px;
    background-color: white;
    background-size: 20px;
    margin-top: auto;
    margin-bottom: auto;
    padding-left:30px;
    margin-bottom: 10px;

}
h3 {
    font-family: 'Bree Serif', serif;
    width: 130px;
    color: white;
    border-radius: 5px;
    background: #f44336;
    padding: 5px;

}
#textbox{
width: 90%;
height: 5em;
}

#textbox::-webkit-input-placeholder{
    color:transparent;
}

#textbox::-webkit-input-placeholder::before {
    color:#666;
    content:"program?*";
}

h2 {
font-weight: bold;  
font-size: 1em;
font-family: 'Open Sans', sans-serif;
}

ul.inline {
    list-style-type: none;
    columns: 2;
    -webkit-columns: 2;
    -moz-columns: 2;
    list-style-position: inside;
    padding: 0px;
    margin: 0px;
}

ul.inline li {
    padding: 5px;
    background-image: url(check.png);
    background-repeat: no-repeat;
    background-size: 25px;
    background-position: 0px center; 
    padding-left: 25px; 
}
#fun {
    float: left;
padding-bottom: 50px;
}
#creativity {
    padding-bottom: 80px;

}
#community {
    float: left;
    margin-left: 75px;

}
#innovation {
    float: left;
    margin: 30px;

}
#challenge {
    float: left;
    margin-left: 100px;
    margin-top: -25px;
    margin-bottom: 5em;
    height: 220px;
}
#csanad {
    height: 130px;
    float: left;
    margin-left: 5%;
    margin-top: 4%;
padding-bottom: 80px;

}

#adam {
    height: 130px;
    float: left;
    margin-left: 0%;
    margin-top: 70px;

}
#adamtxt {
    font-size: 0.9em;
    width: 61%;
    float:left;
    margin-top: 72px;
    margin-left: 25px;
}
#rapper {
width:400px;
float: left;

}
#csanadtxt {
    font-size: 0.9em;

    width: 32%;
    float:left;
    margin-top: 4%;
    margin-left: 25px;
}
#noura {
    height: 130px;
    float: left;
    margin-left: 15px;
    margin-top: 75px;

}
#nouratxt {
    font-size: 0.9em;

    width: 23%;
    float:left;
    margin-top: 80px;
    margin-left: 25px;
}
#container {

}
div#logo img {
position: absolute;
height: 25px;
}
#menu{
    position: relative;
    padding: 0px;
float: right;
}
ul#menu  li {
  list-style: none;
  display: inline-block;
  font-size: 0.95em;
    height: 1vw;

}
ul#menu li a {
  padding: 5px;
  padding-left: 0.45vw;
  padding-right: 0.45vw;
  color: #ff4412;
  font-family: 'Open Sans', sans-serif;
  outline: none;
  text-transform: uppercase;
  border-radius: 5px;
}
div#container div#header div#header-inner ul#menu li a:focus {
  text-decoration: none;
}
div#container div#header div#header-inner ul#menu li a:hover {
  background: #ff4412;
  color: white;
  text-decoration: none;
}
div#container div#header div#header-inner ul#menu li.highlight a {
  color: white;
  border-radius: 5px;
  background: #ff4412;
}
div#container div#header div#header-inner ul#menu li.highlight:hover {
  opacity: 0.9;
}
w51jfk4q

w51jfk4q1#

说明

浏览器如何在没有!important的情况下查看CSS:

for screen 200px < x < 800px do this {
  bla bla bla
}
but... wait a second.. forget about it, do this for all screens {
  bla bla bla
}

当你添加!important时,浏览器会像这样处理:

for screen 200px < x < 800px do this {
  bla bla bla
  !do not listen to me if I will ever give you any other instructions
}
but... hey dude, want any instructions? Can you do this for all screens? {
  bla bla bla
}

如果你把@media块放在文件的末尾,并删除所有的!important,它看起来像这样:

for all screens do this {
  bla bla bla
}
but for screens 200px < x < 800px do this {
  bla bla bla
}

Demo

以全页模式打开此代码段并尝试更改浏览器的窗口大小

@media (max-width: 800px) {
    .bad {
        background-color: green;
    }
}

.bad, .good {
    width: 100px;
    height: 100px;
    background-color: firebrick;
}

@media (max-width: 800px) {
    .good {
        background-color: green;
    }
}
<div class="good"></div>
<div class="bad"></div>
bcs8qyzn

bcs8qyzn2#

颠倒顺序,稍后再进行媒体查询。它似乎没有增加任何选择器权重,所以后者优先。
所以你会有(剪下):

html body {
position: absolute;
height: 100%;
width: 100% !important;
margin: 0 auto;
text-align:center;
}
@media all and (min-width: 200px) and (max-width: 850px) {
    html body {
    position: absolute;
    height: 100%;
    width: 100% !important;
    margin: 0 auto;
    text-align:center;
    }
}

例如,请参阅CSS specificity on MDN,它not@media-queries列为递增权重。

ghhkc1vu

ghhkc1vu3#

在我的情况下,我的媒体查询是在初始SCSS之后添加的,并且它们仍然被覆盖。
我发现这是因为初始样式和媒体查询之间的嵌套不匹配!
我有:

.icon-tray {
  // stuff
    .icon-tray-icon {
      // stuff
        div {
          // stuff
        }
    }
}

然后,我的媒体查询有:

// Medium devices (tablets, 768px and up)
@media (min-width: 768px) {
    .icon-tray {
      // stuff
    }
    .icon-tray-icon {
      // stuff
        div {
          // stuff
        }
    }
}

所以我把它改成了这个,它起作用了:

@media (min-width: 768px) {
        .icon-tray {
            // stuff
            .icon-tray-icon {
              // stuff
                div {
                  // stuff
                }
            }
        }
    }

希望它是明确的,并帮助别人!

dvtswwa3

dvtswwa34#

我有一个类似的问题,我试图将媒体查询分为另一个CSS文件,并将其导入到主CSS。在第一种方法中,媒体查询只有在我使用大量!重要宣言。然而,当我将@media查询移到我的主CSS的末尾时,它工作得很好。

  • 其他方面要注意的是在html中有:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
  • 看看@media是怎么写的:(例)
@media only screen and (max-width: 768px) {
  /* CSS styles */
}

@media only screen and (min-width: 768px) and (max-width: 1024px) {
  /* CSS styles */
}
l0oc07j2

l0oc07j25#

CSS样式优先顺序
1.“!重要”
1.优先级是最明确定义的规则。ex:1).class1 { .class2 { .class3 { color:绿色
2).class1 { .class3 { color:红色的
1.优先级按顺序排列(底部优先于顶部)

s4n0splo

s4n0splo6#

必须检查<head>标记。首先,包含主样式css文件,然后包含响应式css文件。
就像这样:

<link rel="stylesheet" type="text/css" href="mainstyle.css">
<link rel="stylesheet" type="text/css" href="reponsive.css">
e4yzc0pl

e4yzc0pl7#

如果有匹配的问题,请将responsive css放在style css之后。问题就解决了。

avwztpqn

avwztpqn8#

我也有同样的问题。我通过将媒体查询样式放在其他样式的末尾来解决它。

相关问题