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

前端 未结 5 828
长发绾君心
长发绾君心 2020-12-09 11:35

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 regar

5条回答
  •  不知归路
    2020-12-09 12:01

    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....
    }
    

提交回复
热议问题