Creating CSS-Only Tooltips with Arrow: A Guide

Over-the-shoulder view of a person coding on a laptop

A tooltip is an interactive design enhancement that allows users to access supplementary information when browsing a webpage. This guide will demonstrate how to construct elegant tooltips utilizing only Cascading Style Sheets (CSS) languages. These tooltips usually appear when users hover over an item with their mouse pointer. Creating CSS-Only Tooltips with Arrows can be a valuable addition to your web design skills. In a similar vein, understanding how to maintain aspect ratios using CSS offers further control over your web layouts.

HTML has an in-built feature that permits the user to display a native tooltip using the ‘title’ attribute. When the user hovers over an item with the title attribute, the tooltip will surface following a minor delay.

Creating Tooltips Using CSS

Let’s explore how to create a stylish tooltip for an <abbr> element in HTML. Tooltips are handy for providing users with additional information about an abbreviation or acronym.

Initially, we have a simple <abbr> element like this:

<abbr>HTML</abbr>

Before proceeding, let’s make some adjustments to the existing HTML:

We will assign a “tooltip” class to select it and apply styles through CSS. Additionally, we’ll introduce the “data-tooltip” attribute to store the actual tooltip content, resulting in the following structure:

<abbr class="tooltip" data-tooltip="HyperText Markup Language">HTML</abbr>

To craft a CSS-only tooltip, we’ll make use of a pseudo-element. This pseudo-element is equipped with a “content” attribute, which can store the information to be displayed within the tooltip. We can provide the desired text to populate the tooltip content.

content: "HyperText Markup Language";

For reusability across multiple elements, we can establish a connection between the pseudo-element’s “content” attribute and the HTML attribute of the respective element using the “attr()” function.

content: attr(data-tooltip);

Tooltip Styling

Before proceeding with the styling of the tooltip, let’s begin by defining the positioning rule for the “tooltip” class. This step ensures that our tooltip will be correctly positioned as intended. Additionally, we can include a dotted underline (note that the <abbr> tag may already have this style by default in certain browsers) and apply a help cursor to indicate to users that supplementary information is accessible.

.tooltip {
  position: relative;
  text-decoration: underline dotted;
  cursor: help;
}

The appearance of the pseudo-element will be as follows:

.tooltip::before {
  content: attr(data-tooltip);
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translate(-50%);
  margin-bottom: 15px;
  color: #fff;
  background: rgba(0,0,0, .7);
  border-radius: 5px;
  padding: 5px;
}

Designing the Tooltip Arrow

To craft the tooltip arrow, we can fashion a triangle shape using the borders of the second pseudo-element.

.tooltip::after {
  position: absolute;
  content: "";
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 7px solid rgba(0,0,0, .7);
}

Adding Animation to the Tooltip

To enhance the interactivity of our tooltip, we can introduce a subtle animation effect. By applying a minimal transition effect and setting the opacity to zero for both pseudo-elements, we can achieve a smooth fade-in animation when the tooltip is hovered over.

.tooltip::before,
.tooltip::after {
  opacity: 0;
  visibility: hidden;
  transition: opacity .3s ease-in-out;
}

.tooltip:hover::before,
.tooltip:hover::after {
  opacity: 1;
  visibility: visible;
}

Customizing Tooltip Placement

A workspace with a computer displaying code and design tools

In the given example, the tooltip is positioned above the element. Nevertheless, this may not always be the desired arrangement. There could be instances where you’d prefer to display the tooltip below, to the left, or to the right of an element.

To cater to such flexibility, we can implement modification classes following the BEM (Block Element Modifier) principle. As a result, we will have a total of four additional classes at our disposal, allowing us to easily configure the positioning of the tooltip as needed:

  1. tooltip–top;
  2. tooltip–bottom;
  3. tooltip–left;
  4. tooltip–right;
.tooltip--top::before,
.tooltip--top::after {
  bottom: 100%;
  left: 50%;
  transform: translate(-50%);
  margin-bottom: 15px;
}

.tooltip--top::after {
  margin-bottom: 8px;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 7px solid rgba(0,0,0, .7);
}

.tooltip--bottom::before,
.tooltip--bottom::after {
  top: 100%;
  left: 50%;
  transform: translate(-50%);
  margin-top: 15px;
}

.tooltip--bottom::after {
  margin-top: 8px;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 7px solid rgba(0,0,0, .7);
}

.tooltip--right::before,
.tooltip--right::after {
  top: 50%;
  left: 100%;
  transform: translate(0, -50%);
  margin-left: 15px;
}

.tooltip--right::after {
  margin-left: 8px;
  border-top: 5px solid transparent;
  border-right: 7px solid rgba(0,0,0, .7);
  border-bottom: 5px solid transparent;
}

.tooltip--left::before,
.tooltip--left::after {
  top: 50%;
  right: 100%;
  transform: translate(0, -50%);
  margin-right: 15px;
}

.tooltip--left::after {
  margin-right: 8px;
  border-top: 5px solid transparent;
  border-left: 7px solid rgba(0,0,0, .7);
  border-bottom: 5px solid transparent;
}

Conclusion

With the knowledge shared in this guide, creating a CSS tooltip arrow becomes effortless. Remember every detail is vital, from the creation process to styling, animating, and positioning. Moreover, understanding the importance of tooltip accessibility and recognizing the benefits of a customized CSS tooltip can heighten the user’s browsing experience. So, equip your web design toolkit with this skill and bring an enhanced level of interactivity and aesthetics to your websites.