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
1 2 3 4 5 6 7 8 9 |
#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
1 2 3 4 5 6 7 8 9 10 |
<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> <!-- end main_nav --> |
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
1 2 3 4 5 6 7 |
#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
1 2 3 4 |
#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.
1 2 3 4 5 |
body { behavior: url("/css/csshover.htc"); } |
The Whole Shabang
CSS
1 2 3 4 5 6 7 8 9 10 11 12 |
#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
1 2 3 4 5 6 7 8 9 10 |
<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> <!-- end main_nav --> |
Pingback: kre8ive » Advanced CSS Menu Trick
Pingback: Fatih Hayrio?lu’nun not defteri » 31 Aral?k 2007 Web’den Se?me Haberler
Pingback: 7 Advanced CSS Menu For Your Next Design
Pingback: 7 Menu css da usare nei propri siti web | Lordmarin
Pingback: CSS ile yatay menü ?rnekleri - Günlük Haftal?k Ayl?k
Pingback: Menú horizontal muy interesante
Pingback: napyfab:blog» Blog Archive » links for 2008-01-07
Pingback: 7个强大超酷的CSS导航菜单 | 帕兰卓一得
Pingback: Best Of January 2008 | Best of the Month | Smashing Magazine
Pingback: Best Of January 2008 | Best of the Month | Smashing Magazine
Pingback: Best Of January 2008 | Blog
Pingback: Advanced CSS Navigation Menu Trick
Pingback: purrl.net |** urls that purr **|
Pingback: Usenet Newsgroups: Anachronistic Service or Useful Communication Tool?
Pingback: 101 CSS Techniques Of All Time- Part 1
Pingback: links for 2008-01-28 | Funny Stuff is all around
Pingback: 7 Advanced CSS Menu, A Great Roundup!!
Pingback: Blog | exand.net » Мои закладки в Январе
Pingback: sbh* - Ma.gnolia: Recently Ma.rk’d
Pingback: Quick Accessibility Testing
Pingback: UK Government fails to take IPv6 implementation seriously
Pingback: Upgrading to Rails 2.0. A Recipe
Pingback: News » Best Of January 2008
Pingback: dock men? yap?m?_?_ - vBulletinTurk.org | Webmaster Forumu
Pingback: blog.bouctoubou.com » Archive du blog » Bouctoubou’s links Graphics #2
Pingback: Powerful CSS-Techniques For Effective Coding | How-To | Smashing Magazine
Pingback: Powerful CSS-Techniques For Effective Coding | Blog
Pingback: 50种强大的CSS技术 | 帕兰映像
Pingback: ????? ??????? ?? » ????? ??????? » ????? ?????? (?????? -2008)
Pingback: Powerful CSS-Techniques For Effective Coding
Pingback: Tips Trick, Software dan Aplikasi Review » Blog Archive » Powerfull CSS Teknik
Pingback: Skylog » Blog Archive » links for 2008-02-28
Pingback: Un par de trucos avanzados CSS « el50
Pingback: AllSprite | 关注互联网发展,为访客创造价值! » Blog Archive » 50种强大的CSS技术
Pingback: Powerful CSS-Techniques For Effective Coding
Pingback: Advanced CSS Navigation Menu Trick | GreatSo.com
Pingback: Truco CSS para Menu de Navegación | Blogultura.com
Pingback: Kolz Blog » Blog Archive » Powerful CSS-Techniques For Effective Coding
Pingback: jujubeans » Blog Archive » Gotta brush up on the scripting
Pingback: CSS Concept » Blog Archive » Techniques for a Professional CSS
Pingback: Sharepoint 2007 from an interface developer’s view
Pingback: Web Design » ? Advanced CSS Menu Trick - Web Design Marketing Podcast & Blog
Pingback: 傻仔仔 » 網誌彙整 » 50種強大的CSS技術
Pingback: Powerful CSS-Techniques For Effective Coding | Studio 646
Pingback: bdITjobs.com : : Blog » Blog Archive » Powerful CSS-Techniques For Effective Coding
Pingback: Flying Code — Flying over the code… » Articles » Créer un menu avec rollover avancé
Pingback: 50种强大的CSS技术 | CodeLog
Pingback: Powerful CSS-Techniques For Effective Coding | adtech ile reklam 2.0 d?nemi ba?l?yor ve Trkycmhrytllbtpydrklcktr r10.net seo yar??mas?
Pingback: Umair’s Blog » Blog Archive » Super Powerful CSS - Techniques For Effective Coding
Pingback: Powerful CSS Techniques For Effective Coding | Udesign's Blog
Pingback: CSS-Styled Lists: 20+ Demos, Tutorials and Best Of
Pingback: sapnagroup notes » Blog Archive » CSS Techniques
Pingback: 五毒 » Blog Archive » 7个强大超酷的CSS导航菜单
Pingback: Advanced CSS Menu Trick | WhiteSandsDigital.com
Pingback: Улыбнитесь » Blog Archive » CSS-Styled Lists: 20+ Demos, Tutorials and Best Practices
Pingback: deckox - The design Destination » Blog Archive » Advanced CSS Navigation Menu Trick
Pingback: 10 Challenging But Awesome CSS Techniques - NETTUTS
Pingback: 10 tutoriales de técnicas avanzadas de CSS | frogx.three
Pingback: 50种强大的CSS技术 | 伊莱利奥的博客
Pingback: Blog - WEBmagix » Blog Archive » Awesome CSS techniquies
Pingback: Simple Way to Modify Menu Items with jQuery | Joh.nson.us
Pingback: Colección Css (tutoriales): Layout, tables, forms, buttons… — WYDBLOG
Pingback: 50 técnicas avanzadas CSS para una codificación efectiva. | AlainAlemany
Pingback: WebMaster Hizmetleri - CSS ile yatay menü kullan?n - derleme
Pingback: 放飞梦想 » Blog Archive » 一个伟大的汇集(7个超炫的CSS菜单)
Pingback: Download 6 Free Ebooks,Software,Scripts and Templates, Iphone, Technology, Games, Life, Webmasters » Blog Archive » 50 Free CSS Navigation Menu Designs
Pingback: OpsiyonBlog.com | Video | Program | Photoshop | Müzik | Css | E-Ticaret | Script | Youtube | Yutub | » Blog Archive » CSS ile yatay menü ?rnekleri
Pingback: CSS ile yatay menuler | Egenc.Net | css ile menu yap, css, menu, css menu, yatay menu, menu yap, css menu yap | Egenc.Net | E?lence Sitesi
Pingback: N?gra CSS knep som ?r v?rt att ta en titt p? | Webbrelaterat
Pingback: Sete tutoriais avan?ados para criar menus via CSS - Blog do yogodoshi
Pingback: A Quick Introduction | StylizedWeb.com
Pingback: 50种强大的CSS技术 at cssframework
Pingback: A Quick Introduction | Castup
Pingback: Powerful CSS Techniques For Cool Website | Standalone Complex
Pingback: 43 PSD to XHTML, CSS Tutorials Creating Web Layouts And Navigation | 1stwebdesigner - Love In Design
Pingback: 30 Exceptional CSS Navigation Techniques
Pingback: 55 CSS Menu And Button Coding Tutorials « FED视野