Metadata
- Date :: 13-04-2025
- Tags :: web-dev
Notes
π HTML Attributes & The Anchor Element (<a> Tag) β Full Notes
π Overview
In this lesson, we dive deeper into HTML elements by learning about:
-
Attributes in HTML
-
The Anchor (
<a>) Element used for creating hyperlinks -
How attributes modify behavior
-
Using global and specific attributes
-
Practical coding challenge using anchor tags in an ordered list
-
Extra: Using attributes like
startin the<ol>tag
π§± 1. What are HTML Attributes?
HTML attributes are used to provide additional information about an element. They modify the behavior, appearance, or function of elements.
π§© Attributes:
-
Are always written in the opening tag of an element.
-
Follow the syntax:
attributeName="attributeValue"
β Example:
<a href="https://example.com">Click here</a>-
hrefis the attribute name. -
"https://example.com"is the attribute value. -
This tells the browser where the link should go when clicked.
π 2. The Anchor Element (<a>)
The anchor element is used to create hyperlinks in HTML.
𧬠Basic Structure:
<a href="URL">Link Text</a>-
<a>= start of anchor tag -
href= attribute that tells where the link goes -
Link Text= what users see and can click -
</a>= closing tag
π¨ Result on the Webpage:
-
Blue, underlined text by default.
-
Becomes clickable, takes you to the linked URL when clicked.
π« Without the href Attribute
If you write:
<a>Click me</a>-
This will display the text βClick meβ
-
BUT itβs not clickable β itβs just plain text.
-
The anchor element alone does not function without the
hrefattribute.
π§ 3. Syntax Breakdown of Attributes
<a href="https://example.com" target="_blank" draggable="true">Visit Site</a>Letβs break it down:
| Part | Meaning |
|---|---|
href="..." | The destination URL |
target="_blank" | Opens the link in a new tab |
draggable="true" | Allows the user to drag this element (a global attribute) |
β Multiple attributes are separated by spaces and all live inside the opening tag.
π§° 4. Types of Attributes
πΉ Specific Attributes
-
These are only valid for certain HTML elements.
-
Example:
-
hrefβ€ valid for<a> -
srcβ€ valid for<img>
-
πΈ Global Attributes
-
These are universal, meaning they can be applied to any HTML element.
-
Examples:
-
id -
class -
title -
style -
draggable(e.g.,draggable="true")
-
π 5. Practical Coding Challenge
π― Task:
Create a webpage that shows your Top 5 Favorite Websites, using:
-
<ol>for ordered list -
<li>for list items -
<a href="...">inside each<li>to create clickable links
π» Example Code:
<h1>My Top 5 Favorite Websites</h1>
<ol>
<li><a href="https://producthunt.com">Product Hunt</a></li>
<li><a href="https://dribbble.com">Dribbble</a></li>
<li><a href="https://github.com">GitHub</a></li>
<li><a href="https://stackoverflow.com">Stack Overflow</a></li>
<li><a href="https://unsplash.com">Unsplash</a></li>
</ol>π Result:
A numbered list with each item being a clickable link to your favorite website.
π‘ Notes on href
-
The
hrefvalue can be:-
A full URL (
https://example.com) -
A relative path (
/about.html) -
A page fragment (
#section1) -
An email link (
mailto:someone@example.com) -
A phone link (
tel:+1234567890)
-
π§ͺ Bonus Challenge: Use the start Attribute
The <ol> tag supports a special attribute called start, which tells the browser what number to begin the list from.
π§Ύ Syntax:
<ol start="5">
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ol>π Result:
-
Starts from 5, not 1:
-
- Link 1
-
- Link 2
-
- Link 3
-
π How to Use:
-
Add it to the opening
<ol>tag -
Like this:
<ol start="5">π§ Learning Tips
-
Attributes are powerful: They add flexibility and interactivity.
-
Practice nesting: Like placing
<a>inside<li>, and<li>inside<ol>. -
Use MDN Web Docs (developer.mozilla.org) to explore more about tags and their attributes.
-
VS Code helps with auto-completion and suggestions for tags and attributes.
π Summary Table
| Concept | Explanation |
|---|---|
Anchor Tag (<a>) | Used to create hyperlinks |
href | Attribute that sets the link destination |
| Attribute Syntax | name="value" inside the opening tag |
| Global Attributes | Work on all tags (e.g., draggable, id) |
| Specific Attributes | Only work with certain tags (e.g., href for <a>) |
start Attribute | Sets starting number for <ol> |
β Practice Exercise
-
Create an
<h1>for the heading -
Use
<ol>for ordered list -
Add five
<li>elements -
Inside each
<li>, include an<a href="...">Website Name</a> -
Test by clicking on the links
-
Add
start="5"to<ol>as an extra challenge
π§ Review Questions
-
What does
hrefdo in an anchor tag? -
Can you put an
<a>tag inside a list item? -
Whatβs the difference between global and specific attributes?
-
How do you start an ordered list from number 5?
-
What happens if you omit
hrefin an anchor tag?