bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/CSS/Advanced Styling
CSS•Advanced Styling

CSS Tooltip

Concept visual

CSS Tooltip

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

Overview

A CSS tooltip is used to specify extra information about something when the user moves the mouse pointer over an element:

Top

Tooltip text

Right

Tooltip text

Bottom

Tooltip text

Left

Tooltip text

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:

Formula

Use a container element (like < div >) and add the

"tooltip" class to it. When the user mouse over this <div>, it will show the tooltip text.

Formula

The tooltip text is placed inside an inline element (like < span >) with class ="tooltiptext".

CSS:

The tooltip class use position:relative, which is needed to position the tooltip text ( position:absolute ).

Tip:

See examples below on how to position the tooltip. The tooltiptext class holds the actual tooltip text. It is hidden by default, and will be visible on hover.

The

Formula

:hover selector is used to show the tooltip text when the user moves the mouse over the < div > with class ="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 (<div>). Also note that top:-5px is used to place it in the middle of its container element.

We use the number

because the tooltip text has a top and bottom padding of 5px. If you increase its padding, also increase the value of the top property 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

Formula

Right - aligned tooltip:.tooltiptext {
top: -5px;

left:

105%;
}

Result:

Hover over me

Previous

CSS Animations Code Challenge

Next

CSS Tooltip Arrows