CSS Tooltips

CSS Tooltips

Have you ever wanted to create toolips with just CSS code? Like many people the answer is yes. In this tutorial we will go through the process and show you exactly how this can be done without using any javascript or images, only pure CSS code. Firstly let’s explain what a tooltip is: A tooltip is a brief message that will appear when you hover over a link, this message might be a simple sentence explaining where the link will take us or just stating some relevant information about the link.

In this tutorial we will cover the basics of creating the CSS tooltips, I will write another tutorial later about adding animations, borders and shadows to the tooltips and different positions for the arrow on the tooltip. Also hopefully within a few weeks, we will be adding a CSS tooltip generator to this website, you’ll then be able to create your own tooltips with ease.

OK, let’s get on with showing you how to create these wonderful tooltips as shown in the image below.

position: absolute; – Our tooltip will be have a position of absolute.left: 50%; – This will position our tooltip centered at the end of our element.width: 140px; – Our tooltip container will have a width of 140px.padding: 6px; – There will be padding around our text of 6px.margin-left: -76px; – This number will center our tooltip with the element.background: #000; – The background of our tooltip will be black.color: #fff; – The text color will be white.text-align: center; – The text in the tooltip will be centered.visibility: hidden; – This will set our tooltip container to hidden.border-radius: 5px; – This will give our container a border radius of 5px.

Step 3

Now that we have created our tooltip container, we can now add a triangle to the tooltip using the :after selector.

content: ”; – Sets the content to nothing.position: absolute; – The arrow will be positioned absolute.top: 100%; – Positions the arrow at the bottom of the tooltip.left: 50%; – The arrow will be in the center of the tooltip.margin-left: -8px; – Because our arrow is 16px wide, we need to shift the arrow -8px to get it back to center.width: 0; height: 0 – Sets the width and height to zero.border-top: 8px solid #000; – Gives the arrow a background of black.border-right: 8px solid transparent; – Used to create the arrow.border-left: 8px solid transparent; – Used to create the arrow.

Step 4

Finally we need to set the properties for when the user hovers over the link element.

visibility: visible; – Shows the tooltip when the user hovers over the link.opacity: 0.8; – Gives the tooltip an opacity of 0.8bottom: 30px; – Moves the tooltip to above the element.z-index: 999; – The z-index will make sure that the tooltip is in front of other elements.

Step 5

All of our CSS is now completed, with the code above that is all that is required for the style of the tooltip, however, we now need to add some HTML to show you how to achieve the tooltip when a user hovers over a link element.
All we need to do is add the .tooltip class to our link to activate our tooltip, plus will need to add a <span> element for the text used in our tooltip.

Conclusion

That is all, I hope you enjoyed reading this tutorial about CSS tooltips, you can view a demo of the above code by clicking on the demo button below or you can download the entire source code for the tooltips by clicking on the download button.

About Rene Spronk

My name is Rene Spronk and I am the owner of CSSPortal. I started getting interested in CSS when I designed my first website, I loved how you could control so many aspects of the page with CSS. Since then I have developed many websites, but this site is still my favourite as it helps others to learn CSS.