What Are HTML Attributes? How to Define Attributes Inside HTML Tags
HTML attributes provide extra information about HTML elements, modifying their behavior or appearance. They are essential for controlling links, images, forms, styling, and more.
In this guide, you’ll learn:
✔ What HTML attributes are and how they work
✔ Common HTML attributes every developer should know
✔ How to define attributes inside HTML tags
✔ Best practices for using attributes effectively
1. What Are HTML Attributes?
HTML attributes are special keywords used inside an HTML tag to configure or adjust an element’s behavior. They appear in the opening tag and usually follow this format:
Content
Key Features of Attributes:
Provide additional functionality (e.g.,
href
in<a>
defines the link destination).Modify default behavior (e.g.,
disabled
in<button>
prevents clicking).Can be global (work on any tag) or element-specific (e.g.,
src
only works with<img>
).
2. How to Define Attributes Inside HTML Tags
Attributes are written inside the opening tag of an element in the format:
Content
Example:
href
→ Specifies the URL.target="_blank"
→ Opens link in a new tab.
Rules for Writing Attributes:
✅ Use lowercase (standard practice, though HTML is case-insensitive).
✅ Enclose values in quotes (" "
or ' '
).
✅ Separate multiple attributes with spaces.
✅ Boolean attributes (like disabled
, required
) don’t need a value.
3. Common HTML Attributes
A. Global Attributes (Work on Most Tags)
Attribute | Purpose | Example |
---|---|---|
class | Assigns a CSS class for styling | <p class="intro">Text</p> |
id | Unique identifier for an element | <div id="header"> |
style | Inline CSS styling | <p style="color:red;">Red Text</p> |
title | Tooltip text on hover | <a title="Click me">Link</a> |
data-* | Stores custom data | <div data-user="admin"> |
B. Element-Specific Attributes
Element | Attribute | Purpose |
---|---|---|
<a> | href | Link destination |
<img> | src , alt | Image source & description |
<input> | type , placeholder | Input field type & hint text |
<form> | action , method | Form submission settings |
<table> | border , cellpadding | Table styling |
4. Boolean Attributes (True/False Attributes)
Some attributes don’t need a value—they are active just by being present:
Attribute | Effect |
---|---|
disabled | Disables a button/input |
readonly | Makes input uneditable |
required | Forces input to be filled |
autoplay | Starts media automatically |
Example:
5. Best Practices for Using Attributes
✔ Always use quotes (href="link"
, not href=link
).
✔ Prefer semantic attributes (e.g., alt
for images, aria-*
for accessibility).
✔ Avoid inline styles (use CSS classes instead).
✔ Validate HTML (ensure no broken attributes).
6. Common Mistakes with Attributes
❌ Missing quotes → <a href=https://example.com>
(should be href="https://example.com"
).
❌ Incorrect attribute names → <img src="pic.jpg" alt="Photo" border=0>
(avoid deprecated attributes).
❌ Overusing inline styles → <p style="font-size:16px;color:blue;">
(use CSS instead).
7. Practical Examples
Image with Attributes

src
→ Image file path.alt
→ Description (for accessibility & SEO).width
/height
→ Sets dimensions.
Form Input with Attributes
type="email"
→ Validates email format.placeholder
→ Hint text.required
→ Makes field mandatory.
Final Thoughts
HTML attributes are powerful tools that enhance functionality, accessibility, and styling. By mastering them, you can:
✅ Create interactive links, forms, and media.
✅ Improve SEO and accessibility.
✅ Control element behavior dynamically.
Next Steps:
Experiment with different attributes in a sample HTML file.
Learn about ARIA attributes for accessibility.
Explore how JavaScript manipulates attributes.