CSS Tooltip
In this article we introduce how to use HTML and CSS to create tooltips.
A tooltip is triggered when the mouse moves over a specified element. Let's look at the following four examples:
|
Basic Tooltip
A tooltip is displayed when the mouse moves over a specified element:
HTML code:
<
style
/*Tooltip Container
*/
.tooltip
{
position:
relative
;
display:
inline-block
;
border-bottom:
1
px
dotted
black
;
/*
Dotted line shown on hover element
*/
}
/*
Tooltip Text
*/
.tooltip
.tooltiptext
{
visibility:
hidden
;
width:
120
px
;
background-color:
black
;
color:
#fff
;
text-align:
center
;
padding:
5
px
0
;
border-radius:
6
px
;
/*
Locate
*/
position:
absolute
;
z-index:
1
;
}
/*
The tooltip is displayed when the mouse moves over it.
*/
.tooltip
:hover
.tooltiptext
{
visibility:
visible
;
}
style
<div class="tooltip">Move the mouse over this
<span class="tooltiptext">Prompt text
span
div
Try it »
Example Parsing
HTML) Use a container element (like
The tooltip text is placed on an inline element (such as ) and useclass="tooltiptext"。
**CSS)**tooltip Class uses position:relative, the tooltip text needs a position value set position:absolute. **Note: ** The following examples will show more positioning effects.
tooltiptext class is used for the actual tooltip text. The pattern is hidden and displayed when the mouse moves over the element. Some styles such as width, background color, and font color are set.
CSS3 border-radius property is used to add rounded corners to the tooltip.
:hover The selector is used to display the tooltip when the mouse moves over the specified
Positioning the Tooltip
In the following example, the tooltip is displayed on the right side of the specified element (left:105%) 。
Note top:-5px the same as positioning it in the middle of the container element. Use the number 5 because the top and bottom padding of the tooltip text is 5px.
If you change the value of padding, the top value must be changed accordingly to ensure it is centered.
The same principle applies when the tooltip is displayed on the left.
Display on the Right:
.tooltip
.tooltiptext
{
top:
5
px
;
left:
105
%
;
}
Try it »
Display on the Left:
.tooltip
.tooltiptext
{
top:
5
px
;
right:
105
%
;
}
Try it »
If you want the tooltip to be displayed at the top and bottom, we need to use margin-left property and set it to -60px. This number is calculated by using half of the width for center alignment, that is: width/2 (120/2 = 60).
Display on Top:
.tooltip
.tooltiptext
{
width:
120
px
;
bottom:
100
%
;
left:
50
%
;
margin-left:
60
px
;
/*
Use half the width (120/2 = 60) to center the tooltip.
*/
}
Try it »
Display on the Bottom:
.tooltip
.tooltiptext
{
width:
120
px
;
top:
100
%
;
left:
50
%
;
margin-left:
60
px
;
/*
Use half the width (120/2 = 60) to center the tooltip.
*/
}
Try it »
Add an arrow
We can use the CSS pseudo-element ::after and the content property to create a small arrow for the tooltip. The arrow is made of borders, but when combined, the tooltip looks like a speech bubble.
The following example demonstrates how to add a bottom arrow to a tooltip displayed at the top:
Top Tooltip / Bottom Arrow:
.tooltip
.tooltiptext
:
:after
{
content:
" "
;
position:
absolute
;
top:
100
%
;
/*
Tooltip Bottom
*/
left:
50
%
;
margin-left:
5
px
;
border-width:
5
px
;
border-style:
solid
;
border-color:
black
transparent
transparent
transparent
;
}
Try it »
Example Parsing
Position the arrow within the tooltip: top: 100% , the arrow will be displayed at the bottom of the tooltip.left: 50% is used to center the arrow.
**Note:**border-width property specifies the size of the arrow. If you change it, you must also change the margin-left value. This way the arrow will be centered.
border-color is used to convert the content into an arrow. Set the top border to black and the others to transparent. If the others are also set to black, it will display as a black square.
The following example demonstrates how to add an arrow at the top of the tooltip. Note the border color settings:
Bottom Tooltip / Top Arrow:
.tooltip
.tooltiptext
:
:after
{
content:
" "
;
position:
absolute
;
bottom:
100
%
;
/*
Tooltip Top
*/
left:
50
%
;
margin-left:
5
px
;
border-width:
5
px
;
border-style:
solid
;
border-color:
transparent
transparent
black
transparent
;
}
Try it »
The following two examples show left and right arrows:
Right Tooltip / Left Arrow:
.tooltip
.tooltiptext
:
:after
{
content:
" "
;
position:
absolute
;
top:
50
%
;
right:
100
%
;
/*
Tooltip Left
*/
margin-top:
5
px
;
border-width:
5
px
;
border-style:
solid
;
border-color:
transparent
black
transparent
transparent
;
}
Try it »
Left Tooltip / Right Arrow:
.tooltip
.tooltiptext
:
:after
{
content:
" "
;
position:
absolute
;
top:
50
%
;
left:
100
%
;
/*
Tooltip Right
*/
margin-top:
5
px
;
border-width:
5
px
;
border-style:
solid
;
border-color:
transparent
transparent
transparent
black
;
}
Try it »
Fade effect
We can use the CSS3 transition property and the opacity property to achieve a fade-in effect for the tooltip:
Fade-in Effect:
.tooltip
.tooltiptext
{
opacity:
0
;
transition:
opacity
1
s
;
}
.tooltip
:hover
.tooltiptext
{
opacity:
1
;
}
Try it »