What is Title Attribute in HTML: Definition and Usage
title attribute in HTML adds extra information to an element that appears as a tooltip when you hover over it with a mouse. It helps provide helpful hints or descriptions without cluttering the page visually.How It Works
The title attribute works like a small note attached to an HTML element. When you move your mouse pointer over that element, a small box with the note's text appears. This is similar to how a sticky note on a book page gives you extra info without changing the main text.
This tooltip helps users understand more about the element, such as explaining a link's destination or describing an image. It is supported by all modern browsers and works automatically without extra code.
Example
This example shows a link with a title attribute. When you hover over the link, the tooltip text appears.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Title Attribute Example</title> </head> <body> <a href="https://www.example.com" title="Visit Example website">Example Link</a> </body> </html>
When to Use
Use the title attribute to give extra helpful information about an element without adding visible text. It is great for:
- Explaining links, especially if the link text is short or unclear.
- Describing images or icons for better understanding.
- Providing hints for buttons or form fields.
However, do not rely on it for important information because some users may not see tooltips, especially on touch devices or screen readers.
Key Points
- The
titleattribute shows a tooltip on mouse hover. - It adds extra info without cluttering the page.
- Not all users or devices display tooltips.
- Use it for helpful hints, not critical content.
Key Takeaways
title attribute adds a tooltip with extra info on hover.title.title for helpful hints to improve user experience.