Unlabelled search fields
Adam Silver is writing a book on forms���you may be familiar with his previous book on maintainable CSS. In a recent article (that for some reason isn’t on his blog), he looks at markup patterns for search forms and advocates that we should always use a label. I agree. But for some reason, we keep getting handed designs that show unlabelled search forms. And no, a placeholder is not a label.
I had a discussion with Mark about this the other day. The form he was marking up didn’t have a label, but it did have a button with some text that would work as a label:
Search
He was wondering if there was a way of using the button’s text as the label. I think there is. Using aria-labelledby like this, the button’s text should be read out before the input field:
Search
Notice that I say “think” and “should.” It’s one thing to figure out a theoretical solution, but only testing will show whether it actually works.
The W3C’s WAI tutorial on labelling content gives an example that uses aria-label instead:
Search
It seems a bit of a shame to me that the label text is duplicated in the button and in the aria-label attribute (and being squirrelled away in an attribute, it runs the risk of metacrap rot). But they know what they’re talking about so there may well be very good reasons to prefer duplicating the value with aria-label rather than pointing to the value with aria-labelledby.
I thought it would be interesting to see how other sites are approaching this pattern���unlabelled search forms are all too common. All the markup examples here have been simplified a bit, removing class attributes and the like…
The BBC’s search form does actually have a label:
Search the BBC
Search the BBC
But that label is then hidden using CSS:
position: absolute;
height: 1px;
width: 1px;
overflow: hidden;
clip: rect(1px, 1px, 1px, 1px);
That CSS���as pioneered by Snook���ensures that the label is visually hidden but remains accessible to assistive technology. Using something like display: none would hide the label for everyone.
Medium wraps the input (and icon) in a label and then gives the label a title attribute. Like aria-label, a title attribute should be read out by screen readers, but it has the added advantage of also being visible as a tooltip on hover:
This is also what Google does on what must be the most visited search form on the web. But the W3C’s WAI tutorial warns against using the title attribute like this:
This approach is generally less reliable and not recommended because some screen readers and assistive technologies do not interpret the title attribute as a replacement for the label element, possibly because the title attribute is often used to provide non-essential information.
Twitter follows the BBC’s pattern of having a label but visually hiding it. They also have some descriptive text for the icon, and that text gets visually hidden too:
Search query
Emil did some testing. Looks like all screen-reader/browser combinations will read the associated text.
Jeremy Keith's Blog
- Jeremy Keith's profile
- 55 followers
