Tooltips in HTML and CSS



All you need for the HTML is an anchor tag (or any tag you like) with a child element. The child will be hidden by default when the page loads and only shown when the parent is hovered over.


Parent Element

First you need to give the parent a relative value. That way the child element won’t sit at the top of screen but will instead stay “relative” to the parent, which in this case is the anchor link.

Child (Tooltip) Element

The code for the actual tooltip is a little more extensive.

  • Width: You need to give it a set width otherwise it can get a little hairy when the length of the text changes from tooltip to tooltip.
  • Padding: is required to make it more readable, otherwise it may blend too much to the current background.
  • Display: is set to none so you can’t see it on the initial page load.
  • Position: It’s absolutely positioned so it can be inside the parent without affecting any siblings or even the parent itself. The reason we don’t use actual positioning like top:0px; or left:0px; is so we can be more flexible when it comes to having the tooltip at the top, right, bottom or left.
  • Z-index: is set high so it stays above everything else. Anything with z-index automatically sits above everything else however I’ve set it high so it can be above other elements that also have a z-index.
  • Other: The other values are just basic styling to make it look nice. Also I like to put /* Type */ in my CSS files to declare font styles, (in Sass I write it as // Type so it doesn’t get rendered).

Adding the Arrow

We’ll start off with having a base for our arrow that we can then position with more specific values, depending on whether it’s a top, right, bottom or left tooltip.

Top, Right, Bottom and Left

You will realise that the code above doesn’t actually position the tooltip or show the arrow, that’s because it’s easier (and more maintainable) to have a separate class you can add to the end of .tooltip to set the position and show the arrow accordingly.

Now when we need specific placement we just use the html class like so and that’s where it’ll show up.

Why No Default Placement?

The reason is simple, to keep the code modular, reusable and maintainable. Something I’m deeply passionate about. Although one thing to keep in mind about modular, reusable and maintainable code is that it’s up to you to decide what counts as maintainable.

I’m sure Harry Roberts of CSS Wizardry would find my code base and yours quite funny if we were to class it as maintainable, however we might find his overkill.

I respect Harry a lot, he’s actually the reason I code in a maintainable fashion. However one thing I always try to do it what works for me BUT I also make sure the code is to a set standard of maintainability.

In Conclusion

I could hand my code base over to someone else who codes in a completely different way, it may still be maintainable and modular but different all the same. That person can still use my code and edit and experiment with it without hassle.

Thanks for reading this short tutorial on creating tooltips in HTML and CSS. If you’d like more content like this, go ahead and check out the tutorials section!


Facebook Comments

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button