0

I'm having an issue with a CSS media query where the general style is being applied after the media query despite being above the media query in the cascade.

Example:

HTML:

<div id="myblock1">
    <div>Block 1</div>
    <div id="myblock2">Block 2</div>
    <div>Block 3</div>
</div>​

CSS:

#myblock1 div {
    display:block;
    background-color:#F00;
}

@media (min-width:600px) {
    #myblock2 {
        display:none;
    }
}​

Live demo: jsFiddle

In theory, all 3 blocks should be visible in windows of 600px width or less, and when larger the middle block should disappear, but that is not the case. Any ideas on why the ID/media query is being applied before the general styling?

3 Answers 3

3

That's because #myblock1 div is more specific than #myblock2 (1 ID + 1 element vs 1 ID).

You'll either need to be more specific, or add !important on the rule you're trying to override.

  • Be more specific: #myblock1 #myblock2 { display: none; }
  • Use !important: #myblock2 { display: none !important; }

In my opinion, the best solution would be to make the outer container less specific, by giving it a class name, rather than an ID:

<div class="myblock1">
    <div>Block 1</div>
    <div id="myblock2">Block 2</div>
    <div>Block 3</div>
</div>​

Then, the following will work fine:

.myblock1 div {
    display:block;
    background-color:#F00;
}

@media (min-width:600px) {
    #myblock2 {
        display:none;
    }
}​
Sign up to request clarification or add additional context in comments.

4 Comments

I wonder if people are going to start a debate in the comments here over whether it's worse practice to use multiple IDs for the sake of specificity or to use !important instead.
Both of these options work just fine, but I feel like they aren't quite right. In my experience IDs have always been the most specific way of applying something, and if you have to use !important you are doing something wrong, but in this case it's necessary. Thanks for the help!
@BoltClock: I usually like to avoid !important. I think the proper solution in this case is to have the first container have a .className, and the second one (the more specific one) have an #id, then you don't have that problem (since #id > ∞.class)
@DevinRodriguez: See the comment above for a more proper solution.
0

The specificity of your selector isn't high enough, so it isn't overriding your previous rule. Try this one instead:

@media (min-width:600px) {
    #myblock1 #myblock2 {
        display: none;
    }
}​

Demo: http://jsfiddle.net/7aWpQ/2/

Comments

0

Add !important to display:none; than it's working.

#myblock1 div {
display:block;
background-color:#F00;
}

@media (min-width:600px) {
    #myblock2 {
        display:none!important;
    }
}​

Note: Because the media query is less specific than your main css. ie. #myblock1 #myblock2 { display:none; } should also work

3 Comments

Because the media query is less specific than your main css. ie. #myblock1 #myblock2 { display:none; } should also work
I already know that (I'm not the one who asked the question), but you should include that piece of information in your answer, rather than on a comment :)
It's not the media query (or the @media rule) that's less specific, it's the selector inside that rule.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.