How can I create a custom CSS tooltip?

Tooltips are a valuable addition to any website, providing users with helpful information or clarifications without cluttering the user interface. While there are many JavaScript libraries available to create tooltips, you can also create a custom CSS tooltip without the need for any additional libraries. In this step-by-step guide, we will explore how to create a custom CSS tooltip that is simple, lightweight, and customizable. By the end of this tutorial, you’ll have a fully functional CSS tooltip that you can easily integrate into your web projects.

1. HTML Structure

Let’s start by setting up the HTML structure for our tooltip:

<!DOCTYPE html>
<html>
<head>
  <title>Creating a Custom CSS Tooltip</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="tooltip-container">
    <span class="tooltip-trigger">Hover over me</span>
    <div class="tooltip-content">This is a custom CSS tooltip</div>
  </div>
</body>
</html>

In this example, we have a .tooltip-container that wraps the content we want to trigger the tooltip. Inside the container, we have an .tooltip-trigger element that users will hover over to display the tooltip. The actual tooltip content is placed within the .tooltip-content element, which will be hidden by default and only displayed when the user hovers over the trigger element.

2. Basic CSS Styling

Now let’s add some basic CSS styles to create the appearance of the tooltip:

/* styles.css */

/* Reset default list styles */
ul, li {
  list-style: none;
  padding: 0;
  margin: 0;
}

/* Tooltip container styles */
.tooltip-container {
  position: relative;
}

/* Tooltip content styles */
.tooltip-content {
  position: absolute;
  z-index: 1;
  display: none;
  background-color: #333;
  color: #fff;
  padding: 8px;
  border-radius: 4px;
}

/* Tooltip trigger styles */
.tooltip-trigger {
  cursor: pointer;
}

/* Show tooltip content on hover */
.tooltip-container:hover .tooltip-content {
  display: block;
}

In this CSS code, we reset the default list styles to avoid any unwanted formating. We then set the .tooltip-container to have a position: relative, which will allow us to position the tooltip content relative to this container.

The .tooltip-content is set to position: absolute, making it independent of the normal document flow. It is initially hidden with display: none and will only be displayed when the user hovers over the .tooltip-container.

The .tooltip-trigger is styled with cursor: pointer to indicate interactivity.

Finally, we use the .tooltip-container:hover .tooltip-content selector to show the tooltip content when the user hovers over the .tooltip-container.

3. Positioning the Tooltip

By default, the tooltip will appear below the trigger element. However, we can customise its position to show up in different directions, such as above or to the left or right. Here’s how to do it:

Positioning the Tooltip Above the Trigger:

/* Position the tooltip above the trigger */
.tooltip-content {
  bottom: 100%;
}

In this example, we position the tooltip above the trigger by setting bottom: 100%. This places the bottom edge of the tooltip content 100% above the bottom edge of the tooltip container.

Positioning the Tooltip to the Left:

/* Position the tooltip to the left of the trigger */
.tooltip-content {
  right: 100%;
}

In this example, we position the tooltip to the left by setting right: 100%. This places the right edge of the tooltip content 100% to the left of the right edge of the tooltip container.

Positioning the Tooltip to the Right:

/* Position the tooltip to the right of the trigger */
.tooltip-content {
  left: 100%;
}

In this example, we position the tooltip to the right by setting left: 100%. This places the left edge of the tooltip content 100% to the right of the left edge of the tooltip container.

4. Styling the Tooltip

Now that we have the basic tooltip functionality, we can enhance its appearance with additional styling. Let’s add some styling to make it visually appealing:

