Attributes
HTML Attributes (Adding Superpowers to Tags)
Welcome to Topic 1.5! β‘
In the previous lesson, you mastered HTML Elementsβthe basic building blocks of any webpage. Now, imagine you have a plain button or a link on your page. How do you tell the computer:
- Where should this link take the user when clicked?
- Which photo should be displayed inside an image tag?
- What size should the image be?
The answer is: HTML Attributes! Attributes give tags extra information and superpowers.
What is an HTML Attribute?
An Attribute provides additional settings, properties, or behavior to an HTML element.
Let us understand this with a simple real-life example: Your School ID Card! πͺͺ
Just like your ID card gives extra details about who you are, HTML attributes give extra instructions about how an element should behave.
The Golden Rules of HTML Attributes
Every beginner must remember these 4 simple rules:
- 1Always in the Opening Tag: Attributes are ONLY written inside the opening tag (start tag). They are NEVER written in the closing tag!
- 2Name and Value Pair: Attributes usually come in pairs:
name="value". - 3Always Use Quotes: Always wrap the value in double quotation marks (
"..."). - 4Use Spaces, Not Commas: If you use multiple attributes in one tag, separate them with a simple space. Never put commas between them!
The Most Common HTML Attributes You Must Know
Here are the most essential attributes every web developer uses every single day:
1. The href Attribute (For Links)
The <a> tag creates clickable hyperlinks. The href (Hypertext Reference) attribute tells the browser which web address (URL) to open when someone clicks the link:
If you don't provide an href attribute, the link has nowhere to go!
2. The src Attribute (For Images)
The <img> tag displays a picture on your screen. The src (Source) attribute tells the browser where to find the image file:
srccan be a file on your computer (likemyphoto.png) or a link from the internet (likehttps://example.com/logo.png).
3. The alt Attribute (Alternate Description for Images)
The alt attribute provides a text description of the image.
Why is alt so important?
- 1Broken Images: If the user's internet is slow or the image file cannot be found, the browser shows this text instead of an ugly broken icon.
- 2Accessibility: Blind or visually impaired users use "Screen Reader" software that reads the
alttext aloud to describe the picture!
4. The width and height Attributes (Setting Image Dimensions)
You can control the size of images using width and height (measured in pixels):
5. The title Attribute (The Tooltip Helper)
When you add a title attribute to any element, a small floating text box (called a tooltip) appears whenever a user hovers their mouse cursor over that element!
6. The target Attribute (Open in a New Tab)
By default, clicking a link replaces the current webpage. If you want the link to open in a fresh new browser tab, use target="_blank":
7. The lang Attribute (Declaring Page Language)
As you saw in the boilerplate lesson, the lang attribute is used inside the <html> root tag to tell search engines and translation tools the language of the page:
Summary Table of Key Attributes
| Attribute | Used In Tags | What It Specifies | Example |
|---|---|---|---|
href | <a> | Link destination URL | <a href="https://google.com"> |
src | <img>, <video>, <script> | Path or URL to the file | <img src="pic.jpg"> |
alt | <img> | Alternate description text | <img alt="School Logo"> |
width / height | <img>, <video> | Size dimensions in pixels | <img width="300"> |
title | Any tag | Mouse hover tooltip text | <h1 title="Welcome!"> |
target | <a> | Where to open the link | <a target="_blank"> |
lang | <html> | Document language | <html lang="en"> |
What are Boolean Attributes? (True / False Attributes)
Some special HTML attributes do not need a "value". Their mere presence inside a tag turns on that feature! These are called Boolean Attributes.
Common Examples:
- 1
disabled: Turns off an input field or button so users cannot click or type into it.
- 1
required: Makes an input field mandatory in a form (user cannot submit without filling it).
- 1
readonly: Users can read the text but cannot edit or change it.
Common Beginner Mistakes with Attributes
- 1β οΈ Writing Attributes in Closing Tags:
- Wrong:
</p title="Hello"> - Correct:
<p title="Hello">
- 1β οΈ Forgetting Quotation Marks:
- Wrong:
<a href=https://google.com> - Correct:
<a href="https://google.com">
- 1β οΈ Spelling Mistakes in Attribute Names:
- Wrong:
<img scr="photo.jpg">(Many beginners typescrinstead ofsrc!) - Correct:
<img src="photo.jpg">
- 1β οΈ Using Commas Between Attributes:
- Wrong:
<img src="photo.jpg", alt="Photo", width="200"> - Correct:
<img src="photo.jpg" alt="Photo" width="200">
Quick Summary
- Attributes provide extra information, settings, and instructions to HTML tags.
- Attributes are always written inside the opening tag, never in the closing tag.
- Attributes usually follow the format:
name="value". - Always use double quotation marks around attribute values.
- Use
hreffor link destinations andsrcfor image paths. - Use
altto describe images for screen readers and slow connections. - Use
target="_blank"to open links in a new browser tab. - Boolean Attributes (like
disabledandrequired) do not require values.
Practice Quiz
Test your understanding with these multiple-choice questions:
1. Where should an HTML attribute always be placed?
A. Inside the closing tag B. Inside the opening tag C. Between the two tags alongside content D. Outside the <html> tag Answer: B Explanation: Attributes must always be specified inside the start tag (opening tag) of an element.
2. Which attribute specifies the destination URL for a hyperlink (<a> tag)?
A. src B. link C. href D. target Answer: C Explanation: The href (Hypertext Reference) attribute defines the address where the link navigates when clicked.
3. Why is the alt attribute important for image tags?
A. It changes the background color of the image B. It makes the image load faster C. It provides alternate descriptive text if the image fails to load or for screen readers D. It automatically adds animation to the image Answer: C Explanation: The alt attribute provides accessible descriptions for users with visual impairments and displays fallback text if the image link is broken.
4. What type of attribute is disabled or required?
A. Text Attribute B. Boolean Attribute (presence alone means true) C. Numeric Attribute D. CSS Attribute Answer: B Explanation: Boolean attributes do not need a value; their presence on an element automatically sets their state to true.
5. What value of the target attribute opens a link in a fresh, new browser tab?
A. _self B. _parent C. _blank D. _new Answer: C Explanation: Setting target="_blank" instructs the browser to open the linked document in a new tab or window.
Practice Challenge (Try It Yourself)
- 1Open your
index.htmlfile in VS Code. - 2Inside
<body>, try adding:
- A link to your favorite website with
target="_blank"so it opens in a new tab:
- An image tag with
src,alt, andwidth:
- A paragraph with a helpful hover message using the
titleattribute:
- 1Save your file (
Ctrl + S) and view your interactive webpage live with Live Server! π
HTML Headings
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Elements | HTML Headings |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.