<main>
<div class="example-elements">
<p data-tooltip="A sample text">
Hover over me to see a tooltip
</p>
<a href="#"
data-tooltip="This link goes nowhere"
data-tooltip-location="right">
A typical anchor element
</a>
<button
data-tooltip="Example Tooltip"
data-tooltip-location="left">
Cool button
</button>
</div>
<div class="info-wrapper">
<p>
<span class="title-question">
You want a simple, animated & easy-to-use tooltip?
</span>
<span>
Just copy all the CSS declarations blocks starting with
<code>[data-tooltip].</code>
</span>
</p>
<p
data-tooltip="This is an example of a super long tooltip text that goes over multiple lines. 
 
 Note: The tooltip size is dynamically adjusted to the content. However, a minimum and a maximum width are defined."
data-tooltip-location="bottom">
To use the tooltip, simply add the attribute »data-tooltip« with the desired text to an element. P.S. Hover over me to see a long tooltip.
</p>
</div>
</main>
/*
You want a simple and fancy tooltip?
Just copy all [data-tooltip] blocks:
*/
[data-tooltip] {
--arrow-size: 5px;
position: relative;
z-index: 10;
}
/* Positioning and visibility settings of the tooltip */
[data-tooltip]:before,
[data-tooltip]:after {
position: absolute;
visibility: hidden;
opacity: 0;
left: 50%;
bottom: calc(100% + var(--arrow-size));
pointer-events: none;
transition: 0.2s;
will-change: transform;
}
/* The actual tooltip with a dynamic width */
[data-tooltip]:before {
content: attr(data-tooltip);
padding: 10px 18px;
min-width: 50px;
max-width: 300px;
width: max-content;
width: -moz-max-content;
border-radius: 6px;
font-size: 14px;
background-color: rgba(59, 72, 80, 0.9);
background-image: linear-gradient(30deg,
rgba(59, 72, 80, 0.44),
rgba(59, 68, 75, 0.44),
rgba(60, 82, 88, 0.44));
box-shadow: 0px 0px 24px rgba(0, 0, 0, 0.2);
color: #fff;
text-align: center;
white-space: pre-wrap;
transform: translate(-50%, calc(0px - var(--arrow-size))) scale(0.5);
}
/* Tooltip arrow */
[data-tooltip]:after {
content: '';
border-style: solid;
border-width: var(--arrow-size) var(--arrow-size) 0px var(--arrow-size); /* CSS triangle */
border-color: rgba(55, 64, 70, 0.9) transparent transparent transparent;
transition-duration: 0s; /* If the mouse leaves the element,
the transition effects for the
tooltip arrow are "turned off" */
transform-origin: top; /* Orientation setting for the
slide-down effect */
transform: translateX(-50%) scaleY(0);
}
/* Tooltip becomes visible at hover */
[data-tooltip]:hover:before,
[data-tooltip]:hover:after {
visibility: visible;
opacity: 1;
}
/* Scales from 0.5 to 1 -> grow effect */
[data-tooltip]:hover:before {
transition-delay: 0.3s;
transform: translate(-50%, calc(0px - var(--arrow-size))) scale(1);
}
/*
Arrow slide down effect only on mouseenter (NOT on mouseleave)
*/
[data-tooltip]:hover:after {
transition-delay: 0.5s; /* Starting after the grow effect */
transition-duration: 0.2s;
transform: translateX(-50%) scaleY(1);
}
/*
That's it for the basic tooltip.
If you want some adjustability
here are some orientation settings you can use:
*/
/* LEFT */
/* Tooltip + arrow */
[data-tooltip-location="left"]:before,
[data-tooltip-location="left"]:after {
left: auto;
right: calc(100% + var(--arrow-size));
bottom: 50%;
}
/* Tooltip */
[data-tooltip-location="left"]:before {
transform: translate(calc(0px - var(--arrow-size)), 50%) scale(0.5);
}
[data-tooltip-location="left"]:hover:before {
transform: translate(calc(0px - var(--arrow-size)), 50%) scale(1);
}
/* Arrow */
[data-tooltip-location="left"]:after {
border-width: var(--arrow-size) 0px var(--arrow-size) var(--arrow-size);
border-color: transparent transparent transparent rgba(55, 64, 70, 0.9);
transform-origin: left;
transform: translateY(50%) scaleX(0);
}
[data-tooltip-location="left"]:hover:after {
transform: translateY(50%) scaleX(1);
}
/* RIGHT */
[data-tooltip-location="right"]:before,
[data-tooltip-location="right"]:after {
left: calc(100% + var(--arrow-size));
bottom: 50%;
}
[data-tooltip-location="right"]:before {
transform: translate(var(--arrow-size), 50%) scale(0.5);
}
[data-tooltip-location="right"]:hover:before {
transform: translate(var(--arrow-size), 50%) scale(1);
}
[data-tooltip-location="right"]:after {
border-width: var(--arrow-size) var(--arrow-size) var(--arrow-size) 0px;
border-color: transparent rgba(55, 64, 70, 0.9) transparent transparent;
transform-origin: right;
transform: translateY(50%) scaleX(0);
}
[data-tooltip-location="right"]:hover:after {
transform: translateY(50%) scaleX(1);
}
/* BOTTOM */
[data-tooltip-location="bottom"]:before,
[data-tooltip-location="bottom"]:after {
top: calc(100% + var(--arrow-size));
bottom: auto;
}
[data-tooltip-location="bottom"]:before {
transform: translate(-50%, var(--arrow-size)) scale(0.5);
}
[data-tooltip-location="bottom"]:hover:before {
transform: translate(-50%, var(--arrow-size)) scale(1);
}
[data-tooltip-location="bottom"]:after {
border-width: 0px var(--arrow-size) var(--arrow-size) var(--arrow-size);
border-color: transparent transparent rgba(55, 64, 70, 0.9) transparent;
transform-origin: bottom;
}
/* Settings that make the pen look nicer */
html {
width: 100%;
height: 100%;
font-family: 'Roboto', sans-serif;
color: white;
font-size: 1.2em;
background: linear-gradient(45deg, #243949, #2cacd1, #35eb93);
background-size: 120% 120%;
animation: moveFocus 5s ease infinite alternate;
}
@keyframes moveFocus {
0% { background-position: 0% 100% }
100% { background-position: 100% 0% }
}
body {
background: none; display: flex;
flex-direction: column;
height: 100%;
margin: 0;
}
main {
padding: 0 4%;
display: flex;
flex-direction: row;
margin: auto 0;
}
button {
margin: 0;
padding: 0.7rem 1.4rem;
cursor: pointer;
text-align: center;
border: none;
border-radius: 4px;
outline: inherit;
text-decoration: none;
font-family: Roboto, sans-serif;
font-size: 0.7em;
background-color: rgba(174, 184, 192, 0.55);
color: white;
-webkit-appearance: none;
-moz-appearance: none;
transition: background 350ms ease-in-out,
transform 150ms ease;
}
button:hover {
background-color: #484f56;
}
button:active {
transform: scale(0.98);
}
button:focus {
box-shadow: 0 0 2px 2px #298bcf;
}
button::-moz-focus-inner {
border: 0;
}
.example-elements {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: center;
align-content: center;
justify-content: center;
text-align: center;
padding-right: 4%;
}
.example-elements p {
padding: 6px;
display: inline-block;
margin-bottom: 5%;
color: #fff;
}
.example-elements p:hover {
border-left: 1px solid lightgrey;
border-right: 1px solid lightgrey;
padding-left: 5px;
padding-right: 5px;
}
.example-elements a {
margin-left: 6px;
margin-bottom: calc(5% + 10px);
color: #76daff;
text-decoration: none;
}
.example-elements a:hover {
margin-bottom: calc(5% + 9px);
border-bottom: 1px solid #76daff;
}
.example-elements button {
margin-bottom: 20px;
}
.info-wrapper {
flex-grow: 8;
display: flex;
flex-direction: column;
justify-content: center;
text-align: justify;
padding-left: 6%;
border-left: 3px solid #35ea95;
}
.info-wrapper p {
color: rgba(255, 255, 255, 0.69);
}
.info-wrapper p {
max-width: 600px;
text-align: justify;
}
.info-wrapper .title-question {
display: block;
color: #fff;
font-size: 1.36em;
font-weight: 500;
padding-bottom: 24px;
}
@media (max-height: 450px) {
main {
margin: 2rem 0;
}
}
@media (max-width: 800px) {
html {
font-size: 0.9em;
}
}
/* Thumbnail settings */
@media (max-width: 750px) {
html {
animation-duration: 0.6s;
font-size: 1em;
}
body {
display: flex;
background: none;
height: 100%;
margin: 0px;
}
main {
font-size: 1.1em;
padding: 6%;
}
.info-wrapper p:before,
.info-wrapper p:after {
display: none;
}
.example-elements {
max-width: 150px;
font-size: 22px;
}
.example-elements a, button {
display: none;
}
.example-elements p:before,
.example-elements p:after {
visibility: visible;
opacity: 1;
}
.example-elements p:before {
content: "Tooltip";
font-size: 20px;
transform: translate(-50%, calc(0px - var(--arrow-size))) scale(1);
}
.example-elements p:after {
transform: translate(-50%, -1px) scaleY(1);
}
[data-tooltip]:after {
bottom: calc(100% + 3px);
}
[data-tooltip]:after {
border-width: 7px 7px 0px 7px;
}
}
This Pen doesn't use any external JavaScript resources.