Hello Coders! Welcome to CSS Animation Blog. In this, let's create a custom tooltip animation. Tooltips are very helpful in modern webapps. Many times users don't know what a particular button or element does. Tooltip is the best way to give additional information about something. Just hover over the element and tooltip will do the thing.
Here's a preview -
That being said, let us get started.
Step - 1: Like always, create 2 files - index.html and style.css.
Step - 2: Copy the below HTML code and paste it into your code editor.
HTML
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>CSS Tooltip</title>
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css'><link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="icon-container">
<i class="fas fa-info icon"></i>
<div class="tooltip">This is just a plain tooltip</div>
</div>
</body>
</html>
Step - 3: Below is the CSS code for styling.
CSS
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@500&display=swap');
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: #2f2f2f;
font-family: 'Roboto', sans-serif;
}
.icon-container {
display: flex;
justify-content: center;
align-items: center;
width: 50px;
height: 50px;
border-radius: 100%;
background: #E6EAFB;
cursor: pointer;
position: relative;
}
.icon {
color: #b74096;
font-size: 1.125rem;
}
.tooltip {
pointer-events: none;
animation: fade-out-left 0.25s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
opacity: 0;
position: absolute;
line-height: 1.6;
right: 70px;
width: auto;
max-width: 250px;
color: white;
width: max-content;
font-size: 0.875rem;
background: #b74096;
padding: 10px 15px;
border-radius: 4px;
box-shadow:
0 1px 1px rgba(183, 64, 150,0.12),
0 2px 2px rgba(183, 64, 150,0.12),
0 4px 4px rgba(183, 64, 150,0.12),
0 8px 8px rgba(183, 64, 150,0.12),
0 16px 16px rgba(183, 64, 150,0.12);
}
.tooltip::after {
content: '';
position: absolute;
z-index: -1;
top: 50%;
right: -4px;
width: 20px;
height: 20px;
background: inherit;
border-radius: inherit;
transform: translateY(-50%) rotate(45deg);
}
.icon-container:hover .tooltip {
animation: fade-in-left 0.35s cubic-bezier(0.390, 0.575, 0.565, 1.000) 0.35s both;
}
@keyframes fade-in-left {
0% {
transform: translateX(-15px);
opacity: 0;
}
100% {
transform: translateX(0);
opacity: 1;
}
}
@keyframes fade-out-left {
0% {
transform: translateX(0);
opacity: 1;
}
100% {
transform: translateX(-8px);
opacity: 0;
}
}
And that's it. You're done.
Let me know in the comments if you have any doubt related to this.
Follow @creocodigo for more projects and web related content.
If you find this useful, below are some other posts that I am sure you'll love