Create tooltips with CSS

The tooltip element of the web-page is useful to provide some information about other elements (mostly form fields). We cannot give all the information about the element in the label due to obvious reasons. Therefore, the tooltip specifies the additional information to help the user understand the elements every time they hover the mouse over it.

Basic Tooltip

The basic tooltip is created by adding an inline element inside a container tag and making it hidden. The said inline element (like ) will have its visibility set to hidden. It will only appear when we hover the mouse over its container element, displaying the text written on it. To stylize it more, you can give desired, color, font, border-radius etc.

Positioning Tooltips

The positioning of the tooltip can be done with respect to its target element. For example, if you want the tooltip to appear at right of the containing element, you can keep the left property as >=100% so that the tooltip doesn’t hide the actual element. Also, make sure that the z-index for the tooltip is positive so that it’s not hidden by other elements on the page.

Tooltip Arrows

We can also create and arrow to specify the target element for the tooltip. In order to do this, we’ll first have to identify the side of the tooltip where the arrow has to be added. The arrow can be created on that side by adding “empty” content after tooltip using the pseudo-element class ::after and the content properties (like borders).

Fade In Tooltips

You can also animate the tooltip by giving the fade in effect. This can be done by increasing the opacity of the tooltip element over a specific duration. Thus, when you hover over the target element, the tooltip will appear in a fade in manner. You can set the speed curve for the animation as per your desire.