Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling native tooltip from title="Tooltip text" [duplicate]

Tags:

html

css

Example:

<a href="example.com" title="My site"> Link </a> 

How do I change the presentation of the "title" attribute in the browser?. By default, it just has yellow background and small font. I would like to make it bigger and change the background color.

Is there a CSS way to style the title attribute?

like image 572
Kunpha Avatar asked Jan 06 '10 05:01

Kunpha


People also ask

How do I copy text from a tooltip?

Click on the element that contains the tooltip text. Right-click on the selected element. From the content menu, select "Copy" -> "Copy element"

How do I style default tooltip?

You cannot style the default browser tooltip. But you can use javascript to create your own custom HTML tooltips.

How do I change the tooltip style in CSS?

The tooltip is automatically generated by web browsers, it actually can not be removed or changed. To change the tooltip style, we need to add a tooltip text to a different attribute, for example data-title , then use CSS code to create and customise the style. In WordPress, you can add the CSS code to your theme CSS.

How do I edit tooltip in HTML?

HTML: Use a container element (like <div>) and add the "tooltip" class to it. When the user mouse over this <div>, it will show the tooltip text. The tooltip text is placed inside an inline element (like <span>) with class="tooltiptext" .


2 Answers

It seems that there is in fact a pure CSS solution, requiring only the css attr expression, generated content and attribute selectors (which suggests that it works as far back as IE8):

https://jsfiddle.net/z42r2vv0/2/

a {   position: relative;   display: inline-block;   margin-top: 20px; }  a[title]:hover::after {   content: attr(title);   position: absolute;   top: -100%;   left: 0; }
<a href="http://www.google.com/" title="Hello world!">   Hover over me </a>

update w/ input from @ViROscar: please note that it's not necessary to use any specific attribute, although I've used the "title" attribute in the example above; actually my recommendation would be to use the "alt" attribute, as there is some chance that the content will be accessible to users unable to benefit from CSS.

update again I'm not changing the code because the "title" attribute has basically come to mean the "tooltip" attribute, and it's probably not a good idea to hide important text inside a field only accessible on hover, but if you're interested in making this text accessible the "aria-label" attribute seems like the best place for it: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-label_attribute

like image 174
Jon z Avatar answered Sep 23 '22 20:09

Jon z


Here is an example of how to do it:

a.tip {      border-bottom: 1px dashed;      text-decoration: none  }  a.tip:hover {      cursor: help;      position: relative  }  a.tip span {      display: none  }  a.tip:hover span {      border: #c0c0c0 1px dotted;      padding: 5px 20px 5px 5px;      display: block;      z-index: 100;      background: url(../images/status-info.png) #f0f0f0 no-repeat 100% 5%;      left: 0px;      margin: 10px;      width: 250px;      position: absolute;      top: 10px;      text-decoration: none  }
<a href="#" class="tip">Link<span>This is the CSS tooltip showing up when you mouse over the link</span></a>
like image 23
valli Avatar answered Sep 22 '22 20:09

valli