How to create a tooltip in CSS
A tooltip is a GUI element that is used in the user interfaces of our applications. Its primary purpose is to enhance interactivity and user experience by providing additional information through pop-up messages. These tooltips appear when users hover their mouse cursor over specific elements, such as buttons, links, or icons.
In this Answer, we will learn how we can create a tooltip in CSS.
CSS properties for tooltip effect
To create a tooltip in CSS, we define a tooltip container div and add our tooltip element inside it. We use CSS properties to add the tooltip effect to our tooltip element in the container. These include:
Position: We set our tooltip container's position to relative and our tooltip element's position to absolute. This allows the tooltip to be positioned relative to its parent container.
Visibility: We set the visibility of the tooltip element to
hiddenso it only becomes visible when the container is hovered over.:hoverselector: We use the hover selector on our tooltip container to make the tooltip element visible whenever the hover event is triggered.
The rest of the CSS properties are optional and depend on how we want to style our tooltip container and element.
Code example
Let's understand how to create a tooltip in CSS using the code below
Code explanation
In index.html :
Lines 8–14: We define a container
tooltip-containerthat holds a<p>element and aspanelement that acts as our tooltip element.
In styles.css :
Lines 1–3: We center all the textual page content horizontally using the
text-alignproperty.Lines 5–11: We apply styling to the
tooltip-container. We set itspositiontorelativeso the tooltip container acts as a reference for the tooltip element. We apply the other styling details just for the appearance of our container.Lines 13–22: We apply styling for the
tooltipelement. We set itspositiontoabsoluteso it is positioned with respect to its parent container. Moreover, we set thevisibilitytohiddenso that initially, our tooltip is invisible, and we can only see it when we hover over the parent container.Lines 24–27: We use the
:hoverselector to define that when we hover over thetooltip-container, thetooltipwithin it should become visible. We ensure this behavior by setting thevisibilityproperty tovisible.
Positioning of the tooltip
Now we know how we can create a basic tooltip in CSS, let's discuss the positioning of the tooltip relative to its container. There are four possible positions where we can set our tooltip:
Right
Left
Top
Bottom
Right position
To position our tooltip on the right side of its container, we use the left property and set it to 100%. This makes the tooltip start exactly where the container's right edge ends, effectively placing it on the right side. If we apply padding to the container, we adjust the value of the top property to center the tooltip vertically.
Left position
To position our tooltip on the left side of its container, we use the right property and set it to 100%. This makes the tooltip start exactly where the container's left edge ends, effectively placing it on the left side. If we apply padding to the container, we adjust the value of the top property to center the tooltip vertically.
Top position
To position our tooltip on the top of its container, we use the bottom property and set it to 100%. This makes the tooltip start exactly where the container's top edge ends, effectively placing it on the top. We use the left property to center the tooltip horizontally.
Bottom position
To position our tooltip on the bottom of its container, we use the top property and set it to 100%. This makes the tooltip start exactly where the container's bottom edge ends, effectively placing it on the bottom. We use the left property to center the tooltip horizontally.
Conclusion
A tooltip in CSS is a useful and effective way to add additional information to the already existing content in our web applications. It enhances the user experience by offering more details without cluttering the interface.
Free Resources