Advanced CSS Menu Trick
December 22, 2007 @ 6:25 pm . Comments (60)
CSS really opens the doors to a lot of powerful and rich opportunities. It is funny how such minor things can create a whole new look, feel, and effect of a site. The beauty of CSS really is that it gives you power, but not too much power. It is not a tool like flash that really invites you to run away and take things too far.
We are now coming to a point where the browsers are supporting a lot of new features, giving us more opportunities to take advantage of previously unused pseudo elements. This example, “advanced css menu tricks” will work perfectly in any modern browser, yet still be fully functional in your older version of IE as well.
The goal of the demo - example
What we want to do here, is instead of simply altering the state of the navigation item the user is currently rolling over, we want to alter the non navigation items as well. This will help focus the users attention on the item they have selected on, and create a new look and feel for the site overall. Want to see it in action? Look at my demo page before we start.
The first step - CSS roll overs
The first step of the game is building some CSS roll overs. We want to keep things accessible so I have opted to use an IR technique. Essentially we create an image that has both the static, active and rolled over state all lined up next to each other. We then set the image as the background of th element, but the width is only wide enough for one state of the image (so if the button image is 600px wide with all three states, we make the navigation element as a 200px wide button). We then set the text indent really high and overflow to hidden so that it pushes the text out of the box. Then we only see the image even though there is still HTML text on the page for search engines and accessibility.
Image Examples
Menu Before
Button Sliced, fixed and hover states

