?
Solved

Hover problem with <li> and <a>

Posted on 2006-10-27
4
Medium Priority
?
598 Views
Last Modified: 2007-12-19
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>
0
Comment
Question by:madscientist
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:Animasu
ID: 17819607
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
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 17819610
#menu ul li a:hover {
  color: red;
}
0
 
LVL 7

Accepted Solution

by:
Animasu earned 300 total points
ID: 17819639
Oh and a side note :)

Put the css 'Display:block;' without the quotations, in you css on links as well! it makes the entire area a link then not just the text

- Animasu
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 17820048
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>
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question