views:

223

answers:

8

Hi,

I got a problem like this (this is html/css menu):

Eshop | Another eshop | Another eshop

Client wants it work like this:

User comes to website, clicks on Eshop. Eshop changes to red color with red box outline. User decides to visit Another eshop, so Eshop will go back to normaln color without red box outline, and another eshop will do the red outline trick again..

I know there is A:visited but I don't want all visited menu links to be red with red box outline.

Thx for any help :)

+1  A: 

You can do this with CSS classes. For example, a selected class could identify the current shop, changing the color and outline. Then you can change the selection by adding/removing the class from the menu item.

Take a look here, it walks through a tutorial on building CSS menus.

Joe Skora
Can you be more specific please?
Skuta
A: 

As far as I know you can do this only by generating different code for every page (setting a different class for the current page) or by using JavaScript to change the menu after the page is loaded.

Nir
+2  A: 

The same that Joe Skora has written but more specific:

.red {
    outline-color:red;
    outline-width:10px;
}

Now you could use Javascript (in this example using jQuery) in the click-event-handler:

$('.red').removeClass('red'); // removes class red from all items with class red
$(this).addClass('red'); // adds class red to the clicked item

Another way of doing it is the use of the pseudo selector :target.
For informations about it: www.thinkvitamin.com

Georg
That relies on the links NOT targeting individual pages which, while possible, is unlikely
Bobby Jack
+1  A: 

Basically, it can't be done with CSS alone, some scripting would have to take place (server or client side, preferably server). As the others have suggested, add a 'selected' class (or something similar) to the active link, and define the styles for it in CSS.

For example, the links:

 <a href="#">Eshop</a> | <a href="#" class="selected">Another eshop</a> | <a href="#">Another eshop</a>

The styles:

.selected {
     font-weight:bold;
     color:#efefef;
}

The links would be generated dynamically, using PHP for example:

 <?php
 foreach(array('eshop' => '#','another eshop' => '#','yet another eshop' => '#') as $title => $url) {
      echo '<a href="' . $url . '"' 
           . ($url == $_SERVER['REQUEST_URI'] ? ' class="selected"' : null) 
           . '>' . $title . '</a>'; 
 }
Eran Galperin
A: 

you could use and attribute selector like this...

a[href^="http:\\www.EShop"]:visted { color: red; }

By doing that you are saying any link that has a href that starts with http:\Eshop.com and has been visted apply this style.

Matthew M. Osborn
+1  A: 

You can do this with plain CSS and HTML. A method we commonly use is to have a matching ID and class selector for each navigation item.

The benefit to this is that you don't have to modify your menu code per page, you modify the page itself, which you'll already be doing unless everything is fully dynamic.

It works like this:

<!-- ... head, etc ... -->
<body>

<ul class="nav">
    <li><a href="home.html" class="nav-home">Home</a></li>
    <li><a href="art.html" class="nav-art">Art</a></li>
    <li><a href="contact.html" class="nav-contact">Contact</a></li>
</ul>

<!-- ... more page ... -->

</body>

Then you set up some CSS like this:

#NAV-HOME .nav-home,
#NAV-ART .nav-art,
#NAV-CONTACT .nav-contact { color:red; }

To change the "current" menu item, you can just assign the corresponding ID to an element higher in the document's structure. Typically I add it to the <body> tag.

To highlight the "Art" page, all you have to do is this:

<!-- The "Art" item will stand out. -->
<body id="NAV-ART">

<ul class="nav">
    <li><a href="home.html" class="nav-home">Home</a></li>
    <li><a href="art.html" class="nav-art">Art</a></li>
    <li><a href="contact.html" class="nav-contact">Contact</a></li>
</ul>

<!-- ... more page ... -->

</body>
Zack Mulgrew
A: 

It depends on how your pages are constructed, but the classic CSS was of doing this is with an id on the body, as well as each navigational link, so you might have something like:

eshop.html

<body id="eshop">
  <ul>
    <li><a href="" id="link-eshop">Eshop</a></li>
    <li><a href="" id="link-aeshop">Another eshop</a></li>
    <li><a href="" id="link-eshop-three">Another eshop</a></li>
  </ul>
</body>

and corresponding CSS:

#eshop #link-eshop, #aeshop, #link-aeshop, #eshop-three #link-eshop-three
{
    color: red;
    outline: 1px solid red;
}

the navigation is consistent; only the id on the body changes from page to page.

Bobby Jack
+1  A: 

If you are moving to a new page in the same browser window, Zack Mulgrew and Bobby Jack both have excellent answers.

If you are opening the eshop link in a new window, there is not much you can do with css alone, and gs has a reasonable answer except for the choice of class name of (red).

Which is it?

Traingamer