Solved

Hover problem with <li> and <a>

Posted on 2006-10-27
4
589 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
  • 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 75 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now