Link to home
Start Free TrialLog in
Avatar of madscientist
madscientist

asked on

Hover problem with <li> and <a>

Hi, I have a menu using an unordered list and want the hover color of the link to show when hovering over the list item, but I can't figure out how to do it.....

Many Thanks, Adam.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
      <title>Test</title>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <meta http-equiv="Content-Language" content="en-gb" />
      <style type="text/css">
            * {
                  margin: 0px;
                  padding: 0px;
            }
            
            #menu {
                  position: absolute;
                  top: 100px;
                  left: 100px;
                  height: auto;
                  width: 120px;
                  background: #007aa8;
                  font-family: Verdana, Arial, Sans-Serif;
            }
            
            #menu ul {
                  list-style-type: none;
            }
            
            #menu ul li, #menu ul li.menu_norm {
                  padding: 3px 5px 2px 5px;
            }
            
            #menu ul li.menu_hover {
                  padding: 3px 5px 2px 5px;
                  background-color: #bcddeb;
                  color: #3564ae;
            }
            
            #menu a.level1:link, #menu a.level1:visited {
                  color: #ffffff;
                  font-size: 0.9em;
                  font-weight: normal;
                  text-decoration: none;
            }
            
            #menu a.level1:hover, #menu a.level1:active {
                  color: #3564ae;
                  font-size: 0.9em;
                  font-weight: normal;
                  text-decoration: none;
            }
            
      </style>
</head>

<body>

<div id="menu">
      <ul>
            <li onmouseover="this.className='menu_hover'" onmouseout="this.className='menu_norm'"><a href="#" class="level1">Link 1</a></li>
            <li onmouseover="this.className='menu_hover'" onmouseout="this.className='menu_norm'"><a href="#" class="level1">Link 2</a></li>
            <li onmouseover="this.className='menu_hover'" onmouseout="this.className='menu_norm'"><a href="#" class="level1">Link 3</a></li>
            <li onmouseover="this.className='menu_hover'" onmouseout="this.className='menu_norm'"><a href="#" class="level1">Link 4</a></li>
            <li onmouseover="this.className='menu_hover'" onmouseout="this.className='menu_norm'"><a href="#" class="level1">Link 5</a></li>
            <li onmouseover="this.className='menu_hover'" onmouseout="this.className='menu_norm'"><a href="#" class="level1">Link 6</a></li>
            <li onmouseover="this.className='menu_hover'" onmouseout="this.className='menu_norm'"><a href="#" class="level1">Link 7</a></li>
            <li onmouseover="this.className='menu_hover'" onmouseout="this.className='menu_norm'"><a href="#" class="level1">Link 8</a></li>
      </ul>
</div>

</body>
</html>
Avatar of Animasu
Animasu
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi Adam, do you meen you want it so your links are colored? as in on hover you get a differant background color, font color/style/border etc...?

If so then you only need to use CSS not Javascript!

a{color:black;}
a:hover{color:blue;background-color:red;}
a:active{color:purple;background-color:black;}
a:visited{color:red;background-color:blue;}
a:visited:hover{color:blue;background-color:red;}
a:visited:active{color:purple;background-color:black;}

if you want to define it a bit more as in "You only want These link to be hovers" then use


.name a{color:black;}
.name a:hover{color:blue;background-color:red;}
.name a:active{color:purple;background-color:black;}
.name a:visited{color:red;background-color:blue;}
.name a:visited:hover{color:blue;background-color:red;}
.name a:visited:active{color:purple;background-color:black;}

with the latter you need to specify the class in the <a> link eg...

<a href="#" class="name">Name</a>

This should work! if you require any more assistance please ask :)

- Animasu
Avatar of GrandSchtroumpf
GrandSchtroumpf

#menu ul li a:hover {
  color: red;
}
ASKER CERTIFIED SOLUTION
Avatar of Animasu
Animasu
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, i posted to fast whithout correctly looked at your code.
The color change was working fine with your initial code.
It's always a good idea to keep your code as simple as possible.
You don't need neither the javascript nor the "level1" class.
If you want to style nested lists (level2?) differently, then use "#menu ul ul li a" as selector.
This is what you should use:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
     <title>Test</title>
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
     <meta http-equiv="Content-Language" content="en-gb" />
     <style type="text/css">
         
          #menu {
               position: absolute;
               top: 100px;
               left: 100px;
               width: 120px;
               font-family: Verdana, Arial, Sans-Serif;
          }
         
          #menu ul, #menu li {
               list-style-type: none;
               margin: 0;
               padding: 0;
          }
         
          #menu ul li a {
               padding: 3px 5px 2px 5px;
               background: #007aa8;
               color: #ffffff;
               font-size: 0.9em;
               font-weight: normal;
               text-decoration: none;
               display: block;
          }
         
          #menu ul li a:hover, #menu ul li a:active  {
               background-color: #bcddeb;
               color: #3564ae;
          }
         
          /* Fix for IE6 */
          * html #menu ul li a {
               height: 1%;
          }
         
     </style>
</head>

<body>

<div id="menu">
     <ul>
          <li><a href="#">Link 1</a></li>
          <li><a href="#">Link 2</a></li>
          <li><a href="#">Link 3</a></li>
          <li><a href="#">Link 4</a></li>
          <li><a href="#">Link 5</a></li>
          <li><a href="#">Link 6</a></li>
          <li><a href="#">Link 7</a></li>
          <li><a href="#">Link 8</a></li>
     </ul>
</div>

</body>
</html>