html
/

HTML Attributes: Adding Extra Information to Elements

Last Sync: Today

On this page

9
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

html

HTML Attributes: Adding Extra Information to Elements

What are HTML Attributes?

HTML attributes provide additional information about elements. They are always defined in the opening tag and usually come in name-value pairs.

HTMLRead-only
1
<a href="https://example.com">Visit Site</a>

Basic Syntax

HTMLRead-only
1
<tagname attribute="value">Content</tagname>

Common HTML Attributes

AttributeDescriptionExample
hrefLink destination<a href="url">
srcImage source<img src="img.png">
altAlternative text<img alt="image">
idUnique identifier<div id="header">
classGroup identifier<div class="box">
styleInline styling<p style="color:red">

Global Attributes

Global attributes can be used on any HTML element.

AttributePurpose
idUnique element identifier
classCSS/JS grouping
styleInline CSS
titleTooltip text
hiddenHide element

Boolean Attributes

Boolean attributes do not require a value. Their presence alone applies the effect.

HTMLRead-only
1
<input type="text" required>
<input type="checkbox" checked>

Custom Data Attributes

Custom attributes starting with data- allow storing extra data.

HTMLRead-only
1
<div data-user-id="123">User Info</div>

Multiple Attributes Example

HTMLRead-only
1
<img src="image.jpg" alt="Sample" width="200" height="100">

Best Practices

  • Always use quotes for attribute values
  • Use meaningful class and id names
  • Avoid inline styles when possible
  • Use alt attribute for accessibility
  • Keep attributes clean and minimal

Conclusion

HTML attributes enhance elements by adding extra functionality and information. Understanding attributes is essential for styling, scripting, and building dynamic web applications.

Try it yourself

<a href="https://google.com" target="_blank">Open Google</a>
<img src="https://via.placeholder.com/150" alt="image">

Test Your Knowledge

Q1
of 3

Which attribute is used for links?

A
src
B
href
C
link
D
url
Q2
of 3

Which attribute is unique?

A
class
B
id
C
style
D
title
Q3
of 3

Which attribute provides alternate text for images?

A
src
B
title
C
alt
D
desc

Frequently Asked Questions

What is an HTML attribute?

An attribute provides extra information about an HTML element.

What is the difference between id and class?

id is unique, while class can be used for multiple elements.

Are attributes required?

No, but they enhance functionality and usability.

Previous

html elements

Next

html headings

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.