20. June 2009 by Janko in Tutorials | tags:

I rarely see drop down menus that behave like drop down lists, but I really like them. I noticed, however, that people often use JavaScript to achieve this effect. Actually, there is a simple way to do it only with CSS.

View demo

You are probably familiar with the menu structure; nested unordered lists are common way to create menus.



Showing/hiding child menus is done with simple CSS: ul > li:hover ul. By using this selector we can define behavior of all child menus. When creating normal menus, besides display:block we set some other styles like background color and so on. The only difference between regular menus and "drop down list menus" is in the fact that we don't set styles for nested UL but rather for LI that is hovered (ul > li:hover).
#header { height:120px; position:relative;
background: transparent url(header_bkg.png) repeat-x scroll top center;}
#nav { margin:0px; padding:0px; position:absolute; top: 70px; display:block;}
#nav > li { list-style-type:none; float:left; display:block; margin:0px 10px; 
position:relative; padding:10px; width:100px;}
#nav > li:hover ul { display:block; }
#nav > li:hover { background-color:#808080; -moz-border-radius:10px; -webkit-border-radius:10px; }
#nav li ul { margin:0px; padding:0px; display:none;}
#nav li ul li { list-style-type:none; margin:10px 0 0 0;}
#nav li ul li a { display:block; padding:5px 10px; color:#A2E200; text-decoration:none;}
#nav li ul li:hover a { background-color:#606060; -moz-border-radius:5px; -webkit-border-radius:5px;}
#nav li span { cursor:pointer; margin:0px 10px; font-weight:bold; }
This is how it can be done jus with CSS. I really can't figure out why would someone use JavaScript for this, except for adding animations or other effect. Please note that this code doesn't work in IE6 (this poor browser just doesn't know what ul > li means).

If you liked this article why don't you share it:

Create dropdown menus with CSS only (via @jankowarpspeed) Share this on StumbleUpon Share this on delicious Share this on Digg Share this on Dzone Share this on DesignBump Send this to friend

Comments

Pingbacks and trackbacks

  1. Pingback from guiabreve.com Menús desplegables, ¡solo con CSS! - Guiabreve
  2. Pingback from chicablogger.com Menús CSS desplegables sin javascript | Chica Blogger
  3. Pingback from csshead.com Create dropdown menus with CSS only | CSS Head
  4. Pingback from arbent.net Arbenting’s Weekly Inspiration and Best of the Web #9 | Arbenting
  5. Pingback from ladosisdiaria.com Crea menús drop-down (desplegables) usando solo CSS | La Dosis Diaria
  6. Pingback from markdvl.com Creare un menu drop down con i CSS | Markdvl Killer Web Designer
  7. Pingback from scripting.com.cn 精通 CSS 造型设计元素(二)
  8. Pingback from theblog4.me Shopping Mall » Mastering CSS, Part 1: Styling Design Elements
  9. Pingback from erkcm.wordpress.com 15 New jQuery Plugins for Web development « Er.Krushna Chandra Muni :: Website Design Orissa,Website Design Bhubaneswar,Indian Web Designer,Indian Freelancer,website design india
  10. Pingback from ronaldnunez.com Let's update IE6 on your machine | Ronald Nuñez - Freelance Web Designer / Developer
  11. Pingback from topsy.com Twitter Trackbacks for Create dropdown menus with CSS only [jankoatwarpspeed.com] on Topsy.com
  12. Pingback from endorfine.od.ua » Mastering CSS, Part 1: Styling Design Elements endo – luxury coding
  13. Pingback from unionroom.com 15 New jQuery Plugins To Help You Conquer The Web | Web Design Blog
  14. Pingback from blog.cargoo.net 全面掌握CSS系列一:为设计元素添加样式效果(译文) « 日落旅馆
  15. Pingback from smashingmagazine.com Mastering CSS, Part 1: Styling Design Elements « Smashing Magazine
  16. Pingback from blog.barkerdesign.com Barker Design | Graphic & Web Development » Blog Archive » Styling HTML Lists with CSS: Techniques and Resources
  17. Pingback from blog.caointeractive.com CaoInteractive Blog | Graphic & Web Design » Blog Archive » Styling HTML Lists with CSS: Techniques and Resources
  18. Pingback from paranimage.com 精通CSS-1: 常用设计元素的高级CSS技巧 | 帕兰映像
  19. Pingback from dagonr.com 精通CSS造型设计元ç´
  20. Pingback from bbon.cn 精通 CSS 样式设计元素 - 菠菜博
  21. Pingback from mkbar.com 精通 CSS 造型设计元素 Mastering CSS - E@生活在别处
  22. Pingback from blogdesignworks.wordpress.com 15 New jQuery Plugins To Help You Conquer The Web « Design Works
  23. Pingback from muniu.info 精通 CSS 样式设计元素 - 木牛工作室
  24. Pingback from dreamstep.cn Dream step » Blog Archive » 精通 CSS 样式设计元ç´

Add comment

   
        Country flag
biuquote
Loading