Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Hover problem with <li> and <a>

Posted on 2006-10-27
4
Medium Priority
?
606 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
Suggested Courses

598 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