HTML id Attribute
Subject: html
๐ HTML id
Attribute
The id
attribute in HTML is used to assign a unique identifier to a specific element on the webpage. Unlike classes, which can be shared by multiple elements, an id
must be unique across the entire HTML document.
โ What Is an ID?
You can assign an ID using the id
attribute like this:
This unique name can be referenced in CSS, JavaScript, or page anchors.
๐ Why Use IDs?
- ๐ฏ Apply CSS styles to a single, unique element
- ๐งช Target a specific element with JavaScript
- ๐ Create anchor links within a page
- โฟ Enhance accessibility by associating labels with elements
๐ก Example: ID with CSS
๐ Key Points About IDs
- ๐ IDs must be unique โ one per document
- #๏ธโฃ Target with
#
in CSS (e.g.,#main-header
) - ๐ Higher specificity than classes in CSS
- ๐ง Ideal for JavaScript DOM manipulation
- ๐งผ Use clear, descriptive ID names (e.g.,
#footer-logo
)
๐ง Key Takeaway
- The
id
attribute uniquely identifies a single element - Used in CSS (
#idName
), JavaScript (getElementById()
), and anchor links (#idName
) - Always ensure IDs are unique on the page
- Prefer IDs for specific targeting, not general styling
- Combine with semantic HTML for clear and accessible web structure
Advertisement Slot 1
Advertisement Slot 2