Usually <legend> text is pretty short so I had no idea this was a problem until I ran into it yesterday. I was trying and failing to set a 50% width on a <fieldset>, but it wouldn’t work due to long legend text. Either the fieldset won’t be smaller than the legend, or the legend’s width exceeds that of the fieldset.
This doesn’t seem to be an issue in IE8, Chrome, Safari, Opera, and maybe others. It is an issue in Firefox, IE6, and IE7.
Goal: Get text to wrap in a <legend> cross-browser
- Without setting any fixed widths
- Hopefully without extra markup
- Without javascript
- Any way we can if the above are impossible
- Without giving up and using a different tag
I’ve seen this post: Getting LEGEND tags to wrap text properly
…But there is only a single answer that uses a <div> with a fixed width inside the legend tag, I can’t actually get it to work (see fiddle), and OP closed with the comment “in the end we gave up”. Googling this subject turns up a whole lot of “not much” as well.
I put up a jsfiddle demo with some CSS I’ve tried. As I said, I’ve never run into this before so I’m baffled that this is so difficult, and I can’t seem to get anything to work. Is it really just impossible?
Adding
white-space: normal;to the legend works fine except in IE7 and IE6. Please see this jsfiddle demoAfter playing around a bit with the CSS, I got it work on IE7, IE8, IE9, FF3-4, and Chrome11 by adding a
<span>inside the<legend>with the below CSS:Please have a look at this jsfiddle