The CSS
#main_nav { list-style: none; margin: 0; padding: 0; }
#main_nav li { float: left; }
#main_nav li a { text-indent: -999999px; overflow: hidden; display: block; height: 72px; }
#home { background: url(../images/navigation/home.gif); width: 103px; }
#home.active { background: url../images/navigation/home.gif) -103px 0; }
The HTML
<ul id="main_nav">
<li><a href="index.html" accesskey="3" id="home" title="Home Page">Home Page</a></li>
<li><a href="about-us.html" accesskey="4" id="about" title="About 3.7 Designs">About Us</a></li>
<li><a href="web-design.html" accesskey="5" id="webdesign" class="active" title="Web Design and Development">Web Design</a></li>
<li><a href="graphic-design.html" accesskey="6" title="Graphic Design and Marketing" id="graphicdesign">Graphic Designs</a></li>
<li><a href="keyword-optimization/michigan-seo.html" accesskey="7" title="Search Engine Optimization and Marketing" id="seo">Search Engine Optimization SEO</a></li>
<li><a href="ann-arbor-marketing.html" accesskey="8" id="contact" title="Contact Us">Contact Us</a></li>
</ul>
Then when we want to change the state of the button we simply adjust the background position to be -200px (or the size of the button itself) pulling the different state of the button into view. The reason for doing it this way then simply swapping images, is the latter method tends to create flickering in some browsers.
At this point most people have it set so that if an item is hovered on (#home for example) it switches the background-image position. This creates the standard run of the mill css roll over. However we want to do something else, something more unique. We want to have every roll over item on the menu change except the one you are hovering on. This requires a little css trickery!
IE7, Safari, Firefox, all support the :hover pseudo selectors so let’s take advantage of that. What we need to do is have all the menu items change the background-image position when the menu item itself has been rolled over. Then the item that is hovered on is set to have the background-position: 0px to keep it from moving when the rest do.
The CSS
#main_nav:hover li a#webdesign { background-position: -280px; }
#main_nav:hover li a#home { background-position: -206px; }
#main_nav:hover li a#graphicdesign { background-position: -340px; }
#main_nav:hover li a#contact { background-position: -232px; }
#main_nav:hover li a#about { background-position: -242px; }
#main_nav:hover li a#seo { background-position: -540px;
This pulls each menu item’s background position back when any of #main_nav has been hovered on. Now all we have to do is set the hovered items to have a background-position of 0
#home { background: url(../images/navigation/home.gif); width: 103px; }
#home:hover { background: url(../images/navigation/home.gif) 0 0 !important; }
The HTML is all set, already coded! Now you are ready to rock and roll except for that pesky IE5.5+. Luckily there has been a behavior file developed called cssHover.htc that will fix this issue. Simply download it, and copy and paste the following code into an IE5+ specific style sheet.
body {
behavior: url("/css/csshover.htc");
}
The Whole Shabang
CSS
#main_nav { list-style: none; margin: 0; padding: 0; }
#main_nav li { float: left; }
#main_nav li a { text-indent: -999999px; overflow: hidden; display: block; height: 72px; }
#home { background: url(../images/navigation/home.gif); width: 103px; }
#home:hover { background: url(../images/navigation/home.gif) 0 0 !important; }
#home.active { background: url../images/navigation/home.gif) -103px 0; }
#main_nav:hover li a#home { background-position: -206px; }
HTML
<ul id="main_nav">
<li><a href="index.html" accesskey="3" id="home" title="Home Page">Home Page</a></li>
<li><a href="about-us.html" accesskey="4" id="about" title="About 3.7 Designs">About Us</a></li>
<li><a href="web-design.html" accesskey="5" id="webdesign" class="active" title="Web Design and Development">Web Design</a></li>
<li><a href="graphic-design.html" accesskey="6" title="Graphic Design and Marketing" id="graphicdesign">Graphic Designs</a></li>
<li><a href="keyword-optimization/michigan-seo.html" accesskey="7" title="Search Engine Optimization and Marketing" id="seo">Search Engine Optimization SEO</a></li>
<li><a href="ann-arbor-marketing.html" accesskey="8" id="contact" title="Contact Us">Contact Us</a></li>
</ul>
No Tags
« Web Design and Marketing Solutions for Business Websites - Review | Optimized Press Releases for Time Sensitive Marketing »


COMMENT kre8ive » Advanced CSS Menu Trick
[…] Check it out! While looking through the blogosphere we stumbled on an interesting post today.Here’s a quick excerpt CSS really opens the doors to a lot of powerful and rich opportunities. It is funny how such minor things can create a whole new look, feel, and effect of a site. The beauty of CSS really is that it gives you power, but not too much power. It is not a tool like flash that really invites you to run away and take things too far. We are now coming to a point where the browsers are supporting a lot of new features, giving us more opportunities to take advantage of previously unused pseudo element […]
COMMENT Andrei Gonzales
Clever.
Another way to achieve this is by using Apple.Com’s menu technique (multiple states on a single image - controlled by positions).
COMMENT Fatih Hayrioğlu’nun not defteri » 31 Aralık 2007 Web’den Seçme Haberler
[…] Güzel bir css menü örneği. Bağlantı […]
COMMENT Peter
That is a good technique. I would like to point out to you that your site slightly bends (not break;)) when viewed in Opera. The grey background and page itself seem to extend as far as if the content were in a horizontal line.
COMMENT Jermayn Parker
I like this…
Something different and a nice option that can be used for all browsers and look the same.
Well done. I will definitely have to use something like this.
COMMENT 7 Advanced CSS Menu For Your Next Design
[…] 2) Advanced CSS Menu Trick […]
COMMENT 7 Menu css da usare nei propri siti web | Lordmarin
[…] 2) Advanced CSS Menu Trick […]
COMMENT CSS ile yatay menü örnekleri - Günlük Haftalık Aylık
[…] Advanced CSS Menu Trick […]
COMMENT Menú horizontal muy interesante
[…] que utilizando técnicas CSS desarrolla un menú horizontal muy curioso, lo tenemos en Advanced CSS Menu Trick, en donde te explica los códigos empleados y el CSS, para que puedas realizar dicho menú, […]
COMMENT napyfab:blog» Blog Archive » links for 2008-01-07
[…] » Advanced CSS Menu Trick - Web Design Marketing Podcast & Blog (tags: css menu navigation tutorial rollover webdesign web development advanced design hover) […]
COMMENT Jon
thanks
COMMENT psilos
Whenever I try to load the example page, my FF 2.0.0.11 breaks.
Just to know.
COMMENT 7个强大超酷的CSS导航菜单 | 帕兰卓一得
[…] 2) 先进的CSS导航菜单技术 […]
COMMENT Best Of January 2008 | Best of the Month | Smashing Magazine
[…] Advanced CSS Menu TrickWhat we want to do here, is instead of simply altering the state of the navigation item the user is currently rolling over, we want to alter the non navigation items as well. […]
COMMENT Best Of January 2008 | Best of the Month | Smashing Magazine
[…] Advanced CSS Menu TrickWhat we want to do here, is instead of simply altering the state of the navigation item the user is currently rolling over, we want to alter the non navigation items as well. […]
COMMENT Best Of January 2008 | Blog
[…] Advanced CSS Menu TrickWhat we want to do here, is instead of simply altering the state of the navigation item the user is currently rolling over, we want to alter the non navigation items as well. […]
COMMENT one web designer
ADVANCED WOULD BE MAKING IT WORK IN IE6
than you’ll see some trafic
COMMENT Ross
It does work in IE6 with the behavior file mentioned at the end of the article.
COMMENT Advanced CSS Navigation Menu Trick
[…] out the advanced CSS navigation menu first before read the post! Advanced CSS Menu Trick is a new concept of navigation menu solution for modern browsers. Unlike any hovered on menu that […]
COMMENT purrl.net |** urls that purr **|
This is one of the web’s most interesting stories on Sun 27th Jan 2008…
These are the web’s most talked about URLs on Sun 27th Jan 2008. The current winner is …..
COMMENT Usenet Newsgroups: Anachronistic Service or Useful Communication Tool?
[…] 2) Advanced CSS Menu Trick […]
COMMENT 101 CSS Techniques Of All Time- Part 1
[…] 2) Advanced CSS Menu Trick […]
COMMENT links for 2008-01-28 | Funny Stuff is all around
[…] » Advanced CSS Menu Trick - Web Design Marketing Podcast & Blog (tags: css menu navigation tutorial webdesign hover rollover) […]
COMMENT 7 Advanced CSS Menu, A Great Roundup!!
[…] 2) Advanced CSS Menu Trick […]
COMMENT Blog | exand.net » Мои закладки в Январе
[…] Advanced CSS Menu Trick […]
COMMENT Foxinni - Wordpress Designer
Very nice. Clean, easy, good looking. Great post!
COMMENT sbh* - Ma.gnolia: Recently Ma.rk’d
[…] how and when to use them.”Rating: ★ ★ ★ ★ ★ Tags: web, design» Advanced CSS Menu Trick - Web Design Marketing Podcast & BlogCool i’l blurred out menu that creates good focus. I like the idea.Rating: ★ ★ […]
COMMENT Quick Accessibility Testing
[…] 2) Advanced CSS Menu Trick […]
COMMENT UK Government fails to take IPv6 implementation seriously
[…] 2) Advanced CSS Menu Trick […]
COMMENT Upgrading to Rails 2.0. A Recipe
[…] 2) Advanced CSS Menu Trick […]
COMMENT News » Best Of January 2008
[…] Advanced CSS Menu Trick What we want to do here, is instead of simply altering the state of the navigation item the user is currently rolling over, we want to alter the non navigation items as well. […]
COMMENT dock men yapm_?_ - vBulletinTurk.org | Webmaster Forumu
[…] Advanced CSS Menu Trick […]
COMMENT blog.bouctoubou.com » Archive du blog » Bouctoubou’s links Graphics #2
[…] exemple de menu en css où j’ai bien aimé l’effet de mise en avant du rollover avec un effet de flou sur le […]
COMMENT Powerful CSS-Techniques For Effective Coding | How-To | Smashing Magazine
[…] Advanced CSS Menu TrickWhat we want to do here, is instead of simply altering the state of the navigation item the user is […]
COMMENT Powerful CSS-Techniques For Effective Coding | Blog
[…] Advanced CSS Menu TrickWhat we want to do here, is instead of simply altering the state of the navigation item the user is […]
COMMENT 50种强大的CSS技术 | 帕兰映像
[…] Advanced CSS Menu Trick […]
COMMENT مدونة الدكتور نت » أرشيف المدونة » مواقع ووصلات (فبراير -2008)
[…] قائمة متقدمة بواسطةcss […]
COMMENT Powerful CSS-Techniques For Effective Coding
[…] robust copyright information without defacing your own work. 8. Particletree Category List 9. Advanced CSS Menu Trick What we want to do here, is instead of simply altering the state of the navigation item the user is […]
COMMENT Tips Trick, Software dan Aplikasi Review » Blog Archive » Powerfull CSS Teknik
[…] Advanced CSS Menu Trick […]
COMMENT Jarek
Good CSS Menu Trick.
COMMENT jarald
Hi
this example are very nice.
i need one more help.
how to give sub menus????????
jarald
COMMENT David Jacques-Louis
Really creative.
COMMENT Skylog » Blog Archive » links for 2008-02-28
[…] Advanced CSS Menu Trick (tags: css) […]
COMMENT Un par de trucos avanzados CSS « el50
[…] el cursor sobre él, es mejor que lo veas en “vivo“. Acá el artículo para recrearlo Advanced CSS Menu Trick. Posted by Ulises E. Filed in CSS, Diseño Web, […]
COMMENT AllSprite | 关注互联网发展,为访客创造价值! » Blog Archive » 50种强大的CSS技术
[…] Advanced CSS Menu Trick […]
COMMENT Powerful CSS-Techniques For Effective Coding
[…] Advanced CSS Menu TrickWhat we want to do here, is instead of simply altering the state of the navigation item the user is […]
COMMENT St4lkr
Why this technique doesn’t work under a joomla site? Look here: (http://www.eliacomes.com)
COMMENT Advanced CSS Navigation Menu Trick | GreatSo.com
[…] new features, giving us more opportunities to take advantage of previously unused pseudo elements. Advanced Css Menu Tricks is instead of simply altering the state of the navigation item the user is currently rolling over, […]
COMMENT Truco CSS para Menu de Navegación | Blogultura.com
[…] 3point7designsVía: WebAppers Escrito el 04.03.08 @ 5:58 pm | Categoria: CSS, Diseño Web | | […]
COMMENT Kolz Blog » Blog Archive » Powerful CSS-Techniques For Effective Coding
[…] robust copyright information without defacing your own work. 8. Particletree Category List 9. Advanced CSS Menu Trick What we want to do here, is instead of simply altering the state of the navigation item the user is […]
COMMENT jujubeans » Blog Archive » Gotta brush up on the scripting
[…] today, I found this which points out the behavior whatever:hover (somewhat like the png fix). I remember hearing about […]
COMMENT CSS Concept » Blog Archive » Techniques for a Professional CSS
[…] Advanced CSS Menu Trick […]
COMMENT bleki
Brillant idea. But in code is error which made me mad. I spend a half day to before it works properly. In line:
[code]
#home.active { background: url../images/navigation/home.gif) -103px 0; }
[/code]
you forgott about “(” before path to image.
COMMENT nihal koçan
malesef kod da bazı eksiklikler var sanırım ben yapamadım bunu indirmemizin bir yolu varmı?????
Helpppp….!!!
COMMENT Web Design Manager
May be you are right with all these opportunities of CSS. I will test them and give my opinion after.
COMMENT Sharepoint 2007 from an interface developer’s view
[…] 2) Advanced CSS Menu Trick […]
COMMENT Web Design » » Advanced CSS Menu Trick - Web Design Marketing Podcast & Blog
[…] Online Money Making Forum | HYIP | Autosurf | Forex | Investments wrote an interesting post today on » Advanced CSS Menu Trick - Web Design Marketing Podcast & BlogHere’s a quick excerpt […]
COMMENT 傻仔仔 » 網誌彙整 » 50種強大的CSS技術
[…] Advanced CSS Menu Trick […]
COMMENT dennis
gotta try this and see if it works. thanks.
COMMENT imran
intresting…