Why do I have to put media queries at the bottom of the stylesheet?

不想你离开。 提交于 2020-01-10 14:08:30

问题


I am new learning responsive design. What I have noticed on my journey is that when I put media queries at the bottom of the stylesheet, everything works flawlessly in regards to breakpoints. If I put the media queries at the top of the stylesheet, nothing works, and only recently I found out that I need to add !important and max-DEVICE-width ( as opposed to max-width) to the css that is being changed.

Why is this? Why do the media queries work on both desktop and mobile when put at the bottom of the stylesheet.

Why is it that when I put media queries on the top of the stylesheet I need to add !important and also max-DEVICE-width in order for the breakpoints to work on desktop and mobile?


回答1:


Because css is read from top to bottom. The rule that is set last, is the one that will be executed.

Translating, it is like this:

@media (max-width: 600px) { //If my screen fits this size
    .text {
        color: red; //Paint it red
    }
}

.text {
    color: yellow; //Now, forget about everything and paint it yellow!
}

When you add !important is like saying:

@media (max-width: 600px) { //If my screen fits this size
    .text {
        color: red !important; //Paint it red, and don't change it ever!!!
    }
}

.text {
    color: yellow; //Ok, I'm not going to paint it yellow....
}



回答2:


CSS is read from top to bottom. Everything that is below some other css will overwrite what's on top of it. It is possible however to use !important at the end of a CSS parameter to make it overwrite everything else

body{
    background-color: black !important;
}

body{
    background-color: pink;
}

The background-color will be black. If you remove the !important, it will be pink.




回答3:


Media queries cascade with the rest of the stylesheet. You can intersperse media queries within your stylesheet, and so you can also cascade styles as needed.

For example:

.my-class {
    color: red;
}

.my-class--modifier {
    color: blue;
}

@media screen and (min-width: 760px) {
    .my-class--modifier {
        color: green;
    }
}

.some-other-class {
    width: 200px;
}

@media screen and (min-width: 760px) {
    .some-other-class {
        width: 700px;
        background-color: gray;
    }
    .some-other-class .my-class {
        border: 2px solid red;
        border-radius: 4pt;
    }
}

This works precisely due to CSS's cascading nature. You can organize media queries as required based on sections, individual selectors and more.




回答4:


Basically you are using media queries when you want to apply CSS styles depending on a device's general type (such as print vs. screen), specific characteristics (such as the width of the browser viewport, or environment (such as ambient light conditions).

When you started designing, you generally started doing it for one device of known specifications. So you design it according to you current device and then apply it for other screen sizes.

Hence the order goes like this: Make complete design --> Add the media query to fit for desired screen sizes at the bottom.



来源:https://stackoverflow.com/questions/24456981/why-do-i-have-to-put-media-queries-at-the-bottom-of-the-stylesheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!