I am having trouble implementing a tooltip using CSS for an <img>
element.
While I have successfully added a tooltip to <a href> </a>
tags, applying the same functionality to an <img>
tag has proven difficult.
http://jsfiddle.net/ronluna/6sgLagrr
How can I make the tooltip display when hovering over the <img>
element?
/* Custom styles for adding tooltips */
img,
[data-tooltip],
.tooltip {
position: relative;
cursor: pointer;
}
/* Base styles for the whole tooltip */
img:before,
img:after,
[data-tooltip]:before,
[data-tooltip]:after,
.tooltip:before,
.tooltip:after {
position: absolute;
visibility: hidden;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0);
opacity: 0;
/* Vendor-prefixed transition property */
-webkit-transition:
opacity 0.2s ease-in-out,
visibility 0.2s ease-in-out,
-webkit-transform 0.2s cubic-bezier(0.71, 1.7, 0.77, 1.24);
-moz-transition:
opacity 0.2s ease-in-out,
visibility 0.2s ease-in-out,
-moz-transform 0.2s cubic-bezier(0.71, 1.7, 0.77, 1.24);
transition:
opacity 0.2s ease-in-out,
visibility 0.2s ease-in-out,
transform 0.2s cubic-bezier(0.71, 1.7, 0.77, 1.24);
-webkit-transform: translate3d(0, 0, 0);
-moz-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
pointer-events: none;
}
/* Make the entire tooltip visible on hover and focus */
img:hover:before,
img:hover:after,
img:focus:before,
img:focus:after,
[data-tooltip]:hover:before,
[data-tooltip]:hover:after,
[data-tooltip]:focus:before,
[data-tooltip]:focus:after,
.tooltip:hover:before,
.tooltip:hover:after,
.tooltip:focus:before,
.tooltip:focus:after {
visibility: visible;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=100);
opacity: 1;
}
/* Styles for the arrow of the tooltip */
img:before,
.tooltip:before,
[data-tooltip]:before {
z-index: 1001;
border: 6px solid transparent;
background: transparent;
content: "";
}
/* Styles for the content area of the tooltip */
img:after,
.tooltip:after,
[data-tooltip]:after {
z-index: 1000;
padding: 8px;
width: 160px;
background-color: #000;
background-color: hsla(0, 0%, 20%, 0.9);
color: #fff;
content: attr(data-tooltip);
font-size: 14px;
line-height: 1.2;
text-align: center;
}
/* Direction settings */
<!-- Top (default) -->
[data-tooltip]:before,
[data-tooltip]:after,
img:before,
img:after,
.tooltip:before,
.tooltip:after,
.tooltip-top:before,
.tooltip-top:after {
bottom: 100%;
left: 50%;
}
[data-tooltip]:before,
.tooltip:before,
img:before,
.tooltip-top:before {
margin-left: -6px;
margin-bottom: -12px;
border-top-color: #000;
border-top-color: hsla(0, 0%, 20%, 0.9);
}
/* Horizontally align top/bottom tooltips */
[data-tooltip]:after,
.tooltip:after,
img:after,
.tooltip-top:after {
margin-left: -80px;
}
[data-tooltip]:hover:before,
[data-tooltip]:hover:after,
[data-tooltip]:focus:before,
[data-tooltip]:focus:after,
img:hover:before,
img:hover:after,
img:focus:before,
img:focus:after,
.tooltip:hover:before,
.tooltip:hover:after,
.tooltip:focus:before,
.tooltip:focus:after,
.tooltip-top:hover:before,
.tooltip-top:hover:after,
.tooltip-top:focus:before,
.tooltip-top:focus:after {
-webkit-transform: translateY(-12px);
-moz-transform: translateY(-12px);
transform: translateY(-12px);
}
<br>
<br>
<a href="#" data-tooltip="This is a test">Test1</a>
<a href="#" class="tooltip" data-tooltip="THIS IS A TEST">Test2</a>
<img src="http://ctrlable.com/site/wp-content/uploads/2015/03/ctrlable_logo_white_cyan.png" class="tooltip" data-tooltip="THIS IS A TEST">
What am I missing in my code for the tooltip on the <img>
element to function properly?