section
h1 Automatic Tooltips
p
| Tooltips can be
span( class="TooltipTrigger", data-tooltips = "Attached", data-position = "OnTop" ) attached
| to any element. When you hover the
span( class="TooltipTrigger", data-tooltips = "Element to something", data-position = "OnBottom" ) element
| with your mouse, the title attribute is displayed in
span( class="TooltipTrigger", data-tooltips = "A little box to something to make it longer", data-position = "OnLeft" ) a little box
| next to the element, just like a native tooltip. But as it's not a native tooltip, it can be styled. Any themes built with
span( class="TooltipTrigger", data-tooltips = "Theme Roller to something", data-position = "OnRight" ) ThemeRoller
| will also style tooltips accordingly. Tooltips are also useful for form elements, to show some additional information in the context of each field.
View Compiled
@import url(https://fonts.googleapis.com/css?family=Open+Sans:400,300,700);
body {
margin: 0;
padding: 0;
font-family: 'Open Sans', sans-serif;
font-weight: 300;
font-size: 16px;
position: relative;
text-align: center;
color: #494949;
}
section {
width: 400px;
margin: 90px auto;
}
h1 {
display: inline-block;
font-weight: 700;
font-size: 2rem;
line-height: 2.8rem;
border-bottom: 5px solid #67C8EA;
}
p {
text-align: left;
font-size: 1rem;
line-height: 2;
margin-top: 2rem;
}
span {
border-bottom: 3px solid #67C8EA;
cursor: pointer;
transition: all 0.3s ease;
&:hover {
background: #A4E1F6;
}
}
.Tooltips {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 5;
&:hover p {
visibility: visible;
opacity: 1;
}
&:hover p.OnTop {
transform: translate(-50%, -100%);
}
&:hover p.OnBottom {
transform: translate(-50%, 100%);
}
&:hover p.OnLeft {
transform: translate(-100%, -50%);
}
&:hover p.OnRight {
transform: translate(100%, -50%);
}
p {
min-width: 100px;
opacity: 0;
display: inline-block;
visibility: hidden;
position: absolute;
text-align: left;
width: auto;
background: #67C8EA;
color: #ffffff;
font-size: 1rem;
line-height: 1.2;
padding: 10px;
border-radius: 3px;
white-space: normal;
// white-space: nowrap; // Uncomment this line if you want to break line
box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.2);
transition: all 0.2s ease-out;
&.OnTop {
top: -45px;
left: 50%;
transform: translate(-50%, -150%);
&:before {
position: absolute;
content: "";
width: 0;
height: 0;
border-top: 8px solid #67C8EA;
border-left: 8px solid transparent;
border-right: 8px solid transparent;
bottom: -8px;
margin-left: -8px;
left: 50%;
}
}
&.OnBottom {
bottom: -15px;
left: 50%;
transform: translate(-50%, 150%);
&:before {
position: absolute;
content: "";
width: 0;
height: 0;
border-bottom: 8px solid #67C8EA;
border-left: 8px solid transparent;
border-right: 8px solid transparent;
top: -8px;
margin-left: -8px;
left: 50%;
}
}
&.OnLeft {
left: -15px;
top: -15px;
transform: translate(-150%, -50%);
&:before {
position: absolute;
content: "";
width: 0;
height: 0;
border-left: 8px solid #67C8EA;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
top: 50%;
margin-top: -8px;
right: -8px;
}
}
&.OnRight {
right: -15px;
top: -15px;
transform: translate(150%, -50%);
&:before {
position: absolute;
content: "";
width: 0;
height: 0;
border-right: 8px solid #67C8EA;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
top: 50%;
margin-top: -8px;
left: -8px;
}
}
}
}
View Compiled
// Initialize
var Tooltips = document.getElementsByClassName('TooltipTrigger');
// Track all tooltips trigger
for (var i = 0; i < Tooltips.length; i++) {
// Event Handler
Tooltips[i].addEventListener("mouseenter", function(ev) {
ev.preventDefault();
this.style.position = "relative";
this.innerHTML = this.innerHTML + "<div class='Tooltips'><p class='" + this.getAttribute("data-position") + "'>" + this.getAttribute("data-tooltips") + "</p></div>";
});
Tooltips[i].addEventListener("mouseleave", function(ev) {
ev.preventDefault();
this.removeAttribute("style");
this.innerHTML = this.innerHTML.replace(/<div[^]*?<\/div>/, '');;
});
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.