Implement a tooltip system using only CSS for displayinginformation on hover.
Hey, I am Ajink, and today in this blog, we’re going to implement a tooltip system using only CSS. Tooltips are a handy way to provide additional information when users hover over specific elements on a webpage.
HTML Code: create index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Tooltip System</title>
</head>
<body>
<div class="tooltip-container">
<div class="tooltip-trigger" data-tooltip="Hello! This is a tooltip.">Hover me</div>
<div class="tooltip-text">Hello! This is a tooltip.</div>
</div>
</body>
</html>
HTML Code Explanation:
- We have a basic HTML structure with a container div and a trigger element (
tooltip-trigger) that will display the tooltip on hover.
CSS Code: style.css
body {
margin: 0;
font-family: 'Arial', sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f0f0f0;
}
.tooltip-container {
position: relative;
}
.tooltip-trigger {
cursor: pointer;
position: relative;
border: 1px solid #3498db;
padding: 10px;
background-color: #3498db;
color: #fff;
border-radius: 4px;
}
.tooltip-text {
visibility: hidden;
position: absolute;
z-index: 1;
top: 100%;
left: 50%;
transform: translateX(-50%);
background-color: #333;
color: #fff;
padding: 5px;
border-radius: 4px;
width: 150px;
text-align: center;
opacity: 0;
transition: opacity 0.3s ease-in-out;
}
.tooltip-trigger:hover + .tooltip-text {
visibility: visible;
opacity: 1;
}
CSS Code Explanation:
- The CSS provides styling for the tooltip system.
- The
.tooltip-containerclass is set as relative, and the.tooltip-triggerclass represents the element that triggers the tooltip on hover. - The
.tooltip-textclass represents the actual tooltip and is initially hidden (visibility: hidden;). - On hover, the tooltip becomes visible and fades in with a smooth transition.
Conclusion:
In this blog, we successfully implemented a tooltip system using only CSS. You can use this technique to enhance user experience by providing additional information on hover. Don’t forget to subscribe to my YouTube channel at youtube.com/@ajink21 for more exciting tutorials.
Thanks for reading, and if you have any doubts, feel free to comment!
More in 21 days of HTML CSS JS
See all →- Ultimate Guide to Free Web Development Resources In today’s digital age, web development is a crucial skill that opens up numerous opportunities. Whether you’re a beginner looking to build...
- 10 Effective Ways to Center a Div Hello, myself Ajink. In this article, we will explore the various techniques to center a div element on a web page. Centering a div might se...
- Learn HTML HTML Document Structure: : Defines the root of an HTML document. : Contains meta-information about the document. : Sets the title of the doc...
- How to Create an Valentine’s Day Website? How to Create an Valentine’s Day Website , lets get started Demo : Here is Complete SourceCode : body background-color: #fde8e8; font-family...
- Create a accordion with html css and javascript body { font-family: 'Arial', sans-serif; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh;...
- Create a virtual keyboard that users can interact with on the webpage. Virtual Keyboard body font-family: 'Arial', sans-serif; display: flex; flex-direction: column; align-items: center; justify-content: center;...