Positioning Tooltips Above Triggers

Different ways to position tooltips.

by J. Albert Bowden

HTML

<h1 style="text-align:center;">Positioning Tooltips Above Triggers...and Keyboard Accessible</h1>
<h2>Link with Title Attribute Tooltip Positioned Above Trigger</h2>
<a href="#" title="Tooltip Text">This is a link</a>
<h2>Button with Title Attribute Tooltip Positioned Above Trigger</h2>
<button title="Submit Your Answer">Submit</button>
<h2>Link with Title Attribute using Tooltip Class</h2>
<a title="some advisory info" href="#" class="tooltip">Link with Tooltip Class</a>
<h2>Link with Custom Tooltip. no alt</h2>
<a href="#" class="tooltip2">Llink Text with custom span tooltip<span class="tooltiptext">Custom tooltip</span></a>
<br />
<h2>
Image with Custom Tooltip
</h2>

<div class="tooltip2" tabindex="0"><img style="height:50px; width:50px;" ...

CSS

h1, h2, a {
font-family: arial;
}
/*CSS to position tooltip of title attribute above trigger fpr link and button*/
 a[title]:hover:after, button[title]:hover:after, a[title]:focus:after, button[title]:focus:after  {
  content: attr(title);
  position: relative;
  top: -25px;
  left: -60px;
  padding: 5px, 15px;
  background: black;
  color: white;
  border-radius: 6px;
}
/*Another way to do this by creating a tooltip class*/
.tooltip:hover:after, .tooltip:focus:after{
    background: #333;
    background: rgba(0,0,0,.8);
    border-radius: 5px;
    bottom: 26px;
    color: #fff;
    content: attr(title);
    left: 20%;
    padding: 5px 15px;
    position: relative;
    top:  -25;
    left: -50;
    z-index: 98;
    width: 220px;
}
/*Custom Tooltip, no alt attribute*/
.tooltip2 {
    position: absolute;
}

.tooltip2 .tooltiptext {
    visibility: hidden;
    width: 120px;
    background-color: black;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 5px 5px;

    /* Position the tooltip */
    position: relative;
    top: -35px;
    left: -50px;
}
.tooltip2:hover .tooltiptext, .tooltip2:focus .tooltiptext {
    visibility: visible;
}