Pure CSS Tooltips

Here is a solution for flexible, near-bulletproof pure CSS tooltips. The code uses HTML5 data-* attributes along with attribute selectors and some CSS positioning.

Here is the HTML:

<p class="tt">Lorem ipsum dolor sit amet <a href="#" data-tooltip="This is a pure CSS tooltip">hover over this link</a> elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua ut enim ad minim veniam, <a href="#" data-tooltip="This is a pure CSS tooltip">hover over this link which is longer than the other</a> quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
a[data-tooltip]:link, a[data-tooltip]:visited { position: relative; text-decoration: none; border-bottom: solid 1px; } a[data-tooltip]:before { content: ""; position: absolute; border-top: 1em solid #0090ff; border-left: 1.5em solid transparent; border-right: 1.5em solid transparent; display: none; top: -1em; } a[data-tooltip]:after { content: attr(data-tooltip); position: absolute; color: white; top: -2.5em; left: -1em; background: #0090ff; padding: .25em .75em; border-radius: .5em; white-space: nowrap; display: none; } a[data-tooltip]:hover:before, a[data-tooltip]:hover:after { display: inline; }

Here is a live demo (use the range slider to adjust the font size, which confirms that the tooltip will resize automatically based on the font size):

Some important points to keep in mind:

  • CSS-only tool tips are usually not accessible to assistive technology devices like screen readers, so these should only be decorative and should not be used for vital page content.
  • The fluid size is based on the inherited font size (in the case of the demo, it comes from the <body> element)

Source

Note: To the best of our knowledge, the information above and the snippet are accurate and up to date. However, in case you notice something wrong, please report snippet or leave a comment below.
Back to Snippets

Or start the conversation in our Facebook group for WordPress professionals. Find answers, share tips, and get help from other WordPress experts. Join now (it’s free)!

Most Searched Articles

14 of the Best JavaScript Libraries and Frameworks to Try Out in 2020

In this post, we look at the best JavaScript libraries and frameworks to try out this year. Why? Well, with JavaScript being available in every web browser, this makes it the most accessible programming language of ...

50+ Best Free WordPress Themes for 2020 (Responsive, Mobile-Ready, Beautiful)

If you're looking for only the best free WordPress themes in the market for this year, then you're in the right place. We have more than 60+ such themes for you right ...

10+ Best WordPress Hosting Providers of 2020 Compared & Tested

Looking for the best WordPress hosting that you can actually afford? We did the testing for you. Here are 10 best hosts on the market ...

Handpicked Articles

How to Make a WordPress Website: Ultimate Guide for Beginners and Advanced Users Alike

Many people wonder how to make a WordPress website. They’ve heard about WordPress, its incredible popularity, excellent features and designs, and now they want to join the pack and build a WordPress website of their own. So, where does one get ...

How to Start an eCommerce Business From Scratch (In 10 Steps)

Is 2020 going to be the year you learn how to start an eCommerce business from scratch? You’re certainly in the right place! This guide will give you a roadmap to getting from 0 to a fully functional eCommerce business. ...

Scroll to Top