Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make <legend> text wrap?

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?

like image 339
Wesley Murch Avatar asked May 26 '11 04:05

Wesley Murch


People also ask

How do I set word wrapping for a legend?

They wrap to the total width of the item. Legend titles also wrap to the total width of all the columns. To set word wrapping, complete the following steps: In the Contents pane, right-click the legend and click Properties to open the Element pane. Be sure to click the legend element, not a legend item.

How do I wrap text items in word?

In the Element pane, click the Legend Arrangement Options tab. Under the Word wrapping heading, check Labels or Descriptions to wrap the appropriate text item. Adjust the value in page units for the text to wrap by changing the number next to Labels or Descriptions. For example, set it to wrap all labels wider than 1 inch.

How do I Turn on the legend title text?

The default legend title text is Legend and is turned off. You can turn on the legend title, and change the title text by completing the following steps: In the Contents pane of a layout, right-click the legend and click Properties to open the Element pane. Be sure to click the legend element, not a legend item.

Is it possible to wrap legend names around text in Power BI?

My legends are already on the left but the legend names are very long , so I want it to wrap around so it’s displayed in 2 lines instead of 1. Is this possible in power bi? Don’t think there is a way to do it. Power BI is missing lots of useful text wrap options (see also wrapping Y axis in bar charts).


1 Answers

Adding white-space: normal; to the legend works fine except in IE7 and IE6. Please see this jsfiddle demo

After 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:

legend {
    white-space: normal; 
    width: 100%;
    *margin-left: -7px;
}
legend span {
    display:block;
    width: 100%;
}

Please have a look at this jsfiddle

like image 113
Galeel Avatar answered Oct 17 '22 09:10

Galeel