How to create Tool tips using Materialize CSS

Hey, friend welcome come back to the site, if it's your first time then feel at home because you're in the right place.

In this simple post, I will be showing you how you can create or add simple tooltips on your SITE or WEB APP using materialize CSS.

Before we even go further, the live example is already infront of your eyes. Try hovering over those truncated article titles on your right handside(LEARN MORE SECTION) or at the bottom of the page(POSTS YOU MAY LIKE) if you are using a mobile device. Those are what we call the tooltips.

What are tooltips?

Tooltips are small, interactive, textual hints for mainly graphical elements.

How are they(tooltips) useful?

When using icons for actions you can use a tooltip to give people clarification on its function. For example delete button, Edit button, etc.

Watch Tutorial
How are they created?

You can create a tooltip by adding the .tooltipped class to your element. Moreso, there are different data attributes you can add on the element and specify it's position, that is to say, either on the top, bottom, left, right and this is set using the data-position attribute.

Markup
copy
	
//data-position can be : bottom, top, left, or right
//data-tooltip defines the tooltip text you want to appear
  <a class="btn tooltipped" data-position="top" data-tooltip="I am a tooltip">Hover me!  </a>
     
Initialization

But remember for any materialize CSS widget or component to work, you need to initialize;

copy

//waits for the document to load before running the code inside
document.addEventListener('DOMContentLoaded', function() {

//Create a variable and assign it to any element with the tooltipped class.
	const hint_tip = document.querySelectorAll('.tooltipped');

//Then use the materialize instance(M) to initialize it.
	 M.Tooltip.init(hint_tip, {});
});
	

Options

As you can see in the last line we passed in an object and this takes in key pair values(WATCH VIDEO TO UNDERSTAND THIS).

Or...Visit the official documentation page for the tooltips and learn more: https://materializecss.com/tooltips.html

collections_bookmark Category :: Materialize css
date_range Published :: 2 months ago At: 01:23 PM
person Writer :: Code

YOU MAY LIKE THESE POSTS