简体   繁体   中英

CSS horizontal menu with 2 images; hover state & position

I'm designing a horizontal menu where I use a simple one-colored image of the full menu bar, and a second image for the hover state, which is a bit darker. Actually I made just one image containing both, but that's not the point.

My problems are :

  1. Hover image displays over the correct full width of each list item, but it's only a link (showing hand) when hovering the text.
  2. My attempt of double-class attributes to adjust position of hover image on first/last list item does not work. It seems this code is never reached..? Hence as image shows, I do not get adjusted the x-position of hover image.

Please note I started with CSS a few days ago, so there are likely some basic concepts I've not yet understood..

在此处输入图片说明

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>index</title>
        <meta name="author" content="SS" />
        <!-- Date: 2011-11-26 -->
        <link href="site2.css" rel="stylesheet"type="text/css" />
    </head>
    <body>
        <div class="header-wrap">
            header
        </div>
        <div class="main-wrap">
            <div class="main-content">
                <div class="inner-header">
                    inner header
                </div>
                <div class="navbox">
                    <div class="navmenu">
                        <ul class="ul_nav">
                            <li class="nav-item nav-item-first">
                                <a href="index.html">Home</a>
                            </li>
                            <li class="nav-item">
                                <a href="">Page1</a>
                            </li>
                            <li class="nav-item">
                                <a href="">Page2</a>
                            </li>
                            <li class="nav-item nav-item-last">
                                <a href="">Page3</a>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="lower-content">
            lower-content
        </div>
        <div class="footer">
            footer
        </div>
    </body>
</html>

CSS:

    /* Basics */
body {
    background: #888 url() center 30px;
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    color: #000;
    text-align: center;
    margin: 0 auto;
}
/* Main divs */
.header-wrap {
    background: #ddd;
    height: 35px;
    color: #00f;
}
.main-wrap {
    background: #000 url(07h600a.png) no-repeat center 0;
    margin: 0 auto;
}
.main-content {
    overflow: hidden;
    position: relative;
    z-index: 10;
    background: transparent; /* to be transparent*/
    height: 600px;
    margin: 0 auto;
    width: 980px;
}
.lower-content {
    background: #000;
    color: #888;
    height: 100px;
}
.footer {
    background: #777;
    height: 100px;
    margin: 0 auto;
}
/* Content */
/* Menu */
.navbox {
    background: #660;
    position: relative;
    padding: 0px 0 0 0;
    margin: 0 auto 0px;
    font-weight: bold;
    font-size: 12px;
    line-height: 40px;
    height: 50px;
    width: 980px;
    margin: 0 auto;
}
.navmenu {
    margin: 0 auto 0px;
    padding: 0 0 0 0;
    width: 980px;
}
.ul_nav {
    margin: 0;
    padding: 0;
    list-style: none outside none;
    text-transform: uppercase;
}
.navmenu .ul_nav {
    width: 980px;
    position: relative;
    display: table;
}
.ul_nav li {
    display: table-cell;
}
.ul_nav a, .ul_nav a:visited {

    padding: 0 0;
    text-decoration: none;
    color: #fff;
}
.navmenu .nav-item {
    background: transparent url(MenuGray.png) no-repeat scroll;
    background-position: 50% -10px;

}
.navmenu .nav-item-first {
    background-position: 0px -10px;
}
.navmenu .nav-item-last {
    background-position: 100% -10px;
}
/* Menu hover */
.ul_nav  .nav-item-first li:hover {
        background: transparent url(MenuGray.png) no-repeat scroll;
    background-position: 0px -50px;
}
.ul_nav  .nav-item-last li:hover {
    background-position: 100% -50px;
}
.ul_nav li:hover {
    background-position: 50% -60px;
}

/* Menu font */
.ul_nav_color a, .ul_nav_color a:visited {
    color: #FFF;
}

For

  1. You need to make sure the a tag fills the li parent by setting it to display block or inline-block and adjust padding, margin, etc., to have its edge match up to the li edge. This will get you a link over the whole space, not just the text.
  2. You have the li listed after the class for the li , so you need this:

     .ul_nav .nav-item-first:hover 

    Not

     .ul_nav .nav-item-first li:hover 

I'd suggest you go through http://css.maxdesign.com.au/listutorial/index.htm for some good examples of doing horizontal lists. Probably avoid setting your list to table.

Here is a simple horizontal list that sets the width/height of the anchor which you could change to an image if needed.

css

.newnav ul {margin:0; padding:0; list-style-type: none;}
.newnav ul li { display:inline-block;margin:0; padding:0; }
.newnav ul li a { text-decoration: none; background-color: #aaa; display:inline-block; width: 150px; height: 50px; line-height: 50px; }
.newnav ul li a:hover{background-color: #ddd;}

html

<div class="newnav">
                    <ul>
                        <li class="nav-item nav-item-first"><a href="index.html">Home</a></li>
                        <li class="nav-item"><a href="">Page1</a></li>
                        <li class="nav-item"><a href="">Page2</a></li>
                        <li class="nav-item nav-item-last"><a href="">Page3</a></li>
                    </ul>
                </div>

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