384

I'm currently adding verbose tooltips to our site, and I'd like (without having to resort to a whizz-bang jQuery plugin, I know there are many!) to use carriage returns to format the tooltip.

To add the tip I'm using the title attribute. I've looked around the usual sites and using the basic template of:

<a title='Tool?Tip?On?New?Line'>link with tip</a>

I've tried replacing the ? with:

  • <br />
  • &013; / &#13;
  • \r\n
  • Environment.NewLine (I'm using C#)

None of the above works. Is it possible?

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
penderi
  • 8,673
  • 5
  • 45
  • 62
  • 1
    It's quite tricky, but there might be a workaround. Change all the spaces in your title into non-breaking space ` `. Then put spaces where you want the line breaks. You may also need to add a bunch of ` ` characters before your space (line break). – jumxozizi Nov 25 '16 at 14:29

32 Answers32

351

The latest specification allows line feed characters, so a simple line break inside the attribute or entity &#10; (note that characters # and ; are required) are OK.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kornel
  • 97,764
  • 37
  • 219
  • 309
  • 4
    it's not supported by Chrome but for Firefox fine! – Alaeddine Nov 05 '15 at 09:35
  • 6
    I tried both ` ` and ` `. ` ` would not honor intended "line breaks" in chrome version 50.0.2661.94 (64-bit). ` ` is working well in current versions of chrome, firefox and opera (all for 64-bit Ubuntu) and internet explorer version 11.0 and some change on windows. – Tass May 12 '16 at 21:50
  • This is particularly useful for me because I was trying to do that on Wordpress post editor. – ed1nh0 May 05 '17 at 13:10
  • 18
    works beautifully in IE, Firefox, and Chrome. Thanks! – Daniel May 25 '17 at 16:14
  • For chrome ....... Example: title="title1 title2 title3" – Malik Zahid Jun 20 '19 at 05:26
  • 1
    for me \n\n has worked in all browsers to create one line break – Puschie Jun 15 '21 at 12:09
323

It’s simple: just press Enter!

<a href="#" title='Tool
Tip
On
New
Line'>link with tip</a>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Greg
  • 316,276
  • 54
  • 369
  • 333
  • 1
    @Halcyon, just to clarify for other readers, the above three codes don't seem to be HTML. They look more like C-derived language string escape sequences. – Sam Jan 07 '15 at 01:31
  • 3
    @Sam I don't understand what you mean by that. `\x0A` (byte) is the character-code that corresponds with a newline. Same for `\u000A` (unicode). `\n` is also newline. – Halcyon Jan 07 '15 at 14:02
  • 13
    @Halcyon, I tried inserting those escape sequences into a `title` attribute in Firefox and Chrome, and they were just presented literally in the tooltip. Afterwards, i realised you were probably using that syntax as a way of communicating which characters (not escape sequences) should be used. My comment is for saving other people time by warning them to not try to put your escape sequences in their HTML so they don't waste time like I did. – Sam Jan 07 '15 at 20:53
  • In Chrome (42), the text below the first line has a different, (and for me) less-readable font. – nikodaemus May 01 '15 at 18:21
  • 1
    Not sure if relevant to the comments above, but I've found that while \n and \r do in fact work, if PHP is involved they will only work within strings presented inside of " " and not inside of ' '. – Tarquin May 23 '15 at 00:42
  • 5
    @Tarquin They don't work in HTML, they just work specifically in PHP's double quotes because that's a feature of PHP's double quotes. – Brilliand May 26 '15 at 19:11
  • So are we agreed there is no way to implement in html except a physical new line split in the string? – geotheory Dec 20 '17 at 09:49
  • did things progress regarding compatibility of Firefox Browser yet? (I can't test it right now) – ArchLinuxTux Apr 12 '18 at 11:44
  • This is the solution which works in Svelte, both in `{...}` and `"..."` attribute values. – Nils Lindemann Dec 28 '21 at 11:57
93

Try character 10. Until January 2015 it wouldn't work in Firefox.

The text is displayed (if at all) in a browser dependent manner. Small tooltips work on most browsers. Long tooltips and line breaking work in IE and Safari (use &#10; or &#13; for a new newline). Firefox and Opera do not support newlines. Firefox does not support long tooltips.

http://modp.com/wiki/htmltitletooltips

Firefox now supports using &#13; to insert a line break in an HTML title attribute. See the snippet example below.

<a href="#" title="Line 1&#13;Line 2&#13;Line 3">Hover for multi-line title</a>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stefan Mai
  • 23,367
  • 6
  • 55
  • 61
  • 2
    +1. If you decide to use a jQuery plugin, for optimal accessibility it should read its content from the title attribute and substitute some arbitrary invisible character for a line-break at runtime. – Kent Fredric Dec 11 '08 at 10:19
  • +1. Interesting stuff. There's still the issue of some UAs trimming the title when it pops up - FF2 as I recall but that's less of an issue these days. – roborourke Dec 11 '08 at 10:25
  • 4
    As of January 2015 Firefox does support using ` ` in a `title` attribute to insert a line break. – pseudosavant Feb 01 '15 at 01:38
  • I could not get line breaks to work in IE11 in a SVG element ``. Has anybody had more success with that? Tried the above/below mention char codes, none seem to work. – RemEmber Jun 22 '15 at 14:21
  • What other browsers than Firefox did it work in? – Peter Mortensen Mar 01 '22 at 16:46
69

Tested this in IE, Chrome, Safari, Firefox (latest versions 2012-11-27):
&#13;

Works in all of them...

  • First method (breaking lines in code) seems simpler, but does not stand to auto-formatters js-code programs. – horiatu Jul 24 '15 at 15:05
  • @Camilo Martin, newline and linefeed are the same across platforms. ` ` is the unix linefeed just as it's the windows linefeed. Similarly, ` ` is the newline for both platforms. The [spec](http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#the-title-attribute) specifies the linefeed (LF) char which is of course ` ` on both (all?) platforms. – matty Aug 29 '15 at 07:45
  • 2
    @matty Not sure if I understand you - the correct thing to use seems the unix `LF` (which is kinda standard linefeed across protocols and tools), `CR` was only used by old Macs (and other obscure platforms) and seems out of place. – Camilo Martin Aug 30 '15 at 09:26
  • I apologize for my confusing word choice. What I called a linefeed (#10) is really a newline, although often referred to as LF. What I called a newline (#13) is really a carriage return. In any case, my point was that those values are the same across all platforms. The fact that windows (using both of these characters to end a line) and mac (using the "wrong" character to end a line) should be irrelevant in regards to which HTML entity is to be used in a title attribute. The spec says ` `, although apparently ` ` works in many circumstances as well. – matty Aug 31 '15 at 06:23
  • If anyone else needs a refresher on these character codes, like I did, see [this thread] (http://stackoverflow.com/q/1761051/1298086). – matty Aug 31 '15 at 06:23
  • 4
    plain does not work for chrome on Linux. The sequence does, however. – Sam Watkins Jan 01 '16 at 12:32
  • No luck with IE11 with – Adamy Jul 04 '16 at 02:11
62

Also worth mentioning, if you are setting the title attribute with JavaScript like this:

divElement.setAttribute("title", "Line one&#10;Line two");

It won't work. You have to replace that ASCII decimal 10 with a ASCII hexadecimal A in the way it's escaped with JavaScript. Like this:

divElement.setAttribute("title", "Line one\x0ALine two");

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cprcrack
  • 17,118
  • 7
  • 88
  • 91
32

As of Firefox 12 they now support line breaks using the line feed HTML entity: &#10;

<span title="First line&#10;Second line">Test</span>

This works in IE and is the correct according to the HTML5 spec for the title attribute.

amurra
  • 15,221
  • 4
  • 70
  • 87
18

As a contribution to the &#013; solution, we can also use &#009 for tabs if you ever need to do something like this.

<button title="My to-do list:&#013;&#009;-Item 2&#013;&#009;-Item 3&#013;&#009;-Item 4&#013;&#009;&#009;-Subitem 1">TEST</button>

Demo

enter image description here

Community
  • 1
  • 1
Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
17

If you are using jQuery:

$(td).attr("title", "One \n Two \n Three");

will work.

It was tested in Internet Explorer 9: working.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anil Bharadia
  • 2,760
  • 6
  • 34
  • 46
  • 1
    Worked in MVC 3 Example: @Html.DropDownList("RegId", null, "Select Region", new { title = "Select a region from the drop-down menu, \nwhich is required to get a list of valid contract names", @class = "form-control", onchange = "getContractNames(this)", style = "width: 200px;" }) – Warren LaFrance Nov 04 '15 at 19:12
17

On Chrome 79, &#13; does not work anymore.

I was successful with:

&#13;&#10;

This works in all major browsers.

David Vielhuber
  • 3,253
  • 3
  • 29
  • 34
10

&#13; will work on all majors browsers (IE included)

alexandre-rousseau
  • 2,321
  • 26
  • 33
Greg Dean
  • 29,221
  • 14
  • 67
  • 78
8

&#xD; <----- This is the text needed to insert a carriage return.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Juan
  • 89
  • 1
  • 1
8

Here's a demo: http://jsfiddle.net/rzea/vsp6840b/3/

HTML used:

<a href="#" title="First Line&#013;Second Line">Multiline Tooltip</a>
<br>
<br>
<a href="#" title="List:
  • List item here
  • Another list item here
  • Aaaand another list item, lol">Unordered list tooltip</a>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ricardo Zea
  • 10,053
  • 13
  • 76
  • 79
7

We had a requirement where we needed to test all of these, and here is what I wish to share:

document.getElementById("tooltip").setAttribute("title", "Tool\x0ATip\x0AOn\x0ANew\x0ALine")
<p title='Tool
Tip
On
New
Line'>Tooltip with <pre>
  new
  line</pre> Works in all browsers</p>
<hr/>

<p title="Tool&#13;Tip&#13;On&#13;New&#13;Line">Tooltip with <code>&amp;#13;</code> Not works Firefox browsers</p>
<hr/>

<p title='Tool&#10;Tip&#10;On&#10;New&#10;Line'>Tooltip with <code>&amp;#10;</code> Works in some browsers</p>
<hr/>

<p title='Tool&#x0aTip&#x0aOn&#x0aNew&#x0aLine'>Tooltip with <code>&amp;#xD;</code> May work in some browsers</p>
<hr/>

<p id='tooltip'>Tooltip with <code>document.getElementById("tooltip").setAttribute("title", "Tool\x0ATip\x0AOn\x0ANew\x0ALine")</code> May work in some browsers</p>
<hr/>


<p title="List:
  • List item here
  • Another list item here
  • Aaaand another list item, lol">Tooltip with <code>• </code>Unordered list tooltip</p>
<hr/>


<p title='Tool\nTip\nOn\nNew\nLine'>Tooltip with <code>\n</code> May not work in modern browsers</p>
<hr/>

<p title='Tool\tTip\tOn\tNew\tLine'>Tooltip with <code>\t</code> May not work in modern browsers</p>
<hr/>

<p title='Tool&#013;Tip&#013;On&#013;New&#013;Line'>Tooltip with <code>&amp;#013;</code> Works in most browsers</p>
<hr/>

Fiddle

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
7

This &#013; should work if you just use a simple title attribute.

On Bootstrap popovers, just use data-html="true" and use HTML in the data-content attribute.

<div title="Hello &#013;World">hover here</div>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
csandreas1
  • 2,026
  • 1
  • 26
  • 48
6

Just use this:

<a title='Tool&#x0aTip&#x0aOn&#x0aNew&#x0aLine'>link with tip</a>

You can add new line on title by using this &#x0a.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
6

I don't believe it is. Firefox 2 trims long link titles anyway and they should really only be used to convey a small amount of help text. If you need more explanation text I would suggest that it belongs in a paragraph associated with the link. You could then add the tooltip JavaScript code to hide those paragraphs and show them as tooltips on hover. That's your best bet for getting it to work cross-browser IMO.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
roborourke
  • 12,147
  • 4
  • 26
  • 37
6

As for whom &#10; didn't work for, you have to style the element on which lines are visible in as: white-space: pre-line;

It is referenced from GuitarWorker's answer to Add line break within tooltips.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Youans
  • 4,801
  • 1
  • 31
  • 57
6

On Chrome 16, and possibly earlier versions, you can use "\n". As a side note, "\t" also works.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rehan
  • 1,299
  • 2
  • 16
  • 19
5

Just use JavaScript. Then it is compatible with most and older browsers.

Use the escape sequence \n for newline.

document.getElementById("ElementID").title = 'First Line text \n Second line text'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Welshboy
  • 318
  • 4
  • 10
3

Much nicer looking tooltips can be created manually and can include HTML formatting.

<!DOCTYPE html>
<html>
<style>
.tooltip {
    position: relative;
    display: inline-block;
    border-bottom: 1px dotted black;
}

.tooltip .tooltiptext {
    visibility: hidden;
    width: 120px;
    background-color: #555;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 5px 0;
    position: absolute;
    z-index: 1;
    bottom: 125%;
    left: 50%;
    margin-left: -60px;
    opacity: 0;
    transition: opacity 0.3s;
}

.tooltip .tooltiptext::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: #555 transparent transparent transparent;
}

.tooltip:hover .tooltiptext {
    visibility: visible;
    opacity: 1;
}
</style>
<body style="text-align:center;">

<h2>Tooltip</h2>
<p>Move the mouse <a href="#" title="some text
more&#13;&#10;and then some">over</a> the text below:</p>

<div class="tooltip">Hover over me
<span class="tooltiptext">Tooltip text
some <b>more</b><br/>
<i>and</i> more</span>
</div>

<div class="tooltip">Each tooltip is independent
<span class="tooltiptext">Other tooltip text
some more<br/>
and more</span>
</div>

</body>
</html>

This is taken from the W3Schools post on this. Experiment with the above code here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris
  • 5,664
  • 6
  • 44
  • 55
3

According to this article on the W3C website:

CDATA is a sequence of characters from the document character set and may include character entities. User agents should interpret attribute values as follows:

  • Replace character entities with characters,
  • Ignore line feeds,
  • Replace each carriage return or tab with a single space.

This means that (at least) CR and LF won't work inside the title attribute. I suggest that you use a tooltip plugin. Most of these plugins allow arbitrary HTML to be displayed as an element's tooltip.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • 1
    Yeah, I know that it's not official. You are wrong, hwoever. It doesn't mean they won't be displayed. It means the W3C spec doesn't state how they should be displayed... . The point of the post wasn't to suggest they could be used to replace more functional plugins.... it was more of a case of during degradation can anythign else be used. – penderi Sep 12 '11 at 13:48
  • @ip: I am wrong in that that updated w3c recommendations *suggest* that browsers split the content around the LF character. However browsers will implement this behavior eventually. – Salman A Sep 14 '11 at 04:23
2

Razor syntax

In the case of ASP.NET MVC, you can just store the title as a variable, as \r\n, and it'll work.

@{
    var logTooltip = "Sunday\r\nMonday\r\netc.";
}

<h3 title="@logTooltip">Logs</h3>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shelby115
  • 2,816
  • 3
  • 36
  • 52
2

There are multiple ways to do this. &#13 or &#010; depending on the browser. But the following example works for me on my Google Chrome and Firefox.

<a href="javascript:;" title="Tooltip Line One &#10;Tooltip Line Two &#10;Tooltip Line Three">Tooltip</a>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
john2002
  • 39
  • 2
1

I was directed to this page while searching for a solution to display breakline in Bootstrap tooltips. Bootstrap tooltips are displayed when data-toggle="tooltip" is added to a HTML tag.

Finally, I found out that a data-html="true" should be added to the tag, and doing so the HTML inside your title will be rendered. Then use <br> to breakline. Checkout the samples below:

Before (not showing any breakline):

<i class="bi bi-x-circle" data-toggle="tooltip" data-placement="top" title="test1 <br> test2"></i>

After (showing breakline):

<i class="bi bi-x-circle" data-toggle="tooltip" data-html="true" data-placement="top" title="test1 <br> test2"></i>

- Tip for Laravel and Blade programmers: You can convert \n to <br> with nl2br("string") function.

- The solutions above have been tested in Google Chrome 98.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mostafa Arian Nejad
  • 1,278
  • 1
  • 19
  • 32
1

Using \x0A worked for me to get newline in title/tooltip on mouse hover.

FILENAME: news.php

<span onmouseover="getInfo(this,'06-APR-22')" data-html="true">Day</span>

FILENAME: news.php

<script>
function getInfo(x, dateVal) {       
      var html = $.ajax({
            type: "POST",
            url: "report/get-info.php",
            data: {             
                'dateVal': dateVal,
            },              
            success: function(response) {
                x.setAttribute('title', response );
            }
        });
}
</script>

FILENAME: get-info.php

<?php
$tmp  = $_POST['dateVal'];   
$dateVal = date("m/d/Y", strtotime($tmp));

$querySelectCode = "SELECT INFO_ID, INFO_NAME FROM INFORMATION
                    WHERE TRUNC(DP.DATE_TIME) = TO_DATE('$dateVal','MM/DD/YYYY') ";    
$stmtSelectcode = oci_parse($dbConn, $querySelectCode);
if ( ! oci_execute($stmtSelectcode) ){
    $err = oci_error($stmtSelectcode);
    trigger_error('Query failed: ' . $err['message'], E_USER_ERROR);
}
$INFO_ID = array();
while(oci_fetch($stmtSelectcode)){
    $INFO_ID[]   =  oci_result($stmtSelectcode, 'INFO_ID');
    $INFO_NAME[]     =  oci_result($stmtSelectcode, 'INFO_NAME');        
}
for($index=0 ; $index < count($INFO_ID) ; $index++)
{
    echo   $INFO_NAME[$index ] . "\x0A" ;        
}
?>
Zahid Rouf
  • 1,611
  • 2
  • 12
  • 10
0

It is a hack, but it works - (tested on Google Chrome and mobile)

Just add non-breaking space, &nbsp;, till it breaks - you might have to limit the tooltip size depending on the amount of content but for small text messages this works:

&nbsp;&nbsp; etc

I tried everything above and this is the only thing that worked for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fstarocka Burns
  • 163
  • 1
  • 5
0

I have tried with &#10; to display the title text on a new line and it’s working like a charm.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Niket Joshi
  • 739
  • 5
  • 23
0

I am on Firefox 68.7.0 ESR and the &#013; doesn't work. Breaking the lines via <CR> did work, so I am going with that since it simple and forward.

I.e.,

<option title="Constraint PSC/SCS/Activity
Definition Constraint Checker
Database Start Notifier">CnCk
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3416126
  • 148
  • 11
0

The "just press enter" solution didn't work here, so the good old vanilla JavaScript seems a pretty efficient and clean way.

function customTitle(event, textHeader, text){
    let eventOrigin = event.target || event.srcElement;
    eventOrigin.title = textHeader + '\n\n' + text;
}

And on element onmouseover:

onmouseover="customTitle(event, 'Some Caput', 'Some more or less complete description');"

Voilà! It works on Google Chrome and Firefox (which does not exclude others; I just didn't check it).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Luís Alves
  • 81
  • 2
  • 4
0

If you are trying to do this in a React project where you are rendering in the below format:

title={`First line of text ${varOne} &#13; Second line of text ${varTwo} &#13; Third line of text ${varThree}`}

Then &#13;, &#10; and similar solutions don't work. They will actually render as text.

Instead actually creating the text as required is a better option. An example for better understanding:

title={`First line of text ${varOne}
Second line of text ${varTwo}
Third line of text ${varThree}`}

Make sure to remove tabs/space indentations before "Second line of text" and "Third line of text". Otherwise they will render as well.

It was tested on Google Chrome 96 and Firefox 95.0b12 (Developer Edition, because, well why not). It should work on most modern browsers as well.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Siddhesh T
  • 406
  • 1
  • 5
  • 14
0

Just sharing small bug I saw:

In Chrome 103 with this html:

<!DOCTYPE html>
<html>
<body>
  <select title="Line 1 :&#13;&#13;Line 2">
    <option value="0">test</option>
  </select>
</body>
</html>

I see this:

tittle chrome

On screenshot - two weird characters after "Line 1 :" text, first line.

Fix to this: (I have no idea which is correct/valid but all of them works as fix (in Chrome 103))

  • Adding space(character) after Line 1 : before first &#13; fix it
  • Changing first &#13; to &#10; full line title="Line 1 :&#10;&#13;Line 2"
  • Adding &nbsp; after Line 1 : before first &#13;
  • Also changing &#13; to "Carriage Return symbol" will fix it
Danil S
  • 67
  • 7
-2

Use data-html="true" and apply <br>.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nithin C
  • 215
  • 1
  • 3
  • 12