I am new to developing web pages. I am looking to create menus similar to the ones in stackoverflow.com (like Questions, Tags, Users shown above). How do I change the color of the selected menu (for example, the background color of the Question changes to orange when 'clicked')?
I have managed to change the color while hovering (using CSS) as that was simple, but I am having trouble with this.
Can I achieve this effect of changing the color of a clicked item using only CSS?
Set the styles for class active and hover:
Than you need to make the li active, on the server side. So when you are drawing the menu, you should know which page is loaded and set it to:
But if you are changing the content without reloading, you cannot change set the active li element on the server, you need to use javascript:
It would probably be easiest to implement this using JavaScript ... Here's a JQuery script to demo ... As the others mentioned ... we have a class named 'active' to indicate the active tab - NOT the pseudo-class ':active.' We could have just as easily named it anything though ... selected, current, etc., etc.
I use PHP to find the URL and match the page name (without the extension of .php, also I can add multiple pages that all have the same word in common like contact, contactform, etc. All will have that class added) and add a class with PHP to change the color, etc. For that you would have to save your pages with file extension
.php
.Here is a demo. Change your links and pages as required. The CSS class for all the links is
.tab
and for the active link there is also another class of.currentpage
(as is the PHP function) so that is where you will overwrite your CSS rules. You could name them whatever you like.I'm late to this question, but it's really super easy. You just define multiple tab classes in your css file, and then load the required tab as your class in the php file while creating the LI tag.
Here's an example of doing it entirely on the server:
CSS
PHP
Assuming you want to change the colour of the currently selected link/tab... you're best bet is to apply a class (say
active
) to the currently selected link/tab and then style this differently.Example style could be:
Try this. It holds the color until another item is clicked.