I have a div with an ID:
<div id="main">
What's the correct (or difference) between
div#main {
and
#main {
Regards,
There is a great doco on using efficient CSS selectors, focus on rules with overly qualified selectors:
ID selectors are unique by definition. Including tag or class qualifiers just adds redundant information that needs to be evaluated needlessly.
Instead of just applying the style to an element with id main
, your selector will re-qualify the element by checking whether or not it's also a div
(in that order). To clarify: css selectors are evaluated right to left, unlike same selector syntax when used in jQuery etc.
Re pixelistik's suggestion that div#main
is more specific than #main
- yes, that is technically correct, however if you have to resort to this to raise a rule's specificity, chances are the structure of CSS you're working on is not as thought through as it should be.
#main
matches everything with ID 'main', whereas div#main
matches only <div>
elements with ID main.
Ideally, you should never have two elements with the same ID, so realistically the two don't make a difference, but there's probably performance related issues regarding whether specifying div
makes it find the result faster.
main
, the rendering engine will have to check whether or not it's also a div
. While the effort around this could be seen as negligible, I'd worry more about the structural impact and possibly unwanted specificity override.
#aaa .bbb
is evaluated by the rendering engine and when it is used to match DOM elements w/JS. In the first scenario, all elements matching .bbb
will be found first, and then re-qualified by looking up their parent (RTL). In the second scenario, an element matching #aaa
is (quickly) found and then its descendants are searched for a match against .bbb
. This is what I meant by saying css selectors are parsed RTL - two polarly different ways to solve the same problem. And I don't know why, I just go with the flow :)
#aaa
and .bbb
. Naturally, the key selector is .bbb
, so browsers will find that first and then check if it's in an #aaa
. But in the question, there's only one: div#main
. Most browsers do look up a #main
element and then check if it's a div
, but that has nothing to do with RTL parsing.
Commented
May 28, 2012 at 23:00
So difference is that:
When you write div#main
style will be only for <div>
element.
When you write #main
it can be used as style for <div>
, <span>
, <p>
, etc.
And what recommend is hard to say, every developer it has it different. So i using for example
span.<nameClass>
when is nested in <li>
for example.
#nav li span.href a {
...
}
I think it's used when you want that someone class with specific name can have only one element.
So when your write span#href
it will works only for <span id="href">Simply dummy text</span>
not for others. When you write #href
it will works for <span id="href">Simply dummy text</span>
or <a href="#" id="href">Link</a>
but both are correct when you also asking about this. Differences i wrote above.
id
could/should be applied to different elements. Even if this is not specified in the selector, you know what element you're targeting
#main
will target all of them. But, you know, nobody likes non-conforming HTML :)
Commented
May 28, 2012 at 22:41
id="main"
could be applied to either a div or a table etc., but still one id per document. I'm sure if same ID is applied to multiple elements on the page, CSS performance would be the least of your worries :)
Both are correct.
div#main
is more specific than #main
, which means that styles defined with the first selector will override the ones of the second.
Here's a good introduction to CSS specifity: http://htmldog.com/guides/cssadvanced/specificity/