HTML Attributes

HTML Attributes provide additional information about an element. They are always specified in the opening tag and usually come in name/value pairs like name="value".

Essential HTML Attributes

Attributes can be categorized into Global Attributes (which can be used on almost any tag) and Element-Specific Attributes.

1. Global Attributes

These are the most common attributes you will use to identify or style your elements.

				
					<p id="abc">Lorem Ipsum is a dummy text.</p>

<p class="pqr">Lorem Ipsum is a dummy text.</p>

<p title="xyz">Lorem Ipsum is a dummy text.</p>

<p style="color:red">Lorem Ipsum is a dummy text.</p>
				
			

2. Element-Specific Attributes

Some tags require specific attributes to function at all.
				
					<a href="https://example.com">Visit Site</a>

<img decoding="async" src="photo.jpg">

<img decoding="async" src="photo.jpg" alt="A beautiful sunset">

<img loading="lazy" decoding="async" src="photo.jpg" alt="A beautiful sunset" width="500" height="400">
				
			

The Syntax Rules

To avoid errors, follow these three golden rules for attributes:

Attribute Comparison Table

AttributeCategoryDescription
idGlobalUnique identifier for targeting with JS/CSS.
classGlobalGroup identifier for shared styling.
hrefLinkSpecifies the link destination.
srcMediaSpecifies the source file (image/video/script).
altMediaText description for screen readers/broken images.
hiddenGlobalHides the element from view.
data-*CustomAllows you to store your own private data (e.g., data-user-id="123").

Quick Summary Table

AttributeUsed InPurpose
href<a>The destination URL of a link.
src<img>, <script>The source path of a file.
alt<img>Descriptive text for accessibility.
target<a>_blank opens a link in a new tab.
placeholder<input>Temporary hint text inside a form field.
value<input>The actual data stored in a form field.