id
: Assigns a unique identifier.class
: Specifies one or more class names.style
: Applies inline CSS.title
: Provides additional information as a tooltip.data-*
: Custom data attributes.<div id="main-content">This is unique.</div>
<p class="text-bold">This is bold text.</p>
<h1 style="color:blue;">This is a blue heading.</h1>
<button title="Click to Submit">Submit</button>
<div data-user-id="123">User Info</div>
onclick
: Triggers a function when clicked.onmouseover
: Executes when the mouse pointer is over an element.onchange
: Triggers when the value of an input changes.onload
: Executes JavaScript when a page or an image loads.onsubmit
: Triggers when a form is submitted.<div onmouseover="this.style.backgroundColor='yellow';">Hover over me!</div>
<input type="text" onchange="alert('Value changed!')">
<body onload="alert('Page loaded!')"></body>
<button onclick="alert('Button clicked!')">Click Me</button>
<form onsubmit="alert('Form submitted!')">...</form>
Example :
<a href="https://example.com">Visit Example</a>
Example :
<a href="https://example.com" target="_blank">Open in new tab</a>
Example :
<a href="https://example.com">Visit Example</a>
Example :
<a href="https://example.com" rel="nofollow">NoFollow Link</a>
Example :
<img src="image.jpg" alt="Descriptive text">
Example :
<img src="image.jpg" alt="A beautiful landscape.">
Example :
<img src="image.jpg" width="300" height="200">
Example :
<input type="text" placeholder="Enter your name">
Example :
<input type="email" placeholder="Enter your email">
Example :
<input type="text" value="Default Text">
Example :
<table border="1">...</table>
Example :
<table cellpadding="5" cellspacing="5">...</table>
Accessibility attributes help improve web accessibility.
<img>
to provide alternative text for visually impaired users.<img src="logo.png" alt="Company Logo">
<button aria-label="Close Menu">X</button>
<div aria-hidden="true">Hidden Content</div>
Some attributes are no longer in use but may still appear in older code.
<p align="center">Centered Text</p>
<table bgcolor="#f0f0f0">...</table>
"
) for attribute values.aria-*
attributes to enhance accessibility.1. Add Basic Attributes
Define attributes in the opening tag of an element:
Use multiple attributes within the same element:
Apply CSS directly using the style
attribute:
Store custom data for JavaScript:
1. What are HTML attributes?
HTML attributes are additional details about HTML elements, such as their behavior, style, or actions. They always appear in the opening tag.
2. What is the difference between attributes and tags?
Tags define the structure, while attributes define additional properties and behavior of those tags.
3. Can attributes be used on all HTML elements?
Global attributes can be used on any HTML element, but some attributes are specific to certain tags.
4. What is the purpose of the alt
attribute in images?
The alt
attribute provides alternative text for images, improving accessibility and SEO.
5. How can I use custom data attributes?
Custom attributes use the data-*
format and are helpful for storing information to be accessed via JavaScript.