I am curious about the differences between the following two methods for styling an element like an unordered list
Method 1.
#div_name ul {}
Method 2.
ul.div_name {}
Thanks.
| SitePoint Sponsor |
I am curious about the differences between the following two methods for styling an element like an unordered list
Method 1.
#div_name ul {}
Method 2.
ul.div_name {}
Thanks.



as far as i know, "." is to define a class, "#" is to define an ID

Haha, which should make Method 2
<ul class="ul_class_name"> and
ul.ul_class_name {}
There's also
ul#div_name {} which would select for a <ul id="div_name">


it's a little more involved that that, but only a little.
#div_name ul {}
would target a child of the id "div_name"
so it would look like this:
<div id="div_name">
<ul>... </ul>
</div>
Method 2.
ul.div_name {}
would target ONLY ULs that have the CLASS "div_name"
<ul>...</ul> <!-- would not be targeted-->
<ul class="x">...</ul> <!-- would not be targeted-->
<DIV class="div_name">...</ul> <!-- would not be targeted-->
<ul class="div_name">...</ul> <!-- would be targeted-->
in the example above, if you have merely used .div_name{} then you your rules would have applied to the div as well ( along with any other tags that had the class "div_name").
you could have this as well, ul#div_name{} and the same principle as above would apply.. only to classes instead of divs. It works and it is totally val;id, but leaves the question of WHY!?!?!? as an ID is unique on a page so you SHOULDN'T have to be worrying that you may have a <div id="div_name"> and a <UL id="div_name">. so the occasions in which it would make sense to have something like ul#div_name{} are quite rare.
Brilliant ideas, elegant execution.
Graphic Design, Art Direction, Copywriting and Web Design.

I was assuming that div_name was just a generic name the OP gave, and not actually the name given to a div.

Off Topic:
Btw, dresden_phoenix, jim butcher fan?
Bookmarks