views:

128

answers:

3

I am trying to identify all the <UL> that contain a menu list by defining the ID like this:

<ul id="menutop">
    <li><a href="#">item1</a></li>
    <li><a href="#">item2</a></li>
    <li><a href="#">item3</a></li>
</ul>
<ul id="menumain">
    <li><a href="#">item1</a></li>
    <li><a href="#">item2</a></li>
    <li><a href="#">item3</a></li>
</ul>

As per what I understand, I could use:

ul[id|='menu']>li>a {color:#f00;}

(<a> direct child of a <li> direct child of an <ul> that has its id starting with menu) But it doesn't work.

Searching a bit brought me this [question][1] which suggests that ID is an attribute and not a property so I don't get why it isn't working. What am I doing wrong?

Here's a link to the CSS2 Matching attributes and attribute values as per the W3 standards ( http://www.w3.org/TR/CSS2/selector.html#matching-attrs ).

+1  A: 

To ensure maximum compatibility, I would give each ul a class menu and use

ul.menu>li>a {color:#f00;}

note that the > selector is not supported by IE < 8.

Pekka
Hi Pekka,The thing is that I want to use as minimum code in the XHTML as possible. This is why I am trying to use the ID values as selectors - which should be possible as per the CSS specifications.Thanks for your alternative, I guess I'll need to stick to that.
XaviEsteve
I see your point. I don't have the table handy, but CSS 2.1 support is still crappy. Better stick to plain old CSS 2 for the moment.
Pekka
+1 for the > selector as I'd agree with this. Risky to use with current browsers in use.
KP
+1  A: 

According to the W3 documentation:

[att|=val] Represents an element with the att attribute, its value either being exactly "val" or beginning with "val" immediately followed by "-" (U+002D)

http://www.w3.org/TR/CSS2/selector.html#attribute-selectors

This means ul[id|='menu'] is looking for <ul id="menu-somevalue"/> or <ul id="menu" />

That's likely why it isn't working, because menutop and menumain don't match.

You could try renaming the IDs to menu-top and menu-main, or you could do:

<ul id="menutop" class="menu">
    <li><a href="#">item1</a></li>
    <li><a href="#">item2</a></li>
    <li><a href="#">item3</a></li>
</ul>
<ul id="menumain" class="menu">
    <li><a href="#">item1</a></li>
    <li><a href="#">item2</a></li>
    <li><a href="#">item3</a></li>
</ul>

and your css:

ul.menu li a {color: #f00;}

This would give you coloring as needed on all menu ul's, while still giving you the ability to have unique IDs for use with any scripting, etc. as needed.

Also, as Pekka mentioned, the > selector isn't widely supported so you may want to reconsider use of it for now...

KP
Good point with the `-`.
Pekka
Thanks Kevin! I've added the hyphen and it works like a charm. Also removed the `>` as per your suggestions. Thank you :-)
XaviEsteve
No problem :) I'd suggest testing the selector in multiple older browsers to be sure it works. If you're using Windows, you could try MultipleIE, which is a great IE testing tool http://tredosoft.com/Multiple_IE. It lets you run all of the older IE versions, including point releases. It would at least let you cover off IE6 if you're running 7 or 8. You could also try Adobe Browserlab (https://browserlab.adobe.com/index.html#) for most popular browser renders, or of course the ever-popular http://browsershots.org/
KP
I've tested it in some browsers, here are the results: WORKS: Firefox 3.5.7, IE 8.0.6, Chrome 3.0.195.38, Opera 10.00, Safari 4.0.3. DOESN'T WORK: IE 6.0.2900 SP2. (Everything tested under Windows XP Pro SP3). Kevin, I'm using the Internet Explorer Collection (http://finalbuilds.edskes.net/iecollection.htm) for Windows too. It's a bit unstable in my computer sometimes and I'm not sure if IE7 is really rendering it as it should. I'll give Multiple IE a try. Thanks!
XaviEsteve
A: 

Here is some detailed info about CSS selectors

.

Sarfraz
Nice and very detailed post Sarfraz! Thanks :)
XaviEsteve
@LuckyShot: You are welcome......
Sarfraz