/* Additional tooltip styling */
.tooltip-content {
  background-color: #333;
  color: #fff;
  padding: 8px;
  border-radius: 4px;
  font-size: 14px;
  width: 160px;
  text-align: center;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.tooltip-trigger {
  border-bottom: 1px dotted #999;
}

.tooltip-trigger:hover {
  border-bottom: 1px dotted transparent;
}

In this CSS code, we add styling to the .tooltip-content to set the background colour, text colour, padding, border-radius, font size, width, and text alignment. We also add a subtle box shadow to give the tooltip a slight elevation.

For the .tooltip-trigger, we add a dotted border-bottom to visually indicate the trigger element. On hover, we hide the border by setting it to transparent.

5. Adding Arrow to the Tooltip

To make the tooltip visually distinct from the trigger element, we can add a small arrow to indicate the connection between the tooltip and the trigger. Here’s how to do it:

/* Add arrow to the tooltip */
.tooltip-content {
  /* Existing styles */

  /* Position the arrow */
  margin-bottom: 8px;
  position: relative;
}

.tooltip-content::before {
  content: '';
  position: absolute;
  bottom: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: transparent transparent #333 transparent;
}

In this CSS code, we first add some space below the tooltip content using margin-bottom: 8px. This creates space between the tooltip and the trigger element.

We then use the ::before pseudo-element to create the arrow. The content: '' property is necessary to create a pseudo-element. We position the arrow above the tooltip content by setting bottom: 100% and left: 50% to centre it.

The arrow’s appearance is created using the border property. We set the border width to 5px to create a triangle shape. The border colour is transparent on the top, left, and right sides, and it matches the tooltip’s background colour (#333) at the bottom, creating the illusion of an arrow.

6. Customising the Tooltip

Now that we have a fully functional custom CSS tooltip, you can easily customise it to match your website’s design. You can change the tooltip’s background colour, text colour, font size, arrow colour, and other properties to suit your preferences.

/* Customising the tooltip */
.tooltip-content {
  background-color: #007bff; /* Change the background color */
  color: #fff; /* Change the text color */
  font-size: 16px; /* Change the font size */
}

.tooltip-content::before {
  border-color: transparent transparent #007bff transparent; /* Change the arrow color */
}

In this example, we customise the tooltip’s appearance by changing the background colour to #007bff, the text colour to white, and the font size to 16px. We also update the arrow colour to match the new background colour.

Feel free to experiment with different colours, fonts, and sizes to create the perfect tooltip for your website.

7. Accessibility Considerations

When creating custom tooltips, it’s essential to consider accessibility to ensure that all users can access the tooltip content. For users with screen readers or other assistive technologies, it’s crucial to provide alternative methods to access the tooltip information.

One way to address accessibility is to include a visually hidden text element with the tooltip content. This hidden text will be accessible to screen readers, providing the necessary information to users who cannot see the tooltip. Here’s how to do it:

/* Accessibility for screen readers */
.visually-hidden {
  position: absolute;
  clip: rect(1px, 1px, 1px, 1px);
  padding: 0;
  border: 0;
  height: 1px;
  width: 1px;
  overflow: hidden;
}

In this CSS code, we create a class called .visually-hidden, which hides the element from the visual layout by positioning it off-screen with clip: rect(1px, 1px, 1px, 1px). The element will still be accessible to screen readers, providing the necessary information.

Now, let’s update the HTML to include the visually hidden text:

<div class="tooltip-container">
  <span class="tooltip-trigger">Hover over me</span>
  <div class="tooltip-content">
    This is a custom CSS tooltip
    <span class="visually-hidden">This tooltip provides additional information.</span>
  </div>
</div>

In this example, we have added a .visually-hidden span element inside the .tooltip-content div. This span includes the text “This tooltip provides additional information,” which will be accessible to screen readers but not visible on the screen.

8. Conclusion

Creating a custom CSS tooltip is a fantastic way to enhance the user experience on your website. With a few lines of CSS code, you can create a visually appealing and lightweight tooltip that provides valuable information to your users.

By following this step-by-step guide, you have learned how to set up the HTML structure, style the tooltip, customise its appearance, and add an arrow for better visual indication. You have also considered accessibility by including a visually hidden text element for screen readers.

Feel free to further customise the tooltip to match your website’s design and create a delightful experience for your users. Experiment with different colours, fonts, and positioning to find the perfect fit for your custom CSS tooltip. With this new knowledge, you can confidently create and implement tooltips that add value and interactivity to your web projects.

Scroll to Top