HTML Attributes

HTML Attributes provide additional information about HTML elements.
They help control how elements behave, display, or store extra data.

In this tutorial, you will learn what HTML attributes are, how to use them, and see examples with output.

What Are HTML Attributes

HTML attributes are special properties added to HTML tags to provide extra information.

Attributes are always written inside the opening tag.


Syntax of HTML Attributes

Example

<tagname attribute="value">Content</tagname>

Explanation

PartMeaning
attributeName of the attribute
"value"Value assigned to attribute

HTML attributes, HTML attribute examples, HTML syntax, learn HTML attributes, HTML basics


Simple HTML Attribute Example

Example Code

<a href="https://example.com">Visit Website</a>

Explanation

  • href is an attribute
  • It defines the link destination

Output

Visit Website (clickable link)


Common HTML Attributes

List of Common Attributes

AttributeDescription
hrefDefines link URL
srcSpecifies image source
altAlternative text for images
styleAdds inline CSS
titleTooltip text
idUnique identifier
classGroup elements

href Attribute Example

Example Code

<a href="https://google.com">Go to Google</a>

Output

Go to Google


src Attribute Example

Example Code

<img src="image.jpg" alt="Sample Image">

Explanation

  • src defines image location
  • alt shows text if image is not found

Output

[Image displayed here]


style Attribute Example

Example Code

<p style="color: blue;">This is blue text.</p>

Output

This is blue text. (in blue color)


title Attribute Example

Example Code

<p title="This is a tooltip">Hover over this text</p>

Output

Hover over this text
(Shows tooltip when mouse hovers)


id and class Attributes

Example Code

<p id="para1" class="text">This is a paragraph.</p>

Explanation

AttributePurpose
idUnique element identifier
classUsed for grouping elements

Rules for Using HTML Attributes

Important Rules

RuleDescription
Use QuotesAlways wrap values in quotes
LowercaseUse lowercase for attributes
Inside Opening TagAttributes go only in opening tag
Unique IDid must be unique

Multiple Attributes Example

Example Code

<a href="https://example.com" title="Go to site" style="color:red;">
Click Here
</a>

Output

Click Here (red color with tooltip)


HTML Attributes Summary Table

FeatureDescription
LocationInside opening tag
PurposeAdd extra information
Formatname=”value”
Multiple AllowedYes

Common Mistakes with HTML Attributes

MistakeProblem
Missing quotesInvalid HTML
Wrong attribute nameNo effect
Using uppercaseNot recommended
Duplicate IDsErrors in CSS/JS

Frequently Asked Questions (FAQ)

1. What is an HTML attribute?

An HTML attribute provides extra information about an element.

2. Where are attributes written?

Inside the opening tag.

3. Can an element have multiple attributes?

Yes, an element can have multiple attributes.

4. Are quotes required in attributes?

Yes, it is best practice to use quotes.

5. What is the most common attribute?

href and src are commonly used attributes.

Related Article
Introduction to HTML

HTML Introduction – Learn the Basics of HTML HTML is the starting point of web development. Every website you see Read more

What is HTML

What Is HTML – Complete Beginner Explanation HTML is the foundation of the web. Every website you visit, from simple Read more

HTML Basic Examples

HTML Basic Examples – Learn HTML by Practice The best way to learn HTML is by writing and testing simple Read more

HTML text Editors

HTML Text Editors – Learn How to Write HTML Code To write HTML code, you need a text editor. A Read more

HTML Tags

HTML Tags are the basic units used to create HTML elements.They define how content is displayed in a web browser. Read more

HTML Elements

HTML Elements are the building blocks of every web page.Each element defines a part of the webpage, such as headings, Read more

HTML Headings

HTML Headings are used to define titles and subtitles on a web page.They help organize content and improve readability for Read more

HTML Paragraphs

HTML Paragraphs are used to display blocks of text on a web page.They help organize content into readable sections. In Read more