CSS tooltips are a popular UI feature that allows users to get additional information about elements by hovering over them. Tooltips can be used to provide contextual information, feedback or to enhance the user experience. In this article, we will explore everything you need to know about CSS tooltips, including how to create, customize, and make them accessible.
📖 Learn more about CSS Basics 📖
What are CSS Tooltips?
CSS tooltips are small boxes of text that appear when the user hovers over an element, such as a button, link, or image. They can provide additional information about the element, explain its purpose, or give instructions on how to use it. Tooltips are commonly used in web applications, e-commerce sites, and mobile apps to improve the user experience and reduce friction.
Creating Basic Tooltips with CSS
Creating a basic tooltip with CSS is a simple process that requires only a few lines of code. To create a tooltip, you can use the CSS ::before pseudo-element to add a small box of text before or after an element. You can then use CSS to style the tooltip and control its position and appearance.
Here’s an example of a basic tooltip:
/* Add a tooltip to a button */
button::before {
content: "Click me!";
position: absolute;
background-color: #333;
color: #fff;
padding: 5px;
border-radius: 5px;
}
/* Position the tooltip */
button:hover::before {
top: -30px;
left: 50%;
transform: translateX(-50%);
}

In this example, we’re adding a tooltip to a button using the ::before pseudo-element. We’re setting the content of the tooltip to “Click me!”, styling it with a background color, text color, padding, and border radius. We’re then positioning the tooltip above the button using the top
property, centering it horizontally using the left
and transform
properties.
Customizing Tooltips with CSS
While basic tooltips are useful, you can customize them further to match your site’s style and branding. You can change the background color, text color, font size, and other properties to make the tooltip stand out or blend in with the rest of the UI. You can also add animations, borders, and icons to make the tooltip more engaging.
Here are some CSS properties you can use to customize tooltips:
background-color
: Sets the background color of the tooltip.color
: Sets the text color of the tooltip.font-size
: Sets the font size of the tooltip text.padding
: Adds padding to the tooltip box.border-radius
: Rounds the corners of the tooltip box.box-shadow
: Adds a shadow effect to the tooltip box.transition
: Adds a transition effect to the tooltip box.
Here’s an example of a customized tooltip:
/* Add a customized tooltip to a link */
a::before {
content: attr(title);
position: absolute;
background-color: #007acc;
color: #fff;
font-size: 14px;
padding: 8px 12px;
border-radius: 5px;
box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.3);
transition: opacity 0.2s ease-in-out;
opacity: 0;
}
/* Show the tooltip on hover */
a:hover::before {
top: -40px;
left: 50%;
transform: translateX(-50%);
opacity: 1;
}
In this example, we’re adding a customized tooltip to a link using the attr()
function to display the link’s title
attribute as the tooltip text. We’re styling the tooltip with a blue background color, white text color, and a box shadow effect. We’re also adding a transition effect to the tooltip’s opacity property to create a smooth appearance and disappearance effect. Finally, we’re positioning the tooltip above the link and centering it horizontally using the top
, left
, and transform
properties.
Making Tooltips Accessible
While tooltips can be a useful feature for many users, they can also pose a challenge for users with disabilities or who rely on assistive technology to navigate the web. To make tooltips accessible, it’s important to follow best practices for web accessibility and ensure that tooltips are usable by keyboard-only users, screen reader users, and users with visual impairments.
Here are some best practices for making tooltips accessible:
- Use
aria-label
oraria-describedby
to associate the tooltip with its target element. - Use descriptive and concise tooltip text that is easy to understand.
- Ensure that tooltips are keyboard accessible and can be triggered without a mouse.
- Provide users with the ability to dismiss or close the tooltip using the keyboard.
- Use CSS to create a clear visual distinction between tooltips and other UI elements.
Here’s an example of an accessible tooltip:
<!-- Add an accessible tooltip to a button -->
<button aria-label="Learn more about our product">
Learn More
<span id="tooltip">Our product is a great solution for your needs!</span>
</button>
In this example, we’re using the aria-label
attribute to associate the tooltip with the button element. We’re also using a nested span element to contain the tooltip text and adding an id
attribute to the span element. We can then use CSS to style the tooltip and position it above the button using the top
, left
, and transform
properties.
Conclusion
CSS tooltips are a powerful and versatile UI feature that can help users get more information about elements and improve the overall user experience. By following best practices for creating, customizing, and making tooltips accessible, software developers can create engaging and usable interfaces that meet the needs of all users. With the tips and examples provided in this article, you can get started with creating your own CSS tooltips and take your UI design to the next level.
📕 Related articles about CSS
- How to make website HTML and CSS
- Understanding CSS Backgrounds: Techniques and Best Practices
- CSS How to Make Responsive: Techniques You Need to Know
- How to create CSS animations
- CSS How to Make a Class: A Comprehensive and Detailed Guide
- How to style forms with CSS