📌 HTML Attributes Explained for Beginners | Learn with Examples
👋 Introduction
Welcome to Step 4 of our HTML Learning Series!
In the previous steps, we built a basic webpage using essential HTML tags.
🔹 Step 1: Learn HTML for Beginners
🔹 Step 2: Learn About HTML Tags in Detail
🔹 Step 3: Creating a Simple Webpage
Now, let’s move forward and understand a key concept in HTML — Attributes.
🧩 What Are HTML Attributes?
HTML attributes are extra pieces of information added to HTML tags.
They define the properties or behavior of elements.
🔸 Attributes always appear inside the opening tag
🔸 They follow the format:attribute="value"
🧠 General Syntax of an Attribute:
<tagname attribute="value">Content</tagname>
✅ Example:
<a href="https://example.com">Visit Site</a>
In the above example:
-
Tag:
<a>
-
Attribute:
href
-
Value:
https://example.com
🔍 Common HTML Attributes (With Examples)
Here are the most frequently used attributes and how they work:
1️⃣ href (for links)
Used with <a>
tag to define the destination URL.
<a href="https://google.com">Go to Google</a>
2️⃣ src (for images and media)
Specifies the source path of an image or media file.
<img src="image.jpg" alt="My Photo">
3️⃣ alt (alternative text for images)
Displayed if the image can’t be loaded and also useful for screen readers.
<img src="dog.jpg" alt="A cute dog">
4️⃣ title (tooltip text)
Displays a tooltip when the user hovers over the element.
<p title="I am a tooltip">Hover over me!</p>
5️⃣ style (inline CSS)
Allows you to apply styles directly to an element.
<p style="color:blue; font-size:18px;">Styled Paragraph</p>
6️⃣ target (for links)
Specifies how the link will open.
<a href="https://example.com" target="_blank">Open in new tab</a>
7️⃣ width & height (for images and videos)
Define the size of the media elements.
<img src="cat.jpg" width="200" height="150">
8️⃣ id and class (for styling and scripting)
These help identify elements for CSS or JavaScript.
<p id="intro">This has an ID.</p>
<p class="highlight">This has a class.</p>
✅ Use:
-
id
: unique per page -
class
: can be shared by multiple elements
9️⃣ type (used in forms and inputs)
Specifies the kind of input or button.
<input type="text" name="username">
<input type="submit" value="Submit">
✅ Summary Table of HTML Attributes
Attribute | Purpose | Used With |
---|---|---|
href |
Link destination | <a> |
src |
Media source | <img> , <video> |
alt |
Image description | <img> |
title |
Tooltip text | Most elements |
style |
Inline CSS | Most elements |
target |
Open link in new tab | <a> |
width / height |
Size definition | <img> , <video> |
id |
Unique identifier | All elements |
class |
CSS group | All elements |
type |
Input type | <input> |
⚠️ Best Practices for Using Attributes
✅ Use alt
text for all images
✅ Avoid inline styles; prefer external CSS for large projects
✅ Use meaningful id
and class
names
✅ Always use quotes around attribute values
✅ Do not duplicate id
on the same page
🎯 Practice Challenge
Try writing this code and open it in your browser:
<!DOCTYPE html>
<html>
<head>
<title>HTML Attributes Demo</title>
</head>
<body>
<h1 style="color:green;" title="Main Heading">Welcome!</h1>
<p class="info">This paragraph has a class.</p>
<img src="https://via.placeholder.com/200" alt="Placeholder Image" width="200">
<br>
<a href="https://openai.com" target="_blank">Visit OpenAI</a>
</body>
</html>
🔄 Blog Series Navigation:
<div style="background:#f9f9f9; padding:15px; border:1px solid #ddd; border-radius:6px;">
<strong>📘 HTML Learning Series:</strong>
<ul>
<li>✅ Step 1: <a href="https://your-website.com/learn-html-for-beginners">Learn HTML for Beginners</a></li>
<li>✅ Step 2: <a href="https://your-website.com/html-tags-explained-with-examples">HTML Tags Explained</a></li>
<li>✅ Step 3: <a href="https://your-website.com/create-simple-html-webpage-beginners">Create a Simple Webpage</a></li>
<li>🔵 Step 4: Introduction to HTML Attributes (You are here)</li>
<li>🔜 Step 5: How to Use HTML Forms and Input Elements</li>
</ul>
</div>
🎯 What’s Next?
In the next blog post, we’ll cover:
✅ Step 5: How to Use HTML Forms and Input Elements —
with real form examples and input types?