Why can't I override display property applied

2019-07-31 18:54发布

问题:

I want to hide all the elements in some screen resolution and just show the wanted element to be visible:

For instance:

*{
    display: none;
}
#block{
    display: block !important;
}

But this won't override the display property anymore. demo

回答1:

* targets all elements within the document, including html and body as well. That's why the content is still hidden - verify that.

If you want to select all elements within the <body> you should do that as follows:

body * {
    display: none;
}

#block {
    display: block;
}
<div id="block">block</div>



回答2:

Because body and html are included in the universal selector *, which has the display: none; rule.

http://jsfiddle.net/nk8np9vo/6/



回答3:

If you open the target frame with your favourite DOM inspector you'll see that <body> remains hidden: