How to Create CSS-Only Tooltips

How to Create CSS-Only Tooltips

When it comes to building interactive user interfaces (UIs), tooltips are a subtle yet essential component that often times get ignored. They can enhance the user experience by providing more information without cluttering the interface too much. While JavaScript is commonly used to add dynamic features, CSS-only tooltips are simpler, faster, and eliminate the need for the extra scripting.

In this article, I'll dive into the world of CSS-only tooltips, exploring how you can create clean and effective tooltips for your UI. I'll cover various use cases, provide a few code examples, and guide you through best practices for creating tooltips that look great and function smoothly with just a couple of lines of CSS.

Hover over me
This is a CSS tooltip!

Why Use CSS-Only Tooltips?

Tooltips offer additional context for UI elements. But why should we create tooltips with only CSS instead of using JavaScript? Here are a few key reasons:

Performance: CSS-based solutions are lightweight and fast, reducing the load on your site.

Simplicity: CSS-only tooltips are easier to implement and require no external dependencies.

Maintainability: With fewer scripts, the code is easier to manage and debug.

Accessibility: Well-designed tooltips provide additional context, improving accessibility without adding bloat.

Let’s get started with a simple example. 

The Basics of CSS Tooltips

Here’s a basic structure for a CSS-only tooltip:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Tooltip</title>
    <style>
        .tooltip {
            position: relative;
            display: inline-block;
            cursor: pointer;
        }

        .tooltip .tooltip-text {
            visibility: hidden;
            width: 120px;
            background-color: #333;
            color: #fff;
            text-align: center;
            border-radius: 6px;
            padding: 5px;
            position: absolute;
            z-index: 1;
            bottom: 125%; /* Position above the element */
            left: 50%;
            transform: translateX(-50%);
            opacity: 0;
            transition: opacity 0.3s;
        }

        .tooltip:hover .tooltip-text {
            visibility: visible;
            opacity: 1;
        }
    </style>
</head>
<body>

<div class="tooltip">Hover over me
    <div class="tooltip-text">Tooltip text</div>
</div>

</body>
</html>

How it works

Tooltip Container: The .tooltip class wraps the element that will trigger the tooltip.

Tooltip Text: The .tooltip-text contains the hidden text that appears when hovering over the trigger element. By default, its visibility is set to hidden and opacity to 0.

Hover Effect: On hovering over the .tooltip class, the .tooltip-text becomes visible by changing its visibility and opacity.

This creates a simple tooltip that shows up on hover.

Positioning Tooltips: Above, Below, Left, and Right

Depending on your UI, you might want to position tooltips differently. Here’s how to place tooltips in various locations, relative to the element that a user is hovering over:

Tooltip Below:

To place the tooltip below the trigger element, modify the positioning:

.tooltip .tooltip-text {
    bottom: -30px;
    left: 50%;
    transform: translateX(-50%);
}

Tooltip Left:

If you prefer to place the tooltip to the left, adjust the CSS like this:

.tooltip .tooltip-text {
    top: 50%;
    left: -150%;
    transform: translateY(-50%);
}

Tooltip Right:

To align the tooltip to the right of the element:

.tooltip .tooltip-text {
    top: 50%;
    left: 110%;
    transform: translateY(-50%);
}

Tooltip Above:

To position the tooltip above the trigger element (which is the default in our earlier example):

.tooltip .tooltip-text {
    bottom: 125%;
    left: 50%;
    transform: translateX(-50%);
}

By adjusting the top, left, right, and bottom properties, you can position your tooltip in any direction. But you might still want to be careful with neighboring elements, as you don't want your tooltips to clutter when it doesn't need to.

Tooltip Styling Best Practices

To make your tooltips visually appealing, follow these best practices:

1. Readable Text:

Ensure that the text is readable by adjusting the font size and padding. Keep tooltips short and to the point and avoid having more than 2-3 words max. Often times, you will see tooltips that are full sentences and that take up considerable space, almost negating the benefits of having them to begin with.

.tooltip .tooltip-text {
    font-size: 14px;
    padding: 8px;
}

2. Accessible Colors:

Use contrasting colors for the background and text to ensure that the tooltip is legible. This is important both for your audience, but also for your SEO as Google might rank you higher if your website follows accessibility best practices.

.tooltip .tooltip-text {
    background-color: #333;
    color: #fff;
}

3. Smooth Transitions:

Smooth transitions add polish to the tooltip, making it appear and disappear more naturally.

.tooltip .tooltip-text {
    transition: opacity 0.3s ease-in-out;
}

Responsive Tooltips

A common issue with tooltips is that they are often designed with desktop users in mind. However, mobile users also need access to these small yet important UI details.

On touch devices, the hover state doesn’t work in the same way as on desktops. Instead, we can use the focus pseudo-class to show tooltips when users tap on elements.

Here’s how you can tweak the tooltip for mobile devices:

.tooltip:focus .tooltip-text {
    visibility: visible;
    opacity: 1;
}

Additionally, you should increase the touch target size for mobile users. A small, non-clickable area can lead to poor UX.

.tooltip {
    padding: 12px;
}

Advanced Tooltip Designs

1. Arrow for the Tooltip:

To give the tooltip a more polished look, you can add an arrow pointing to the element. You can create this arrow using CSS borders.

.tooltip .tooltip-text::after {
    content: '';
    position: absolute;
    bottom: 100%; /* Position above the tooltip */
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: transparent transparent #333 transparent;
}

This creates a small triangle that looks like an arrow, further enhancing the tooltip’s visual appeal.

2. Animated Tooltips:

CSS animations can make your tooltips more eye-catching to users. Here’s an example of a tooltip that slides in from the top:

.tooltip:hover .tooltip-text {
    visibility: visible;
    opacity: 1;
    transform: translateY(-10px); /* Moves the tooltip upward */
}

By animating the transform property, the tooltip will smoothly move into place, providing a refined user experience.

Full sample code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Advanced CSS Tooltip</title>
    <style>
        .tooltip {
            position: relative;
            display: inline-block;
            cursor: pointer;
            padding: 10px;
        }

        .tooltip .tooltip-text {
            visibility: hidden;
            width: 140px;
            background-color: #333;
            color: #fff;
            text-align: center;
            border-radius: 6px;
            padding: 10px;
            position: absolute;
            z-index: 1;
            bottom: 125%;
            left: 50%;
            transform: translateX(-50%);
            opacity: 0;
            transition: opacity 0.3s, transform 0.3s;
        }

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

        .tooltip:hover .tooltip-text {
            visibility: visible;
            opacity: 1;
            transform: translateY(-10px);
        }
    </style>
</head>
<body>

<div class="tooltip">Hover over me
    <div class="tooltip-text">This is a CSS tooltip!</div>
</div>

</body>
</html>

Conclusion

CSS-only tooltips are a powerful tool for creating lightweight, user-friendly interactions in your UI. They are easy to implement, highly customizable, and can be styled to fit the needs of any project.

Whether you're creating tooltips for desktop or mobile interfaces, the flexibility of CSS makes it easy to deliver a polished experience. From simple hover effects to advanced animations, CSS allows you to build engaging tooltips without relying on JavaScript.

Walter G. author of blog post
Walter Guevara is a Computer Scientist, software engineer, startup founder and previous mentor for a coding bootcamp. He has been creating software for the past 20 years.

Get the latest programming news directly in your inbox!

Have a question on this article?

You can leave me a question on this particular article (or any other really).

Ask a question

Community Comments

No comments posted yet

Add a comment