Global attributes are attributes that can be applied to any HTML element. They provide additional information and functionality, enhancing the usability and accessibility of web pages.
Here are some of the most commonly used global attributes in HTML:
Attribute | Description | Example |
---|---|---|
class | Specifies one or more class names for an element. It is used to apply CSS styles and JavaScript. |
Try yourself
|
id | Specifies a unique id for an element. It is used to apply CSS styles, JavaScript, and anchor links. |
Try yourself
|
style | Specifies inline CSS styles for an element. |
Try yourself
|
title | Provides additional information about an element, often displayed as a tooltip. |
Try yourself
|
data-* | Used to store custom data private to the page or application. |
Try yourself
|
There are several other global attributes available in HTML:
Let's look at some detailed examples of how these global attributes can be used:
Example: Using the class Attribute
<p class="styled-paragraph">This paragraph is styled with a class.</p>
<p class="styled-paragraph">This is another paragraph with the same class.</p>
This example demonstrates how to use the class attribute to apply CSS styles to an element.
<p id="unique-paragraph">This is a uniquely styled paragraph.</p>
<a href="#unique-paragraph">Go to the uniquely styled paragraph</a>
This example shows how to use the id attribute to uniquely identify an element and apply styles or JavaScript to it.
<div style="color: green; font-size: 20px;">This is a div with inline styles.</div>
This example illustrates how to use the style attribute to apply inline CSS styles to an element.
<div title="This is a tooltip">Hover over this div to see the tooltip.</div>
This example demonstrates how to use the title attribute to provide additional information about an element.
<div data-user-id="12345" data-role="admin">This div contains custom data attributes.</div>
This example shows how to use the data-* attribute to store custom data within an element.
HTML global attributes are versatile and can be applied to any HTML element to provide additional functionality and information. They are essential for enhancing the usability, accessibility, and interactivity of web pages.