开发者

Using CSS, does it make sense to select with multiple ID attributes "#id1 #id2 {..." [duplicate]

开发者 https://www.devze.com 2023-03-23 17:27 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: CSS inner id selectors I encountered the following CSS selector and I feel that it doesn\'t quite make sens
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

CSS inner id selectors

I encountered the following CSS selector and I feel that it doesn't quite make sense.

#id1 #id2 .class1 {
 color: #fff;
}

It seems to me that the CSS selector would match first to the element #id1, and then to the element #id2, and then to elements beneath and including #id2 that have a class attribute set to class1.

My hunch is that it is valid CSS, but also that it is unnecessary to specify #id1, or that if it is meant only to match a document when #id1 has a child #id2, but not to match an #id2 without a parent #id1.

Since #id1 has a specificity of 0,1,0,0 and #id2 has a specificity of 0,1,0,0, and .class1 has a specificity of 0,0,1,0, you get 0,2,1,0. But is this necessary or useful? It seems like a more effic开发者_如何学运维ient method would be to create two rules and select by each id separately.

I can't seem to find any other examples of a CSS selector that looks like #id1 #id2 ....

Can anyone comment on this and help give me a sanity check?


This selects elements with the class class1 and an ancestor (including parents) with id id2 which itself has an ancestor with id1. So your interpretation was close, but this won't select an element with id2 and class1 unless it is contained within another id2.

If you wanted it to be including id2 you would have to use #id1 #id2.class1, #id1 #id2 .class1 as the selector.

If you just did #id2 .class1 then you would select elements with class class1 inside an element with id2 regardless of whether or not the id2 element is in an id1.

So the two selectors have different meanings.

0

精彩评论

暂无评论...
验证码 换一张
取 消