Open In App

Building Tooltip using CSS

Last Updated : 30 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A Tooltip provides users with additional information about an element when the mouse pointer hovers over it. For example, when a user hovers over a button labeled “GeeksForGeeks,” additional information such as “A Computer Science Portal” can appear as a tooltip.

Tooltip Positions

Tooltips can be positioned in various ways depending on the desired visual effect and user experience. There are four common positions for tooltips:

  1. Right Tooltip
  2. Left Tooltip
  3. Top Tooltip
  4. Bottom Tooltip

1. Right Tooltip

In the right tooltip, the tooltip appears to the right of the hovered element. The left and top properties are used to position the tooltip. For a right tooltip, set the left property to (100 + x)% and the top property to (0 + y)%.

Example: In this example, we are building the right tooltip using CSS

Output:

2. Left Tooltip:

The Right and Top properties of CSS are used to place the tooltip left to the hoverable text. The value of Right should be to set (100+x)% to make it appear to the left of the container element (if x=0 then tooltip will touch the hoverable text) and the value of top should be set to (0+y)% to adjust the distance from top end of the container element.

Example: Below program illustrate the above approach

Output:

3. Top Tooltip:

The Bottom and Left properties of CSS are used to place the tooltip top to the hoverable text. The value of Bottom should be to set (100+x)% to make it appear to the top of the container element (if x=0 then tooltip will touch the hoverable text) and the value of left should be set to (0+y)% to adjust the distance from left end of the container element.

Example: Below programs illustrate the above approach

Output:

4. Bottom Tooltip:

The Top and Left properties of CSS are used to place the tooltip bottom to the hoverable text. The value of Top should be to set (100+x)% to make it appear to the bottom of the container element (if x=0 then tooltip will touch the hoverable text) and the value of left should be set to (0+y)% to adjust the distance from left end of the container element.

Example: Below program illustrate the above approach:

Output:

Tooltip with Arrows

To make the tooltip look like a speech bubble, we can add an arrow using the ::after pseudo-element. Depending on the position of the tooltip, the arrow will appear on the corresponding side.

Example: Bottom Arrow Tooltip

Example: In a bottom arrow tooltip, the arrow appears below the tooltip text. The top and left properties are used to position the arrow.

Output:

2. Top Arrow:

Bottom and Left are used to place the arrow at the top of the tooltip. The value of Bottom should be to set (100+x)% to make it appear to the top of the tooltip (if x=0 then the arrow will touch the tooltip) and the value of left should be set to (0+y)% to adjust the distance from left end of the tooltip (if y=50 then arrow will be in the middle of tooltip).

Example: Top Arrow Tooltip


Output:

3. Left Arrow:

Top and Right are used to place the arrow at the left of the tooltip. The value of Right should be to set (100+x)% to make it appear to the left of the tooltip (if x=0 then the arrow will touch the tooltip) and the value of top should be set to (0+y)% to adjust the distance from top end of the tooltip (if =50 then arrow will be in the middle of tooltip).

Example: Left Arrow Tooltip


Output:

4. Right Arrow:

Top and Left are used to place the arrow at the right of the tooltip. The value of Left should be to set (100+x)% to make it appear to the right of the tooltip (if x=0 then the arrow will touch the tooltip) and the value of top should be set to (0+y)% to adjust the distance from top end of the tooltip (if y=50 then arrow will be in the middle of tooltip).

Example: Right Arrow Tooltip


Output:



Next Article

Similar Reads