
Dropdown menus are a great way of including a long list of links without cluttering up your page. The issue though is that they can be hard to style, but look quite ugly if you don’t. There isn’t a lot of flexibility with HTML’s <select> tags.
Well, why not do it with plain HTML instead?
In this post, I’m going to walk you through how I’ve set up the blogroll in our upcoming redesign. Load up the example page to see how our final product will look.
By using CSS only, you avoid slowing down your page while visitor’s download an extra script. On top of that, you don’t need to worry about user’s having JavaScript disabled.
The only caveat is that this method won’t work on IE6 (At least, not without doing some tweaking specifically for IE6 users). But as you’ll see at the end, this could be much more accessible than if you used JavaScript.
1 – Create the Image
The HTML we use later won’t have any traditional <select> codes, so by itself, it’s not going to look anything like a dropdown. We need to take care of that ourselves.
The image you create can look any way you want it to, but since this is a dropdown menu, a little arrow pointing downwards is your best option for easy usability.
The size of the image is also important. It can be any height you like, but the width will be the width of your dropdown.
This is what I have ended up with:
2 – Set up The HTML
Now we set up our HTML. This is where the power of this method comes into play, you are using simple HTML that you can later style in any way that suits you.
We need an unordered list to store all of the links. We will then precede that with a paragraph (or a ‘h’ tag if you prefer) to title it, and finally, we wrap the whole thing in a div.
Your final markup should look like this:
<div class="blogroll"> <p>Blogroll</p> <ul> <li><a href="http://www.site.com/">Example Site</a></li> <li><a href="http://www.site.com/">Example Site</a></li> <li><a href="http://www.site.com/">Example Site</a></li> </ul> </div>
Of course, you may want to use WordPress’ wp_list_bookmarks(); function to use the built in linkroll instead. Either will work.
3 – Style The Menu
For now we are going to forget about the dropdown functionality, and simply design the dropdown as it will be when someone’s mouse is over the image.
The first step is to replace the paragraph text with the image we created earlier.
.blogroll p { background:url(pbd-css-blogroll-rollover-img.png) 0 0 no-repeat; width:274px; height:21px; text-indent:-9999px; }
As you can see, we apply the image as the background first. We then set the dimensions of the paragraph to be the same as those of the image, and finally, we use text-indent to hide the “Blogroll” text from our HTML.
Next we have to style the list. Because our HTML is so simple, you will have no problem styling this any way you like. For instance, you can set a background on the dropdown, or add other types of HTML (Like images).
All I am going to do here is set up a plain list of all the links, and change the background color of each link on rollover. The CSS I’ve used is as follows: (Feel free to adapt for your own site)
.blogroll ul { width:272px; background:#214868; border:1px solid #48758d; border-width:0 1px 1px 1px; padding:0; list-style:none; } blogroll ul a:link, .blogroll ul a:visited { display:block; width:252px; padding:2px 10px 2px 10px; text-decoration:none; color:#93b8d3; font-weight:normal; } .blogroll ul a:hover, .blogroll ul a:active {background:#366386;}
The final thing to note is that we currently have a space between our image and our drop-down. We can remove this by amending our CSS:
.blogroll p, .blogroll ul {margin:0;}

4 – Create the Dropdown
Now it’s time to create the dropdown. The trick is very straightforward – We are going to use the div to hide everything other than the image until the user’s mouse is over it.
To start with, let’s hide everything other than our image.
.blogroll { width:274px; height:21px; overflow:hidden; }
And then we just use the :hover pseudo-class to allow the excess to be shown when the user scrolls over the image.
.blogroll:hover {overflow:visible;}
5 – Fix The Issue of Pushing Content Down
We are almost done. The rollover works, but when the menu appears, it pushes all the content beneath it down. This jumping content looks poor.
We are going to fix that by using absolute positioning to put the dropdown menu above all the other content. That way, there will be no jumping when the menu comes into play.
The first step is to amend the blogroll div to be positioned relatively (So that the dropdown’s absolute positioning can be anchored by this div).
.blogroll {position:relative;}
Now use CSS to position the menu right under the image.
.blogroll ul { position:absolute; left:0; top:21px; }
6 – Issues with IE 6
This menu won’t work with IE6 because it does not support :hover on anything other than links.
There are some workarounds to this using JavaScript. For instance, you could use conditional comments so that IE6 users use JavaScript to see the menu. Or you could try to teach IE6 to use :hover properly.
But given that the whole point of this was to avoid extra scripts, the best solution might be to simply hide the menu altogether. It’s not crucial content and if as few of your reader’s use IE6 as this site’s does, your best bet may just be this:
.blogroll {display:none;} html>body .blogroll {display:block;}
Conclusion
You can see the full code in working order on the example page. I love the control you have over it and the fact that you aren’t using any scripts so load times are as fast as they can be.
Not supporting IE6 is a drawback, but not as serious a one as it could be. For instance, 14% of this site’s visitors have JavaScript disabled, but only 2.6% are using IE6. That means that 5 times more people have a working blogroll than if I had used a JavaScript solution.
What do you think? Will you be using a CSS-only menu on your blog at some point, or do JavaScript effects still hold sway over you?
Custom Search
UPrinting (9 comments)24 July 09
Drop downs are wonderful to keep the layout less cluttered and organized. It presents a very clean look and allows for easier site navigation. This is an excellent tutorial, I wish it worked on IE6 as well so more people can make use of it
Thanks for sharing this, very useful!
chris (18 comments)24 July 09
thank you for this excellent article, extremely well writen and very easy to follow.
docpap (1 comments)24 July 09
Really usefull and simple: well done!
.-= docpap´s last blog ..Hard disk virtuali =-.
Squidge (2 comments)24 July 09
Great tutorial, extremely easy to follow. Just what I was looking for for my new site design.
.-= Squidge´s last blog ..Keeping the Cosmic Trigger Happy: Super Furry Animals live at Somerset House =-.
Adi (2 comments)25 July 09
Finally I found this tutorial on your blog. I already looking for something like this for my company website development. I like the way you wrote this tutorial, clear explanation and easy to understand. Thank a lot for this useful tutorial.
Gamer (3 comments)26 July 09
Exellent work! Thanks!
Brian Yerkes (1 comments)27 July 09
Nice tutorial, especially with the added IE6 support.
I recently re-designed my blog, and used a nice bit of jquery and css to keep the side bar clean, you can check it out for ideas if you want http://www.brianyerkes.com/
Thanks for the post
.-= Brian Yerkes´s last blog ..How To Increase Your RSS Subscriber Numbers =-.
tasarhane (5 comments)28 July 09
good post.. bookmarked..
.-= tasarhane´s last blog ..Google Chrome İşletim Sistemi =-.
Freelance Jobs (3 comments)29 July 09
Wow, this is Super XHTML Tutorial.. I will test it.
.-= Freelance Jobs´s last blog ..softphone for a voipswitch =-.
Michael Martin (1319 comments)31 July 09
Glad everyone enjoyed the post, I’ll try to keep writing more like it!
@Brian – The redesign looks awesome! The JQuery animations on your sidebar menus are great, very nicely done
joyologo design shop 2.0 (1 comments)31 July 09
wow very clever, it looks beautiful..
.-= joyologo design shop 2.0’s Latest Post : premium news wordpress theme =-.
chris (18 comments)3 August 09
Great! will test it for sure
Drupal Developmenrt (1 comments)6 August 09
Very nice read. Thank you for the information. Will apply this information and see the results.
min HMN (1 comments)6 August 09
well… thanks for share
Omar (2 comments)13 August 09
this is an awesome technique i will be using it a lot now! ^^
Ömer UZUN (6 comments)14 August 09
Wow, great. use ?
Kerry (1 comments)18 August 09
Thanks for this; I’m in the process of making my first website, and whilst I’m getting along quite nicely with Javascript as a whole I prefer to exclude it when I can. This is the best tutorial I’ve found offering a purely HTML/CSS based method…that looks good. I’ll be subscribing too
suny (3 comments)3 September 09
Great tutorial! But i don’t like to use dropdown menu in my blog. hehehh。。。
Poker Seo (1 comments)18 September 09
Looks awesome ty
mritunjay (1 comments)30 September 09
very Good…!
JAMEZ (1 comments)25 October 09
can this be modified for categories in wordpress?
BEBEN (1 comments)25 October 09
cool…how to using this trick
BeyondRandom (1 comments)5 November 09
I plan on putting this to use tonight! thanks alot for the great write up.
VB6 Read Text File (1 comments)20 November 09
You are explaining everything in a perfect way with screen shots and screens. the article reading is exceptionally good
now a days we don’t need to worry about java script as most of the emails don’t work(yahoo,msn)
Georg Pichler (1 comments)29 November 09
Hello there,
Im just trying this out on my XMPP server. I wondered if there is a possibility to add and removes links from and to the blogroll without hardcoding the sidebar.php everytime.
other than this: Excellent idea and explanation
Mark@ VB .Net ListView (1 comments)7 December 09
Is there anyway to bring the content to the top by using CSS. I would like to render the content before any menu or blog roll.
Great tutorial.
Vampal (1 comments)11 December 09
awesome!
thanks for sharing
Beny (1 comments)28 December 09
Amazing!
I don’t ever imagine without Javascript we can still do it. Thanks for sharing. Bookmark now.
Mitko (20 comments)22 January 10
May I ask why are we trying to avoid Java here, beside the point that it might slow down the loading time of the site/blog a little?
Although I use, less to none on my blog (java) sometimes I feel like such tricks are not worth the time.
Arfandia (2 comments)22 January 10
awesome tutorial, i’m glad to find this tutorial
rgds
hadie danker (3 comments)11 February 10
TOO NICE TIPS…
THANK’S FOR SHARE…
Tiff (2 comments)11 February 10
Hey – I found your tutorial while looking for a way to clean up my blog. I’m still in the process, and I added your dropdown code for my friends’ private blogs, but ran into a snag..
I don’t have any issues with jumping content at all. My dropdown appears behind all my other content. Did you ever have this problem? How can I make it appear in front of the content??
Thanks – your tutorial is really well written and easy to follow..
-Tiff
Tiff (2 comments)11 February 10
Oh.. and the dropdown is for the little label that says “Private!” along the side.
Top Wordpress Themes (2 comments)3 March 10
Wow, this is a really nice post. It is not often you find a very helpful post that includes the working preview! Kudos.
I have added the code to my site and will customize it as soon as I get some time….I really love the use of CSS over using something like Sucker Fish. So much simpler and easier to implement. Thanks again!
Made in china (1 comments)5 March 10
I have added the code to my site and will customize it as soon as I get some time….I really love the use of CSS over using something like Sucker Fish. So much simpler and easier to implement. Thanks again!……
Wordpress Themes (2 comments)5 March 10
Wow look at that spam right below my first comment…the decency it takes to copy and paste the previous comment, just for a back-link.
Michael Martin (1319 comments)5 March 10
Gah, that’s a nasty new trick from them. Hopefully something Akismet will be able to auto-detect soon!
Thanks for the heads up though, deleted it now!
And to reply to your original comment; it’s great to hear you liked the tutorial! I think having a working example is an essential for a post like this, you want to see the output before you put the time into working through it!
Abheesh (1 comments)14 March 10
thanx…
nice xelent , am searchng abt to hw to create dropdown menu.
Dziner Fusion (4 comments)18 March 10
Thanks for this tutorial. I think it is pretty stylish, although it would be cool to have it fade in, but that could probably mean javascript!