CSS Tooltip
Make pop-up tips using CSS.
Demo: Tooltip Examples
A tooltip is like a small info pop-up that appears when you hover your mouse over something. It gives you more details about that thing.
Basic Tooltip
Make a little pop-up message show up when someone moves their mouse over something on the screen:
Example Explained
HTML: To create a tooltip, you can use a container element such as <div> and apply the "tooltip" class to it. When a user hovers their mouse over this <div>, it will display the tooltip text.
The tooltip text is placed within a small element, such as a tag, which has the attribute class="tooltiptext"
.
CSS: The tooltip
class uses position:relative
to position the tooltip text, which is done using position:absolute
. Note: You can find examples below that show how to position the tooltip.
The class called "tooltiptext" contains the real tooltip text. Initially, it's not visible, but it will appear when you hover over it (as shown below). We've also applied some simple styles to it: a width of 120 pixels, a black background, white text, centered alignment, and 5 pixels of padding at the top and bottom.
The CSS code called "border-radius" is used to make the edges of a tooltip text appear rounded.
The ":hover" selector is a way to display a tooltip when you hover your mouse over a <p> element with the class "tooltip."
Positioning Tooltips
In this example, we're putting a tooltip to the right side of some text that you can hover over. To make sure it's centered within its container, we're setting the distance from the top as top:-5px
. We're using the number 5 because the tooltip text has padding of 5px on both the top and bottom. If you increase this padding, you'll need to increase the top
value too, so the tooltip stays centered. This same rule applies if you want the tooltip on the left side.
If you'd like the tooltip to show up either above or below, check out the examples below. We're using the margin-left
property with a value of -60 pixels. This centers the tooltip above or below the text you hover over. We set it to half of the tooltip's width (120/2 = 60).
Tooltip Arrows
To make a tooltip appear with an arrow pointing from a certain direction, you can add a "dummy" content right after the tooltip. To create the arrow, use a pseudo-element with the class ::after and set its content property. The arrow is constructed using borders. This will give the tooltip the appearance of a speech bubble.
This example shows how to include an arrow at the bottom of a tooltip:
Example Explained
To put the arrow inside the tooltip, use top: 100%
to make the arrow appear at the tooltip's bottom. To center the arrow, apply left: 50%
.
Please remember: The "border-width" property tells you how big the arrow is. If you adjust it, make sure to adjust the "margin-left" value to match. This will make sure the arrow stays in the middle.
The border-color
property is used to change the appearance of an element to look like an arrow. We make the top border black and the other borders transparent. If we made all the borders black, the element would appear as a black square box.
In this example, we show how to put an arrow at the top of a tooltip. You'll see that we change the color of the border at the bottom:
Here's how you can put an arrow on the left side of the tooltip in this example:
This example shows how to include an arrow on the right side of the tooltip:
Fade In Tooltips (Animation)
To make the tooltip text gradually appear when it's going to show up, you can utilize CSS. Combine the transition
property with opacity
. Start with it being completely invisible and slowly make it 100% visible. This change will occur over a specified duration, like 1 second in our example.
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
/* Position the tooltip */
position: absolute;
z-index: 1;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
<body style="text-align:center;">
<h2>Basic Tooltip</h2>
<p>Move the mouse over the text below:</p>
<div class="tooltip">Hover over me <span class="tooltiptext">Tooltip text</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
/* Position the tooltip */
position: absolute;
z-index: 1;
top: -5px;
left: 105%;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
<body style="text-align:center;">
<h2>Right Tooltip</h2>
<p>Move the mouse over the text below:</p>
<div class="tooltip">Hover over me <span class="tooltiptext">Tooltip text</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
/* Position the tooltip */
position: absolute;
z-index: 1;
top: -5px;
right: 105%;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
<body style="text-align:center;">
<h2>Left Tooltip</h2>
<p>Move the mouse over the text below:</p>
<div class="tooltip">Hover over me <span class="tooltiptext">Tooltip text</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
/* Position the tooltip */
position: absolute;
z-index: 1;
bottom: 100%;
left: 50%;
margin-left: -60px;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
<body style="text-align:center;">
<h2>Top Tooltip</h2>
<p>Move the mouse over the text below:</p>
<div class="tooltip">Hover over me <span class="tooltiptext">Tooltip text</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
/* Position the tooltip */
position: absolute;
z-index: 1;
top: 100%;
left: 50%;
margin-left: -60px;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
<body style="text-align:center;">
<h2>Bottom Tooltip</h2>
<p>Move the mouse over the text below:</p>
<div class="tooltip">Hover over me <span class="tooltiptext">Tooltip text</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
bottom: 150%;
left: 50%;
margin-left: -60px;
}
.tooltip .tooltiptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: black transparent transparent transparent;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
<body style="text-align:center;">
<h2>Top Tooltip w/ Bottom Arrow</h2>
<div class="tooltip">Hover over me <span class="tooltiptext">Tooltip text</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
top: 150%;
left: 50%;
margin-left: -60px;
}
.tooltip .tooltiptext::after {
content: "";
position: absolute;
bottom: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent transparent black transparent;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
<body style="text-align:center;">
<h2>Bottom Tooltip w/ Top Arrow</h2>
<div class="tooltip">Hover over me <span class="tooltiptext">Tooltip text</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
top: -5px;
left: 110%;
}
.tooltip .tooltiptext::after {
content: "";
position: absolute;
top: 50%;
right: 100%;
margin-top: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent black transparent transparent;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
<body style="text-align:center;">
<h2>Right Tooltip w/ Left Arrow</h2>
<div class="tooltip">Hover over me <span class="tooltiptext">Tooltip text</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
top: -5px;
right: 110%;
}
.tooltip .tooltiptext::after {
content: "";
position: absolute;
top: 50%;
left: 100%;
margin-top: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent transparent transparent black;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
<body style="text-align:center;">
<h2>Left Tooltip w/ Right Arrow</h2>
<div class="tooltip">Hover over me <span class="tooltiptext">Tooltip text</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
bottom: 100%;
left: 50%;
margin-left: -60px;
/* Fade in tooltip - takes 1 second to go from 0% to 100% opac: */
opacity: 0;
transition: opacity 1s;
}
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
</style>
<body style="text-align:center;">
<h2>Fade In Tooltip on Hover</h2>
<p>When you hover your mouse over the text, a tooltip will appear gradually, taking 1 second to transition from completely hidden to fully visible.</p>
<div class="tooltip">Hover over me <span class="tooltiptext">Tooltip text</span>
</div>
</body>
</html>