Learn HTML Attributes – Complete Guide for Beginners
bigsansar | Jan. 28, 2026
HTML (HyperText Markup Language) is the most essential language for creating web pages. While HTML defines the structure of a webpage, attributes are used to add extra information, control behavior, and style elements. Without attributes, HTML elements are static, and their functionality is limited.
What are HTML Attributes?
HTML attributes provide additional information about an element. They allow you to customize the behavior and appearance of elements.
Syntax:
<tagname attribute="value">Content</tagname>Example:
<a href="https://www.example.com">Visit Example</a>
Here, the <a> tag’s An href attribute tells the browser where the link should go when clicked.
Types of HTML Attributes
1. Global Attributes
Global attributes can be used on all HTML elements.
| Attribute | Description | Example |
|---|---|---|
id | Unique identifier for an element | <p id="intro">Hello</p> |
class | Assigns a group or CSS class | <div class="container">Content</div> |
style | Adds inline CSS styles | <p style="color:blue;">Blue text</p> |
title | Displays tooltip text | <span title="User Name">John</span> |
Example:
<p id="intro" class="text" style="color:blue;" title="Welcome paragraph">
Hello! This is an example paragraph.
</p>
2. Element-Specific Attributes
These attributes are only used with specific HTML elements.2. Element-Specific Attributes.
These attributes are only used with specific HTML elements.
| Element | Attribute | Description | Example |
|---|---|---|---|
<a> | href | Link destination | <a href="https://example.com">Visit</a> |
<a> | target | Where the link opens (_blank = new tab) | <a href="https://example.com" target="_blank">Visit</a> |
<img> | src | Image file path | <img src="image.jpg" alt="Sample Image"> |
<img> | alt | Alternate text if image is missing | <img src="image.jpg" alt="Beautiful sunset"> |
<input> | type | Type of input (text, password, checkbox) | <input type="text" placeholder="Enter name"> |
<input> | placeholder | Placeholder text inside input | <input type="text" placeholder="Username"> |
Example:
<img src="https://via.placeholder.com/150" alt="Sample Image" width="150" height="150">
3. Boolean Attributes
Boolean attributes do not need a value; their presence alone makes them active.
| Attribute | Description | Example |
|---|---|---|
checked | Checkbox selected | <input type="checkbox" checked> I agree |
disabled | Disables an element | <input type="text" disabled> |
readonly | Makes a text field read-only | <input type="text" readonly value="Cannot edit"> |
Important Tips for Using HTML Attributes
- Always use quotes: Attribute values should be wrapped in double quotes
" "or single' 'quotes. - Case-insensitive: HTML attributes are generally written in lowercase.
- Use valid attributes: Only use attributes defined by HTML specifications.
Example of HTML Attributes in Action
<!DOCTYPE html>
<html>
<head>
<title>HTML Attributes Example</title>
</head>
<body>
<h1 title="Welcome Heading">Welcome to My Website</h1>
<p id="intro" class="text" style="color:green;">
This paragraph demonstrates global attributes.
</p>
<a href="https://www.example.com" target="_blank">Visit Example</a>
<img src="https://via.placeholder.com/150" alt="Placeholder Image">
<form>
<input type="text" placeholder="Enter your name" required>
<input type="checkbox" checked> I agree
<input type="submit" value="Submit">
</form>
</body>
</html>
HTML attributes are essential for web development. They allow you to:
- Control the behavior of elements
- Define style and appearance
- Add extra information
Mastering attributes is crucial for creating interactive, professional, and visually appealing web pages.
0 COMMENTS:
HTML Text Formatting, Quotation & Citation Elements Explained | Complete Guide
Learn HTML Text Formatting, Quotation, and Citation Elements with examples. Understand tags like <s…
HTML Paragraphs & Styling: Complete Guide for Beginners
Learn HTML paragraphs, text formatting, and CSS styling. Step-by-step guide with tips to create rea…
Learn HTML: Complete Guide to HTML Paragraphs for Beginners
Master HTML paragraphs with this beginner-friendly guide. Learn how to use <p> tags, line breaks, s…
HTML Headings Tutorial: Learn H1 to H6 for SEO & Web Structure
Master HTML Headings from H1 to H6 with examples, best practices, and CSS tips. Learn how to struct…
Learn HTML Attributes – Complete Guide for Beginners
Discover everything about HTML attributes! Learn how to use global, element-specific, and boolean a…
Learn HTML Elements: Complete Guide for Beginners | Learn HTML
Learn HTML elements step-by-step with easy examples. This beginner’s guide explains HTML tags, head…
HTML Editors: Best Tools for Beginners (HTML Tutorial)
Learn the best HTML editors for beginners. Discover VS Code, Sublime Text, Brackets, and IDEs with …
Learn HTML: Complete HTML Tutorial for Beginners
Learn HTML step-by-step with this complete HTML tutorial for beginners. Understand HTML structure, …