HTML Attributes and Global Attributes
# HTML Attributes and Global Attributes
1. Introduction
HTML tags define the structure of a webpage, but attributes provide extra information about those elements. Think of tags as nouns (like "image" or "link") and attributes as adjectives (like "source URL" or "alternative text").2. Learning Objectives
By the end of this chapter, you will understand:- What HTML attributes are and how to write them.
-
Essential specific attributes like
href,src, andalt.
-
Global attributes like
class,id,hidden, anddata-*that work on almost any element.
3. Detailed Explanations
What are Attributes?
Attributes are always specified in the start tag. They usually come in name/value pairs likename="value".
Essential Specific Attributes
Certain tags *require* specific attributes to function.Links (href):
The <a> tag requires an href (Hypertext REFerence) to know where to navigate.
Images (src and alt):
The <img> tag requires src (source) to find the image, and alt (alternative text) for screen readers and broken links.
Titles (title):
The title attribute adds a tooltip when a user hovers over an element.
Global Attributes
Global attributes can be used on any HTML element.The hidden attribute:
Completely hides an element from the page.
The contenteditable attribute:
Turns any element into a text editor for the user!
The data-* attributes:
Used to store custom invisible data that JavaScript can read later.
4. Mini Project: Customizable Profile Card
Let's combine attributes to make a rich UI component.5. MCQs
Q1: Which attribute is used to provide a tooltip when hovering over an element? A) alt B) tooltip C) title D) hover *Answer: C*6. Summary
Attributes supercharge your HTML tags.src, href, and alt are mandatory for media and links. Global attributes like hidden and contenteditable provide advanced functionality directly from HTML without writing JavaScript.