Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add new line/linebreak character in title attribute in HTML [duplicate]

I am using this

<a href="#" title="select from 1: this 2: that" > Click here </a>

When someone hover over it I see all the text in one line.

Is there any way to show it on new line?

like image 536
user26 Avatar asked Sep 04 '13 06:09

user26


People also ask

How do you add a br tag to a title?

Just use the entity code &#013; for a linebreak in a title attribute.

How do I add a new line in HTML?

To do a line break in HTML, use the <br> tag. Simply place the tag wherever you want to force a line break. Since an HTML line break is an empty element, there's no closing tag. Below is an HTML file with a <p> and <br> element.

How do I Linebreak a new line in tooltip?

Just use the entity code &#xA; for a linebreak in a title attribute.

How do you style title attribute?

You can't style an actual title attribute How the text in the title attribute is displayed is defined by the browser and varies from browser to browser. It's not possible for a webpage to apply any style to the tooltip that the browser displays based on the title attribute.


4 Answers

You can use &#013; or &#010;.

<a href="#" title="select from 1: this &#013; 2: that" >Click here</a>

<a href="#" title="select from 1: this &#010; 2: that" >Click here</a>
like image 131
skulled Avatar answered Oct 10 '22 19:10

skulled


If you are generating it dynamically put < br/> in html part and \n in non html part

$("td").attr("title", "One \n Two \n Three");
$("td").html("One <br/> Two \n <br/>Three <br/>");
$("td").append("One \n Two \n Three");
$("td").append("<br/>One <br/> Two \n Three");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr>
      <td></td>
    </tr>
  <tbody>
</table>

This is using Jquery framework.

like image 31
Gaurav Avatar answered Oct 10 '22 19:10

Gaurav


As you tagged this with accessibility, I would say the most robust approach is not to rely on the title for information.

The current HTML spec discourages this use as people using who cannot use a mouse (e.g. due to mobility impairment) but use a keyboard and see the screen will never see your tooltip/title text. People using touch screens cannot access it.

Most people using a screen reader will not be aware of the title text either, although it is possible to access in most screen readers it is not read out by default, and someone would have to know to look for it. (And there are more issues.)

The spec suggests using this for line breaks:

If the title attribute's value contains "LF" (U+000A) characters, the content is split into multiple lines. Each "LF" (U+000A) character represents a line break.

However, the display of the new lines is dependant on the browser (How can I use a carriage return in a HTML tooltip).

I suspect that the overall UI approach needs a little more thought, as many people would be confused by the sort of instruction in your example, i.e. what does clicking on it achieve if there are multiple options?

If your situation makes it difficult to write good link text then I would post up more about the interaction at the UX stackexchange.

like image 12
AlastairC Avatar answered Oct 10 '22 19:10

AlastairC


<a href="#" title="select from 
1: this 
2: that" >Click here</a>
like image 12
vishalkin Avatar answered Oct 10 '22 19:10

vishalkin