views:

1522

answers:

5

How can I hide an HTML form legend from visual browsers, using CSS, in an accessible way?

legend { display: none; }

is not an option because, as I understand it, this will 'hide' the legend from screen readers. Other attempts I've made do not remove the legend from the layout - i.e. it continues to take up space.

A: 

You could try:

legend
{
    position: absolute;
    top: -1000px;
}
Greg
I did - didn't work. That doesn't change the legend's position at all, at least, not in Firefox. I don't know why that is - something 'special' about the legend element?
Bobby Jack
This won't work in Firefox.
rahul
If you really want legends, have you tried putting a span inside the legend and positioning/manipulating that?
edeverett
@edeverett: I hadn't; works in IE7 and Firefox, though. Care to add this as an answer - it might be the one I accept.
Bobby Jack
+2  A: 

You can't do this in Firefox because it is a bug in the browser.

You can read more here

Browser Bugs

rahul
Great resource - thanks. Guess I'm stuck with "display: none".
Bobby Jack
Ok. Done. Thanks
rahul
+1  A: 

For what it's worth - and I'm sure I'll get flamed for this - legend tags are one of the few places I deliberately break the spec by leaving them out. I replace them with a heading of the appropriate level which provides the same information to the user but without the browser bugs.

(I'm happy to hear about the real-world downsides of this if anybody can see some)

edit: Oh and you should ask yourself why assistive technology users would want to hear the legends when your browser using users don't. If the answer is simply to satisfy the HTML specs, use display:none and be done with it - don't hinder the user experience of one group by providing useless information just for a formality.

edeverett
Interesting approach. I can, of course, see the benefits, although I'm not sure how screen readers would react. I guess you COULD also add an empty legend if validation is required; I find it useful to be 100% valid, if only to avoid thinking about whether I'm invalid in an 'acceptable' way or not.
Bobby Jack
"to avoid thinking about whether I'm invalid in an 'acceptable' way or not." That is a very good reason, but legends seem like a bad bit of spec and badly implemented in browsers, so I reckon it's not worth my time in this case. (I updated my answer with a bit more of my thinking while you were leaving the comment.)
edeverett
Very good point - I'll have to think about that one! Not sure if this question might end up being 'invalid' or if there's ever a justifiable reason for doing what I thought I needed to do ...
Bobby Jack
+2  A: 

Added as an answer instead of a comment so I can get more points. :-)

If you really want legends, have you tried putting a span inside the legend and positioning/manipulating that?

I understand this works in IE7 and Firefox...

edeverett
Although it's not perfectly 'semantically clean', this does appear to be the best option so far - upvoted.FYI, I don't know how serious you're being, but adding this as a real answer is perfectly valid and altruistic: others will benefit from this information, and that's what SO is all about!
Bobby Jack
I'm only being semi seriously sarcastic. I try to only really add answers that I *know* worked. This was originally an off the cuff idea - until you confirmed it worked - so I don't feel like it is fully mine. I am however glad it work (I've learnt something too) and that the information is now available here for others. It's all good :-)
edeverett
A: 

Yes, there's something special about it. It's a replaced element like many form elements. Browsers have a very specific default formatting. Moreover it can't be forced to behave like a regular element using display:block or display:inline, causing attempts to override with CSS to ... not work well.

There are some well documented techniques that can help you accomplish SOME effects with legends, though workarounds are necessary for a semblance of cross-browser compatibility.

Many versions of Firefox specifically ignore both display:none and absolute positioning.

absolethe