Solved

Hover problem with <li> and <a>

Posted on 2006-10-27
4
591 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CSS Position not returning 2 59
alert before form submission 6 42
bootstrap footer centering and expand problems 7 47
Width of Page is Affecting Side Menu 6 27
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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 tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…

825 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