CSS Tooltip
A CSS tooltip is used to specify extra information about something when the user moves the mouse pointer over an element:
CSS Create a Basic Tooltip
Create a tooltip that appears when the user moves the mouse over an element:
Example
<style>
/* Tooltip container */
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted
black; /* Add dots under the hoverable text */
cursor: pointer;
}
/* Tooltip text
*/
.tooltiptext {
visibility: hidden; /*
Hidden by default */
width: 130px;
background-color: black;
color: #ffffff;
text-align: center;
padding: 5px 0;
border-radius: 6px;
position: absolute;
z-index: 1; /* Ensure tooltip is displayed above
content */
}
/* Show
the tooltip text on hover */
.tooltip:hover
.tooltiptext {
visibility: visible;
}
</style>
<div class="tooltip">Hover
over me
<span class="tooltiptext">Some
tooltip
text</span>
</div>
Example Explained
HTML:
- Use a container element (like ) and add the
"tooltip"class to it. When the user mouse over this, it will show the tooltip text.- The tooltip text is placed inside an inline element (like ) with
class="tooltiptext".CSS:
- The
tooltipclass useposition:relative, which is needed to position the tooltip text (position:absolute). Tip: See examples below on how to position the tooltip. - The
tooltiptextclass holds the actual tooltip text. It is hidden by default, and will be visible on hover. - The
:hoverselector is used to show the tooltip text when the user moves the mouse over thewithclass="tooltip".
Positioning the Tooltip
You can position the tooltip as you like. Here we will show how to position the tooltip to the left, right, top and bottom.
Right- and Left-aligned Tooltip
In this example, the tooltip is placed to the right (
left:105%) of the "hoverable" text (). Also note thattop:-5pxis used to place it in the middle of its container element. We use the number 5 because the tooltip text has a top and bottom padding of 5px. If you increase its padding, also increase the value of thetopproperty to ensure that it stays in the middle (if this is something you want). The same applies if you want the tooltip placed to the left.Example
.tooltiptext { top: -5px; left: 105%; }Example
.tooltiptext { top: -5px; right: 105%; }Top- and Bottom-aligned Tooltip
If you want the tooltip to appear on top or on the bottom, see examples below. Note that we use the
margin-leftproperty with a value of minus 65 pixels. This is to center the tooltip above/below the hoverable text. It is set to the half of the tooltip's width (130/2 = 65).Example
.tooltiptext { width: 130px; bottom: 100%; left: 65%; margin-left: -65px; /* Use half of the width (130/2 = 65), to center the tooltip */ }Example
.tooltiptext { width: 130px; top: 100%; left: 50%; margin-left: -65px; /* Use half of the width (130/2 = 65), to center the tooltip */ }
Fade-in Tooltip
If you want a tooltip that fades in, use the CSS
transitionproperty and theopacityproperty, and go from being completely invisible to 100% visible, in a number of specified seconds (2 second in our example):Example
.tooltiptext { opacity: 0; transition: opacity 2s; } .tooltip:hover .tooltiptext { opacity: 1; }Want to go beyond the notes?
Join CodingNow 2.0's CSS course — live mentorship, real projects, and 100% placement support.
Enroll Now — Free Demo Available
CSS Tooltips – FAQs
Quick answers about learning CSS Tooltips in CSS.
This free note from CodingNow 2.0 explains CSS Tooltips in CSS — concept, syntax and worked code examples you can copy, run and revise before interviews.Yes. Every CSS topic on CodingNow 2.0, including CSS Tooltips, is 100% free with no signup required.With focused practice, most students grasp CSS Tooltips in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours. - The tooltip text is placed inside an inline element (like ) with