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.
Since learning about the May 2007 update of the WCAG 2.0 Working Draft, which I wrote about it WCAG 2.0 Working Draft updated, I've been trying to find time to read the whole thing in order to be able to write a more thorough piece on it. However, because of the size of the WCAG 2.0 documentation I just haven't been able to get through it yet.
You don't have to wait until I manage to read it, understand it, and write something about my opinion on it though. Jack Pickard has already done that work, and presents his verdict in WCAG 2.0: Woeful to Wonderful in One Easy Draft?, an article split into three pages (WCAG is a large document, so commenting on it may require using quite a few words after all).
Judging by the result of Jack's assessment, the updated WCAG 2.0 Working Draft is a vast improvement over the Working Draft released in April 2006:
I was critical of WCAG 2.0 before, and it deserved that criticism. Now, I'm prepared to praise it, because it deserves that praise.
What I've read of WCAG 2.0 so far is definitely much better than the April 2006 Working Draft, but I'm not going to stand up and praise it before I have read it thoroughly.
Visit site to read or post comments…Add 456 Berea Street to your Technorati favorites.
Posted in Accessibility.
As most designers and developers who use CSS on a daily basis know, it's been a while since a new CSS Recommendation was released. There are several possible reasons for it taking so long, something Andy Budd brings up in CSS2.2.
While I agree that the W3C's process is way too slow, I don't think we can lay all the blame on the W3C. Browser vendors are a big part of the problem. Especially one of them (you-know-who).
Of course there are many neat features in CSS 3 that I would love to be able to use now, but just imagine for a moment that all browsers had full support for CSS 2.1. They really should, considering how long they have had to implement it. Don't you think that would be a great first step forward, and that it would open up for completely new approaches to designing with CSS?
I do. So I think the first thing is to get CSS 2.1 fully implemented. How to make you-know-who do that, I have no idea.
In the meantime, some people could form the 2nd Generation CSS Samurai and write a CSS 2.2 specification in the hopes that it will be adopted, or at least put some pressure on the W3C and browser vendors.
Visit site to read or post comments…Add 456 Berea Street to your Technorati favorites.
Posted in CSS.
As someone who has spent lots of time taking screenshots of various CSS applied to form controls, I know that styling form controls consistently across browsers and operating systems is impossible. If you don't know what I'm talking about, have a look at Styling form controls, Styling even more form controls, and Styling form controls with CSS, revisited.
In the discussions following those articles (and any article that discusses styling of form controls), there tends to be a bit of frustration expressed by designers who feel that being able to specify exactly how form controls appear in a graphical browser is necessary for their design to work well. I really cannot agree with that. In my opinion form controls should be left mostly alone (though some light styling may be acceptable) in order for the user to quickly recognise them for what they are.
Regardless of whether or not you agree with my opinion on this you will probably find Eric Meyer's article Formal Weirdness a good read. In the article, Eric explains some of the technical reasons for form controls being so hard to style consistently across platforms with CSS. He also asks a lot of good questions related to how various CSS properties should affect form controls if browsers would let them.
Eric wrote the article as a follow-up to his series of articles about resetting the default CSS in browsers in order to get a consistent, cross-browser base stylesheet. His reset stylesheet evolved over several articles, and the final version is described in Reset Reloaded. Several people posted comments asking why Eric did not use a universal selector to set the margin and padding properties of all elements to zero. Eric's response to that is that he avoided doing so because of the problems it causes for form controls.
After reading Formal Weirdness I think you will understand why.
Visit site to read or post comments…Add 456 Berea Street to your Technorati favorites.
Posted in CSS.
A CSS problem I have been wrestling with lately is how to create a bulletproof shrinkwrapping graphic button. By that I mean an image-based button that will expand and contract to fit the amount of text it contains. It is a very useful technique for CMS-driven sites that allow the client to change the text that is displayed on buttons, as well as for multilingual sites.
A successfull bulletproof image-based button should:
Doing this with CSS may sound easy at first, but there are several things to be considered that make it a little tricky. I also ran into several browser related problems before ending up with the solution described in this article.
Before I go on to describe the problems and the solution, I'd like to give you a friendly reminder that this technique should not be used to mindlessly replace real form buttons.
A real form button (normally <input type="submit">) works equally well without CSS and JavaScript. A styled link, however, can only submit a form through the help of some JavaScript. And since You cannot rely on JavaScript being available. Period., you need to take that into account. What I did in the project I came up with this solution for was to use JavaScript to replace the input element with one of these buttons. That way, if JavaScript is not available, the user can click a real button instead.
OK, let's move on. The problems, as always when it comes to CSS, occur in Internet Explorer. The first problem is one that I'm not actually sure whether it is a bug or just a different implementation of the specification. I mentioned the problem in Transparent custom corners and borders, version 2, and it consists of IE having problems figuring out the width of block level elements inside floated elements with no width assigned.
When a floated element has no specified width, the browser should adjust the element's width to fit its content – shrinkwrapping. In the case of a floated link acting as a button, that would come in handy as it will make the link automatically adjust its width depending on the amount of text it contains. Sadly, Internet Explorer will not properly shrinkwrap the following combination of HTML and CSS I use for the Transparent custom corners and borders technique.
When div.cb is floated without a specified width, it will shrinkwrap to fit its content. The width depends on the content of div.i1, and that part works fine. The problem is with the div.bt and div.bb elements, which don't expand to the same width as div.i1. That kind of makes sense, since they are empty siblings of div.i1. A workaround is to give div.cb a width, but I really needed shrinkwrapping buttons, so I had to to come up with something else.
I ran into the second problem while exploring other solutions and experimenting with absolutely positioning the corners. That actually worked, except for a one pixel gap that showed in IE at certain sizes, probably due to a rounding error.
What I ended up with is a link that contains four nested span elements. Here's the markup for a button (nicely indented here for readability):
Yes, I know. That is a lot of span elements. It isn't pretty, but I can't think of a better way to achieve this effect until more browsers than Safari support Layering multiple background images. In a production situation I would use a script similar to the one in Transparent custom corners and borders, version 2 to remove the span elements from the markup. The end result is the same to the browser, but the markup is cleaner for us humans who have to edit it.
And of course we need a bit of CSS to turn that markup into a nice-looking button:
You'll need to adjust the paddings to fit your image if it has wider or narrower corners and edges.
Oh, I made a demo page that contains a few buttons.
Ideally, this kind of button would be accomplished by using a single background image, so that was my initial goal. I did not manage to reach that goal, however, but had to resort to using two images. The two images are button.gif and corners.gif. If you take a look at them, you will notice that they are much larger than any button should become since they define the maximum size of the button. If you need even larger buttons, just increase the size of the images.
Note that corners.gif is transparent except for in the corners. That way it can be used to put each corner in place without covering any of the other corners.
And that's it. A bulletproof, resizable, shrinkwrapping graphic button.
There are a few caveats though:
To me, those are completely acceptable drawbacks in most situations.
So, does anyone see how this could be further improved?
Update (2007-05-23): The class attributes on the span elements are gone. I was sure I had a reason for using classes instead of descendant selectors at the time, but I can't see the need for them now. So no more classitis, and a little leaner markup.
Visit site to read or post comments…Add 456 Berea Street to your Technorati favorites.
Posted in CSS.
On April 1 I posted WCAG 2.0 released today, which of course was an April Fool's joke. But this time I am not joking: WCAG 2.0 has been updated, and a new WCAG 2.0 Working Draft was published on May 17.
The Web Content Accessibility Guidelines Working Group (WCAG WG) received a lot of criticism (read To Hell with WCAG 2 for details) after publishing a Last Call Working Draft of WCAG 2.0 in April 2006, and appears to have listened to much of the feedback from the accessibility community.
A rundown of what has changed since the previous version is available in Summary of Issues, Revisions, and Rationales for Changes to WCAG 2.0 2006 Last Call Draft.
I have not read the whole thing yet. WCAG 2.0 has been shortened and reorganised, but it still consists of a number of documents, some of which are quite long. It will be a while before I have time to read all of it. I have read the summary of changes and skimmed through the Techniques for WCAG 2.0 document though, and what I've seen so far really seems like an improvement.
Like last year, the WCAG WG has issued a Call for review and is accepting comments on this Working Draft until June 29. Instructions for Commenting on WCAG 2.0 Documents are available.
Visit site to read or post comments…Add 456 Berea Street to your Technorati favorites.
Posted in Accessibility.