title
attributeHow 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.
However, you can create something very similar using other attributes.
data-title
)For this, I'd use a data-title
attribute. data-*
attributes are a method to store custom data in DOM elements/HTML. There are multiple ways of accessing them. Importantly, they can be selected by CSS.
Given that you can use CSS to select elements with data-title
attributes, you can then use CSS to create :after
(or :before
) content
that contains the value of the attribute using attr()
.
Bigger and with a different background color (per question's request):
[data-title]:hover:after {_x000D_
opacity: 1;_x000D_
transition: all 0.1s ease 0.5s;_x000D_
visibility: visible;_x000D_
}_x000D_
[data-title]:after {_x000D_
content: attr(data-title);_x000D_
background-color: #00FF00;_x000D_
color: #111;_x000D_
font-size: 150%;_x000D_
position: absolute;_x000D_
padding: 1px 5px 2px 5px;_x000D_
bottom: -1.6em;_x000D_
left: 100%;_x000D_
white-space: nowrap;_x000D_
box-shadow: 1px 1px 3px #222222;_x000D_
opacity: 0;_x000D_
border: 1px solid #111111;_x000D_
z-index: 99999;_x000D_
visibility: hidden;_x000D_
}_x000D_
[data-title] {_x000D_
position: relative;_x000D_
}
_x000D_
<a href="example.com" data-title="My site"> Link </a> with styled tooltip (bigger and with a different background color, as requested in the question)<br/>_x000D_
<a href="example.com" title="My site"> Link </a> with normal tooltip
_x000D_
More elaborate styling (adapted from this blog post):
[data-title]:hover:after {_x000D_
opacity: 1;_x000D_
transition: all 0.1s ease 0.5s;_x000D_
visibility: visible;_x000D_
}_x000D_
[data-title]:after {_x000D_
content: attr(data-title);_x000D_
position: absolute;_x000D_
bottom: -1.6em;_x000D_
left: 100%;_x000D_
padding: 4px 4px 4px 8px;_x000D_
color: #222;_x000D_
white-space: nowrap; _x000D_
-moz-border-radius: 5px; _x000D_
-webkit-border-radius: 5px; _x000D_
border-radius: 5px; _x000D_
-moz-box-shadow: 0px 0px 4px #222; _x000D_
-webkit-box-shadow: 0px 0px 4px #222; _x000D_
box-shadow: 0px 0px 4px #222; _x000D_
background-image: -moz-linear-gradient(top, #f8f8f8, #cccccc); _x000D_
background-image: -webkit-gradient(linear,left top,left bottom,color-stop(0, #f8f8f8),color-stop(1, #cccccc));_x000D_
background-image: -webkit-linear-gradient(top, #f8f8f8, #cccccc); _x000D_
background-image: -moz-linear-gradient(top, #f8f8f8, #cccccc); _x000D_
background-image: -ms-linear-gradient(top, #f8f8f8, #cccccc); _x000D_
background-image: -o-linear-gradient(top, #f8f8f8, #cccccc);_x000D_
opacity: 0;_x000D_
z-index: 99999;_x000D_
visibility: hidden;_x000D_
}_x000D_
[data-title] {_x000D_
position: relative;_x000D_
}
_x000D_
<a href="example.com" data-title="My site"> Link </a> with styled tooltip<br/>_x000D_
<a href="example.com" title="My site"> Link </a> with normal tooltip
_x000D_
Unlike a real title
tooltip, the tooltip produced by the above CSS is not, necessarily, guaranteed to be visible on the page (i.e. it might be outside the visible area). On the other hand, it is guaranteed to be within the current window, which is not the case for an actual tooltip.
In addition, the pseudo-tooltip is positioned relative to the element that has the pseudo-tooltip rather than relative to where the mouse is on that element. You may want to fine-tune where the pseudo-tooltip is displayed. Having it appear in a known location relative to the element can be a benefit or a drawback, depending on the situation.
:before
or :after
on elements which are not containersThere's a good explanation in this answer to "Can I use a :before or :after pseudo-element on an input field?"
Effectively, this means that you can't use this method directly on elements like <input type="text"/>
, <textarea/>
, <img>
, etc. The easy solution is to wrap the element that's not a container in a <span>
or <div>
and have the pseudo-tooltip on the container.
Examples of using a pseudo-tooltip on a <span>
wrapping a non-container element:
[data-title]:hover:after {_x000D_
opacity: 1;_x000D_
transition: all 0.1s ease 0.5s;_x000D_
visibility: visible;_x000D_
}_x000D_
[data-title]:after {_x000D_
content: attr(data-title);_x000D_
background-color: #00FF00;_x000D_
color: #111;_x000D_
font-size: 150%;_x000D_
position: absolute;_x000D_
padding: 1px 5px 2px 5px;_x000D_
bottom: -1.6em;_x000D_
left: 100%;_x000D_
white-space: nowrap;_x000D_
box-shadow: 1px 1px 3px #222222;_x000D_
opacity: 0;_x000D_
border: 1px solid #111111;_x000D_
z-index: 99999;_x000D_
visibility: hidden;_x000D_
}_x000D_
[data-title] {_x000D_
position: relative;_x000D_
}_x000D_
_x000D_
.pseudo-tooltip-wrapper {_x000D_
/*This causes the wrapping element to be the same size as what it contains.*/_x000D_
display: inline-block;_x000D_
}
_x000D_
Text input with a pseudo-tooltip:<br/>_x000D_
<span class="pseudo-tooltip-wrapper" data-title="input type="text""><input type='text'></span><br/><br/><br/>_x000D_
Textarea with a pseudo-tooltip:<br/>_x000D_
<span class="pseudo-tooltip-wrapper" data-title="this is a textarea"><textarea data-title="this is a textarea"></textarea></span><br/>
_x000D_
From the code on the blog post linked above (which I first saw in an answer here that plagiarized it), it appeared obvious to me to use a data-*
attribute instead of the title
attribute. Doing so was also suggested in a comment by snostorm on that (now deleted) answer.