Codier
Codier

Reputation: 2160

Using the id selector with other selectors

I want to know, what does the following CSS mean?

.twoCol #sidebar

Isn't #sidebar alone enough? What does this line of CSS actually select?

Upvotes: 4

Views: 94

Answers (3)

No Results Found
No Results Found

Reputation: 102745

Demian and thirtydot are largely correct, but this does have a conditional use:

If you have several templates and sometimes #sidebar occurs in .twoCol, but sometimes it occurs somewhere else - you can target that specific template without including another CSS file.

In this case, it makes more sense to assign a class to the <html>, <body>, or template wrapper, but I just thought I'd point out that it's not strictly useless.

Upvotes: 1

thirtydot
thirtydot

Reputation: 228162

Isn't #sidebar alone is enough?

Yes. An id should be unique. (and it will be, unless you're dealing with poor quality HTML)

The .twoCol prefix is not optimal because it's adding redundant information.

This is comically demonstrated here: http://www.css-101.org/descendant-selector/go_fetch_yourself.php

A more complete article, which isn't specific to this case, but a good and relevant read nonetheless:

http://css-tricks.com/efficiently-rendering-css/

Upvotes: 1

Demian Brecht
Demian Brecht

Reputation: 21368

This is accessing the element with the id sidebar within any element that uses the twoCol class.

#sidebar is enough on its own really, as it's an ID, but this is a little more semantically correct.

Upvotes: 3

Related Questions