🎉 New: Top 75 PHP Interview Questions for 2026 — Free for all learners
Beginner ⏱ min read 🔄 Updated
Home

Advertisement

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.

# Summary

Here is what you've learned in this lesson:

  • What Are HTML Attributes
  • Syntax of HTML Attributes
  • Simple HTML Attribute Example
  • Common HTML Attributes
  • href Attribute Example
  • src Attribute Example
  • style Attribute Example
  • title Attribute Example
  • id and class Attributes
  • Rules for Using HTML Attributes
  • Multiple Attributes Example
  • HTML Attributes Summary Table
  • Common Mistakes with HTML Attributes
  • Frequently Asked Questions (FAQ)
🚀
Next up: Why is Marquee in HTML and CSS Context Named That Way?

Continue to the next lesson and learn more about Why is Marquee in HTML and CSS Context Named That Way?.

Start Next Lesson →

← Previous Post
HTML Elements
Next Post →
HTML Headings