简体   繁体   中英

CSS rollover navigation

I haven't really ever done a background navigation rollover, I usually just change the colour of the text once it's been rolled over. However I'm try to do this now but can't seem to get it right.

I'm trying to do it all with CSS as I believe there is a way however I do see a lot of others using sprites and image rollovers. Which way is the best? I might end up having a lot of images on my website so I'm trying to stay away from them so I myself, am thinking strictly CSS. There is a way right?

This is my website

CSS

#main-navigation { width: 100%; height: 100px; background: url(../img/NAV-BG.jpg) top center no-repeat; text-transform: uppercase; font-size: 1em; letter-spacing: 1px; line-height: 90px; /*border: 1px solid #000;*/ }
#main-navigation ul { width: 860px; list-style: none; margin: 0 auto; text-align: center;}
#main-navigation li { float: left ;margin-left: 30px; }
#main-navigation li a { display: block; text-decoration: none; color: #000; }
#main-navigation li a:hover { color: #c7bd89; background-color: #900; width: 120%; height: 30px; -moz-border-radius: 5px; border-radius: 5px; margin: 0 auto; margin-top: 20px;}

HTML

<nav id="main-navigation">
           <ul id="main-nav-left">
               <li class="current"><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Current Season</a></li>
               <li><a href="#">Past Seasons</a></li>
               <li><a href="#">Contact</a></li>
               <li><a href="#">Partners/Sponsors</a></li>
           </ul>
  </nav>

But I want it to look like this 在此处输入图片说明

What am I missing?

Use this

#main-navigation li a:hover {
    color: #c7bd89;
    background-color: #900;
    width: 120%;
    line-height: 30px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    margin-top: 30px;
}

All the problem is that you're defining a height ... You should define a line-height instead and it will work flawlessly ... But I still can find a space for improvement in terms of padding and margin.

see the fiddle for code and demo

fiddle: http://jsfiddle.net/quR4E/3/

demo: http://jsfiddle.net/quR4E/3/embedded/result/

screen shot:

在此处输入图片说明

Try changing your #main-navigation li a to this:

#main-navigation li a {
   /*display: block;*/
   text-decoration: none;
   color: black;
   padding: 5px;
}

Using display block was pushing the text outside the box. Adding the padding will give you some spacing around the text.

Sprites are definitely better than using multiple images but in the end they are essentially going to be the same thing. If you can create your images small enough (for bandwidth performance) it won't affect your site that much. Sprites are nice to group images. Using background colors and borders around text is also a very efficient way to go.

Have a look at what I did here: http://torontobanfffilmfest.com/splash

Each of the eight blocks has a single image in two versions, one light and one dark, attached side-by-side. In the top-left corner, for example, is an image, splash_buy_tickets_m.png, that is 582 pixels wide. But the space in which it's displayed is 291 pixels wide. We only see half the full image, and WHICH half depends on :hover.

The CSS that makes the image change on rollover is pretty simple:

#b1:hover, #b2:hover, #b3:hover, #b4:hover, #b5:hover, etc. {
 background-position: -291px 0;
}

If each of the buttons in your button bar consists of an "active" and an "inactive" version, then you can just change the image position within the DIV in which it's shown, shifting horizontally or vertically.

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