简体   繁体   中英

Having trouble with CSS hover with image background

I am having some trouble with a nav section on a site which is TEXT within a list... such as:

<div class="nav_wrapper">
    <ul class="nav">
        <li><a href="index.php">HOME</a></li>
        <li><a href="about.php">ABOUT US</a></li>
        <li><a href="services.php">SERVICES</a></li>
        <li class="last"><a href="contact_us.php">CONTACT US</a></li>
    </ul>
</div>

Now what I am trying to do is on hover have it display an image background; however when it does this the image doesn't display completely... it only surrounds the text itself; which makes sense as I am applying the background to the a tag.

I have tried setting a width & height to the a tag via css but it doesn't work.

So my question is how do I get it to display the whole image when they hover?

My current css is below:

#header .nav_wrapper {
    float: left;
    width: 597px;
    margin: 30px 0 0 50px;
}

#header .nav_wrapper .nav {
    list-style-type: none;
    list-style-image: none;
}

#header .nav_wrapper li {
    display: inline;
    width: 72px;
    height: 23px;
    font-family: "Zurich Cn BT", Tahoma;
    font-size: 12px;
    margin-right: 15px;
}

ul.nav li a, ul.nav li a:visited, ul.nav li a:focus {
    color: #764422;
    text-decoration: none;    
}

ul.nav li a:hover, ul.nav li a:active {
    color: #fff;
    text-decoration: none;
    background-image: url(../images/nav-bg.png);
}

#header .nav .last {
    width: 87px;
    margin-right: 0;
}

Add this CSS

ul.nav li a {
    display:block;
}

the <a> links need be display: block; in order to be able to take height and width

to make the block level links still display side by side you should float the li items or make them display: inline-block;

eg using float for the li 's and some colors for visualisation

#header .nav_wrapper ul {
    font-family: "Zurich Cn BT", Tahoma;
    font-size: 12px;    
    list-style: none;
    margin: 0;
    padding: 0;
}

#header .nav_wrapper li {
    float: left;
    width: 72px;
    height: 30px;
    margin-right: 15px;
    background: #eee;
    text-align: center;
}

#header .nav_wrapper li a {
  display: block; 
  line-height: 30px;
  }

ul.nav li a, ul.nav li a:visited, ul.nav li a:focus {
    color: #764422;
    text-decoration: none;    
}

ul.nav li a:hover, ul.nav li a:active {
    color: #fff;
    background: #444 url(../images/nav-bg.png);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM