Everybody makes mistakes, so occasionally things go wrong. One example is when Swedish design magazine Cap&Design recently published a tutorial on creating a collapsing and expanding menu.
While reading the article my jaw dropped as I realised that the tutorial uses invalid, non-semantic HTML, inline CSS and event handlers, bloated (but, amazingly, valid) CSS, and obtrusive JavaScript. In 2007.
I sent an email to the magazine's editor, who agreed that the article shouldn't have been printed and took the example code offline. Thank you very much for doing that – hopefully it will limit the number of people using the code in their projects.
Instead of just complaining about the lousy code and deriving satisfaction from making them take the code offline, I decided to recreate the menu from the tutorial in a more modern way, using leaner and more efficient HTML and CSS, and unobtrusive JavaScript. This kind of menu isn't anything new, of course. Nevertheless I think publishing this comparison of the two approaches may be useful.
The HTMLThe HTML that appeared in the article is this (shortened, and with id values translated from Swedish):
This markup is problematic for several reasons:
I replaced the HTML with this:
The menu now consists of nested lists of links, the inline CSS and event handlers are gone, and a class name is used to allow for more than one menu on a page, should there be a need for that. Should the browser lack support for JavaScript, all sub menus will now be displayed, and the user is not prevented from navigating the site. Much better in my opinion.
The CSSNext a look at the CSS used in the article (id selectors translated from Swedish):
While that CSS is not invalid, it is definitely not optimal, for the following reasons:
Here is what I replaced it with:
I removed the font declarations since font-family and font-size are normally specified elsewhere in the CSS. While I was at it I also changed the colours to make looking at the menu a bit easier on my eyes, and added some padding and a bottom margin to the links.
The JavaScriptFinally a look at the JavaScript. The article uses the following (variable names translated from Swedish):
The problems with this script are:
Ok, I'll admit that the script does have an advantage over my replacement: it is a lot shorter than this:
The replacement script works by adding or removing a class name instead of directly changing the style property of the sub menu elements.
The addEvent() and getElementsByClassName() functions are included in the full togglemenu.js script. If you use a JavaScript library that contains similar functions (they all do, probably) I suggest using those functions instead to avoid code bloat.
You can try it all put together on the Accessible expanding and collapsing menu demo page.
Caveats and some homework for the readerLike I said at the beginning of this article, this has been done before. But hopefully showing the before and after versions of the HTML, CSS, and JavaScript will make it easier to see why I made the changes.
I should also note that this script does inherit a problem from the script published in the magazine: it disables the links to the top level category pages, preventing the user from navigating to them. That could be a serious problem depending on how the site is structured.
This script also does not handle multiple levels. Extending it to allow for that is no big deal, but I'd think twice before doing so. Remember that there will be a lot of links for people with JavaScript disabled to wade through.
I'll leave it as a learning exercise for the reader to alter the script so that it inserts a separate link for expanding and collapsing each sub menu and lets the top level links behave normally. And, if you want the functionality, add support for multiple levels.
Update (2007-05-30): As has been mentioned in a couple of comments, this kind of menu can also cause usability issues, making users confused when clicking a link does not open a new page. Be aware of that, and I would suggest at least styling the links in a way that makes it obvious that they contain hidden information.
Visit site to read or post comments…Add 456 Berea Street to your Technorati favorites.
Posted in JavaScript.
(Last Google Reader theme post, I promise)
Before now, I’ve felt quite protective of my site skins, and have always stated “Feel free to modify the CSS to suit your own tastes, but please don’t redistribute”. This stems from vanity to be truthful – I spend a long time creating these themes, and don’t want other people getting the credit!
That was how I used to think though. As time goes on, I feel like I’m becoming more communist open when it comes to the web. Besides, I have little time to update the themes, and carry out feature/bug requests, and they’re both at a point where I’m happy with them. Sure, there’s so much you could do to them, but the original intention was to create something for myself. Which is where you come in.
These themes are now both “do what you like with them”. Take out the bits you don’t like, put in new bits, whatever. Feel free to re-distribute them anywhere you like, whether slightly tweaked or massively altered. They’re yours to do with as you please! Give them a good home and let me know what do with them (hopefully something better!)
downloadsGoogle Reader Theme
Bloglines Theme for Webkit & Opera
Bloglines Theme for Mozilla
Finally, to answer a much-asked question I don’t have plans to do a GMail or GCal skin, but you never know.
The International Olympic Committee (IOC) has asked Chicago to re-design its 2016 Olympic logo.
Last October, Chicago officials unveiled a logo as the visual element of their 2016 Olympic bid. The logo featured a torch with a flame that resembled the city’s skyline. It was designed to commemorate Chicago’s architectural style, while focusing on the idea of “a city that rose from the ashes” during the 1871. The orange flame looked a lot like the famous Sears Tower, whereas, the colored blue and green torch’s body represented Lake Michigan and the city’s park system including Millennium Park, respectively.
Created by Chicago-based VSA Partners Inc., the 2016 Chicago Olympic logo was featured on billboards and various merchandises including T-shirts, before it was finally turned down by the IOC earlier this week.
According to the officials of IOC, Chicago’s logo was rejected because the IOC rules stated that the candidate city logos should not “contain the Olympic symbol, the Olympic motto, the Olympic flag, and/or any other Olympic-related imagery, such as flame, torch, medal, etc.” This is in accordance to protect and advance the Olympic movement.
Chicago’s officials heartily accepted the proposal of the IOC, as it was done in the best interest of the Olympic Games. Patrick Sandusky, a spokesman for Chicago 2016, commented: “Our logo was fantastic for the domestic phase. It talked about our lake, our skyline, and our parks. We still could have that in our new logo. We just have to spend time thinking about what the best logo would be (going forward). We’re confident that we’ll have an equally fantastic logo for the international phase.”
For now, we have to wait and see what the designers will come up with this time. All eyes and hopes will be on the launch of the new Chicago 2016 logo due later this year. We, too, hope that the new logo incorporates the creativity and style of the previous one, while also fulfilling the IOC requirements.
Chicago is a candidate in the United States bid for the 2016 Olympic Games. The final selection will be made by the International Olympic Committee on October 2, 2009 in Copenhagen, Denmark from a bunch of international contenders.