Avatar of Siv
SivFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

How to use CSS to switch custom images in Unordered List.

Hi,
I have been playing round with an unordered list that I have used CSS to alter the bullet image from the usual black spot to a ball graphic.  

What I want to do is make it that when you hover over each item in the list which is being used as a "nav" block for my page, the image toggles to a version that looks highlighted (ie slightly brighter colour) to enhance the selection.  In the past I have used JavaScript to switch the images but I am sure you ought to be able to do it with CSS.

I have used the background-image rather than list-style-iamge as another issue I was getting was that the text of the list was not centred vertically on the bullet symbol and it didn't work either as far as the image toggling goes.

I have played around with the background-position attribute and found that when the mouse hovers over the link the background image appears but only in the section of the li element where the text is, not over the existing sphere image.  I then tried making its value negative and as you get nearer to the left edge of the screen it just disappears behind the original image that was layed out in the CSS for the li element.

If you visit this page you will see how it is with a background-position set to -20 so the image is appearing but when I set it to -40 it does not replace the original image.

Lnk to my test page

The Style is in the sheet so if you view the source you should see where I have got to.  Any help with this using CSS would be greatfully accepted.

Siv
CSSWeb DevelopmentHTML

Avatar of undefined
Last Comment
SSupreme
ASKER CERTIFIED SOLUTION
Avatar of SSupreme
SSupreme
Flag of Belarus image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Siv
Siv
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

@SSupreme,

Yo da man!

Can you explain why that works?

Many thanks!
Avatar of Siv
Siv
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Answer direct and to the point.
Worked a treat.
thanks a lot.
Siv
Avatar of SSupreme
SSupreme
Flag of Belarus image

There are two elements which could be affected by styling, they are <li> and <a>.
You applied "unselected" background for <li> tag and "selected" for <a> tag, so basically you added extra background for <a> which appear on hover. If you want to change <li>'s "unselected" background, you need to apply style for <li> not for <a>.

Avatar of SSupreme
SSupreme
Flag of Belarus image

You have this style for <li>:
nav li { background-image: url("images/ButtonUnselected.jpg");
on hover will be this style for <li>:
nav li:hover { background-image: url("images/ButtonSelected.jpg");
Avatar of Siv
Siv
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

@SSupreme

Blimey, I can't believe I didn't think of that. I think this is a case of two pairs of eyes are better than one. When you know the answer it's so simple.

Thanks again much appreciated.

Siv
Avatar of SSupreme
SSupreme
Flag of Belarus image

You are welcome.
Web Development
Web Development

Web development includes all aspects of presenting content on intranets and the Internet, including delivery development, protocols, languages and standards, server software, browser clients, databases and multimedia generation.

78K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo