Forum Moderators: not2easy

Message Too Old, No Replies

Embedded List

the inner gets the style of the outer ... but I don't want it to

         

le_gber

9:27 am on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi All,

I have a small CSS issue that I thought I share with you to try and get some help -

I've got a navigation that looks like this:

<div id="nav">
<ul>
<li><a>Home</a></li>
<li><a>Service</a></li>
<li><a>Products</a>
<ul>
<li><a>Prod 1</a></li>
<li><a>Prod 2</a></li>
</ul>
</li>
<li> ... </li>
</ul>
</div>

I've applied a style to the links to have a background image on the left and centered.
On hover, focus and active the link color changes and the image is swapped for the same in a different color.

I have also added the same style to the page link - i.e. if on home page the link looks as if you had your focus / mouse over it

So in my stylesheet I have (simplified)

#nav ul li a{
color: black;
background: black image
}

#nav ul li a:focus, #nav ul li a:hover, #nav ul li a:active, #nav ul li#selectedlink{
color: red;
background: red image
}

This works fine for all the pages except when on the main product one, because then the style 'red' is applied to the inner list element (Prod 1 and Prod 2).

Any idea how I could get round this?

Cheers

Leo

Span

9:43 am on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



#nav ul li ul {
/* style for nested list here */
}

#nav ul li ul li {
/* style for list item in nested list here */
}

#nav ul li ul li a {
/* style for link in list item in nested list here */
}

Hope that helps.

le_gber

12:12 pm on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Span

Yep I had done that but the stlye still 'cascades' on inner li

Leo

createErrorMsg

12:38 pm on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the stlye still 'cascades' on inner li

Span's selector code will allow you to target styles to the inner list without effecting the outer list, but styles from the outer list will still cascade down to the inner. You can use Span's code, however, to reset styles for the inner list back to default (or whatever setting you want) while leaving the outer list as is.

For instance, say you styled the link text color for the outer list to be red with a hover effect of removing the underline...

ul li a:link {
color:red;
}
ul li a:hover{
text-decoration:none;
}

This does what you want, but also makes the inner lists links behave the same way (because they, too, match the selector pattern of an A in a LI in a UL). If you want the inner lists to have the default settings of blue links with a persistent underline, you use Span's code like this...

ul li ul li a:link{
color:blue;
}
ul li ul li a:hover{
text-decoration:underline;
}

These styles will override the cascading styles from the first set and set the inner list back to normal while the outer list remains red and un-underlined on hover.

cEM

le_gber

2:45 pm on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



createErrorMsg,

thanks for that - although it didn't work either I did sort my problem by creating a new style and span-ning the li text, therefore instead of applying the style to all the parent and child it stayed with the parent.

Leo

createErrorMsg

3:37 pm on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it sorted, but I'm confused about why it didn't work. I'm posting a test page below that uses your code, plus the right decendant selectors to target just the inner list. To keep it simple, I left the text color black and altered only the background colors. On the page, the outer list has a yellow background that changes to blue on hover. The inner list has a red background that changes to green on hover. If you remove the last two style rules, the inner list reverts to the styling of the outer list, which it inherits. WIth the last two rules in place, their styles override the identical properties in the outer list's styling with styling of their own. The key is that you have to explicitly override anything you want different in the inner list, or the outer list's styles will cascade.

Feel free to ignore this, as you said you've solved the problem. I just get hung up on this stuff. ;)

cEM

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<style type="text/css">
#nav ul li a{
background: yellow;
}

#nav ul li a:focus, #nav ul li a:hover, #nav ul li a:active, #nav ul li#selectedlink{
background: blue;
}
#nav ul li ul li a{
background:red;
}
#nav ul li ul li a:focus, #nav ul li ul li a:hover, #nav ul li ul li a:active, #nav ul li ul li#selectedlink{
background:green;
}
</style>
</head>
<body>
<div id="nav">
<ul>
<li><a>Home</a></li>
<li><a>Service</a></li>
<li><a>Products</a>
<ul>
<li><a>Prod 1</a></li>
<li><a>Prod 2</a></li>
</ul>
</li>
<li> ... </li>
</ul>
</div>
</body>
</html>

cEM

le_gber

4:06 pm on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi cEM,

here's you code modified to suit the pb I had,

I think I did everything that I intended to do on my own page:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<style type="text/css">
#nav ul li a{
background: yellow;
}

#nav ul li a:focus, #nav ul li a:hover, #nav ul li a:active, #nav ul li#selectedlink{
background: blue;
}
#nav ul li ul li a{
background:red;
}
#nav ul li ul li a:focus, #nav ul li ul li a:hover, #nav ul li ul li a:active, #nav ul li ul li#selectedlink{
background:green;
}
</style>
</head>
<body>
<div id="nav">
<ul>
<li><a>Home</a></li>
<li><a>Service</a></li>
<li id="selectedlink">Products
<ul>
<li><a>Prod 1</a></li>
<li><a>Prod 2</a></li>
</ul>
</li>
<li> ... </li>
</ul>
</div>
</body>
</html>

And here's what I did to fix it:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<style type="text/css">
#nav ul li a{
background: yellow;
}

#nav ul li a:focus, #nav ul li a:hover, #nav ul li a:active, #nav ul li#selectedlink, #selectedlink{
background: blue;
}
#nav ul li ul li a{
background:red;
}
#nav ul li ul li a:focus, #nav ul li ul li a:hover, #nav ul li ul li a:active, #nav ul li ul li#selectedlink{
background:green;
}
</style>
</head>
<body>
<div id="nav">
<ul>
<li><a>Home</a></li>
<li><a>Service</a></li>
<li><span id="selectedlink">Products</span>
<ul>
<li><a>Prod 1</a></li>
<li><a>Prod 2</a></li>
</ul>
</li>
<li> ... </li>
</ul>
</div>
</body>
</html>

Thanks for the help.

Leo

natty

10:02 pm on May 18, 2005 (gmt 0)

10+ Year Member



do you have to do the

ul li ul li{

}

cant you just do

ul ul li{

}

or is that bad skills?

Robin_reala

11:41 am on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks better to me.

createErrorMsg

2:18 pm on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cant you just do
ul ul li{
}
or is that bad skills?

Which to use is primarily a matter of opinion. Because of the way descendant selectors work, either one will do the job just fine.

By using UL LI UL LI, the structure of the selector mirrors that of the markup it's connected to. In my experience, this makes it slightly easier to interpret the CSS at a later date without having the html markup in front of me. But, again, this would really just be a matter of preference.

I don't think either one is technically "better" than the other. There's a tendency in the realm of CSS to sweepingly equate "less" with "cleaner". While this may be true, IMO there is sometimes a worthwhile tradeoff between a few characters and human accessibility.

cEM

Robin_reala

2:37 pm on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Technically no, but I would find it easier to think 'ah, two nested levels' with the last example.