GUIs have made monumental leaps since the inception of the humble Sketchpad. However, it’s the simplest tools that often spruce up your website from mediocrity to excellence. One such GUI element that holds this distinction is the humble tooltip. For the uninitiated, the tooltip is the little hovering box that pops up when you float your cursor over an item without clicking on it. A tooltip is typically used to provide information on an item or instructions to its purpose.
At first glance, tooltip might seem like an inelegant entity not worth investing time in when creating your website. On the contrary, it is like the last Lego block needed to complete your model set: a small piece without which the whole model falters. Tooltip allows visitors of your website to stay on the same page when browsing thus effectively eliminating the need for a ‘Help’ section. This enhances user experience and ensures that the point you are communicating through your website is delivered seamlessly without any needless interruptions. Moreover, they are a fine addition as it does not crowd out the content you are presenting whilst giving the viewer the choice to make use of the tooltip. These minute improvements in user experience could lead to a positive cumulative effect at the end of the day. I have outlined the methods to build your own CSS only tooltip below. I have chosen to demonstrate using CSS over JS to reduce the unwanted JavaScript overhead.
How to Build a DIY CSS tooltip?
You don’t have to rummage through StackExchange to come across a decent free tooltip.
What if I say you can make one yourself?
Here’s a DIY CSS tooltip that you can use. Alternatively, you can use SASS or Less, as per your preference.
1. Define HTML elements with tooltip related attributes
You can define any HTML elements such as button, anchor, span, and then add the attribute to enable a tooltip on them. In the following example, I’ve added tooltip-title
attribute to the elements.
2. Add styling to your tooltip.
We are styling the tooltip in the :after
pseudo-element of the selected element using CSS.
3. Advanced tooltip features (optional).
3.1 Position your tooltips
In addition to its looks, you can define the position of the tooltip as well. For instance, top, bottom, left, right, top-right, top-left, bottom-right, bottom-left, etc.
Shown below is an example of making a tooltip display according to the position mentioned in the tooltip-position
attribute. ( tooltip-position='bottom'
is shown as the example, you can do it similarly with other possible positions.)
3.2 Colour your tooltips
You can define a color to your tooltip depending on the message you are displaying (example: success/green, warning/yellow, error/red, etc.)
In the example below, I’m adding tooltip-color
attribute to the element to define the required color. ( tooltip-color='warning'
is shown as the example, you can do it similarly with other possible colors.)
3.3 Size your tooltips
You can define size to your tooltip depending on the length of the message you are displaying (example: small, medium, large, extra-large, auto-width, etc.)
In the below example I’m adding the tooltip-length
attribute to the element to define the width of the tooltip. ( tooltip-length='lg'
is shown as the example, you can do it similarly with other possible lengths.)
You can have a look at the working demo of the above example here: Tooltip Preview
Also, I’ve created an open-source tooltip library called SpikeTip, which you can use in your website/ fork/ and then modify it the way you want.
For those of you who want to directly incorporate the tooltip into your projects, I have created an NPM package and you’ll find the installation guide here: SpikeTip — NPM Package.
Tooltips would definitely be a pertinent addition to your website and I hope the DIY mini-tutorial above helped you to create stylized tooltips of your choice.
Originally published at https://vishnubaliga.com.
Top comments (11)
I've started a whole series of CSS-only web design patterns like this. Coincidentally, tooltips was the first installment: dev.to/kallmanation/building-a-too...
Cool to see people thinking the same way; good article!
Thank you so much! 😊
Looking forward to your upcoming series, good luck with it.
You should add
focus
triggers to show the tooltip as well. It's important to remember that not everyone uses a mouse or similar pointing device. Adding focus triggers makes it more accessible. Test with screen readers though, some might need additionalaria-*
attributes to read out the tooltip in the most convenient way for the user.Thank you for mentioning about
a11y
, That is an important factor in making web accessibility easier.Having said that,
aria-*
can be added directly to the HTML element instead of the tooltip, because here, we are creating a tooltip using CSS alone, without any js overhead.Not all keyboard users are screen reader users, so aria won't help them. For example, somebody with Parkinson's or a broken wrist might be using a keyboard as they struggle with a mouse or similar pointing device. For these users, using focus styles can mean the tooltip is available to them.
I completely agree with you.
It's up to the developer to decide who is the targeted audience, It is very simple to achieve this as well, along with
:hover
he needs to define:focus
as well.I think in terms of accessibility, it's not about who we think our target audience is, but about making sure we're not discriminating against people because we thought they aren't our audience. Like you said, simply using
:focus
to trigger the display of the tooltip should resolve its visibility for those. But there might be some other edge cases where testing with a screen reader should be done too.I would say that you should rename the
tooltip-title
attribute todata-tooltip-title
the general HTML spec doesn't like when sites implement custom attributes that aren't prepended by adata-
.This is because not all browsers work with custom attributes as intended, older browsers might not be able to understand attributes that aren't explicitly defined in the w3c spec unless placed with
data-
. Placingdata-
also future proofs browsers as if there is any change in the future wheretooltip-title
becomes an accepted standard in HTML, this implementation would break.More information here:
[1] w3.org/TR/2011/WD-html5-20110525/e....
[2] developer.mozilla.org/en-US/docs/W...
Thank you for pointing that out. As you said, it is always good to use a custom data attribute.
In the DIY examples mentioned above, I wanted to keep it simple and easy to understand. They can always have their own way of defining the attributes.
spiketip looks really 👌 nice
Glad you liked it! Thank you so much! 😊