Is there a reason as to why you would declare a HTML selector with an ID in CSS?
Edited version of the original
How is declaring div#test
different to declaring it as just #test
? Also can I declare both of the in the same stylesheet i.e.
#div test {
}
#test {
}
Original question
If I were to declare the following style div#test
how is that different to referencin开发者_运维技巧g a div
element with an ID test
e.g. <div id="test">content</div>
EDIT
To clarify what I mean is that how is div#test
different to just #test
and then referencing it in the div
element.
it makes a sense if you have global css for every page and on one page you add #test to div element and on some other page you add it for p. in such case, even though p has id #test, css rules will not be applied to it if div#test is used instead of #test.
however probably you should consider renaming such conflicting elements, but not always it is possible (for example you are not the only one who works with html and css or some external plugins, whatever). of course it is not good protection, but still, could make sense in some specific cases.
as id must be unique nothing. although in an external stylesheet it may allow readers to know the type of element this applies to.
If you use div#test in your css, it will look for any ID with the name test, that belongs to a div, as if you just did #test, it could be applied to any other element. It also add's specificity to your selection, and could potentially help those reviewing your code know exactly what you are referring to.
Example.
#test { margin-top: 10px; }
<ul id="test">
<li><a href="#">LinkyLink</a></li>
</ul>
div#test { background: blue; }
<div id="test">
<p>This is jargan!</p>
</div>
Hope this helps.
精彩评论