W3Newbie
  • HTML Tutorial
  • CSS Tutorial
  • Web Dev
    • VS Code Tutorial
    • Command Line Tutorial
    • Git Tutorial
    • Github Tutorial
    • Sass Tutorial
  • Courses
  • Resources
  • Tutorials
  • Click to open the search input field Click to open the search input field Search
  • Menu Menu

HTML IDs

HTML IDs (identifiers) are attributes used to uniquely identify specific elements within an HTML document and are only used once in a web page per element. They act like unique names or labels for individual elements, enabling precise targeting for styling, scripting, and linking.

Key characteristics of HTML IDs:

  • Uniqueness: Each ID must be unique within a single HTML document. No two elements can have the same ID.
  • Assignment: IDs are assigned using the id attribute within an element’s opening tag.
  • Targeting: They are targeted using the hash symbol (#) followed by the ID name in CSS and JavaScript.
  • Specificity: IDs have the highest specificity in CSS, meaning styles applied to an ID will override any other styles targeting the same element.

HTML ID’s Example:

HTML:

<h2 id="main-heading">This is the main heading</h2>
<p id="important-notice">Please read this notice carefully.</p>

CSS:

#main-heading {
  color: blue;
  font-size: 2em;
}

#important-notice {
  background-color: yellow;
}

Common uses of IDs:

  • Styling: Targeting specific elements with CSS for unique styling or visual effects.
  • JavaScript: Selecting and manipulating specific elements with JavaScript to create dynamic behavior.
  • Linking: Creating internal links within a page that jump to a specific section by referencing its ID.
  • Accessibility: Linking to specific sections of a page for assistive technologies like screen readers, enhancing navigation for users with disabilities.

Best practices for using IDs:

  • Use descriptive and meaningful ID names that reflect the element’s purpose or content.
  • Ensure each ID is unique within the document to avoid conflicts.
  • Use IDs for elements that require unique identification and targeting.
  • Use classes for elements that share common styling or grouping needs, as classes can be applied to multiple elements.

Remember: HTML IDs are unique and an element with an ID should only be referenced once on a web page.

< Previous
Next Lesson >
  • HTML Tutorial
  • HTML Introduction
  • HTML Text Editor
  • HTML Document
  • HTML Comments
  • HTML Elements
  • HTML Paragraphs
  • HTML Headings
  • HTML Text Formatting
  • HTML Links
  • HTML Images
  • HTML Unordered Lists
  • HTML Ordered Lists
  • HTML Tables
  • HTML Block Elements
  • HTML Inline Elements
  • HTML Attributes
  • HTML Classes
  • HTML IDs
  • HTML Audio
  • HTML Video
  • HTML Divs
  • HTML Semantics
  • HTML Accessibility
  • HTML Forms

The Newbie Template Bundle

The 12 Website Bundle Pack

Subscribe on YouTube

Web Development Courses

The HTML Email Mastery Course - Build Responsive HTML Email Templates.

Latest Tutorial Posts

  • The HTML Email Mastery Course - Build Responsive HTML Email Templates.w3newbie.com
    HTML Email Mastery Course CouponOctober 12, 2020 - 8:10 pm
  • Create A 5 Page Website With PHP Includes, HTML5, CSS3 & Bootstrap 4w3newbie.com
    Create A 5 Page Website With PHP Includes, HTML5, CSS3 & Bootstrap 4April 22, 2019 - 1:48 pm
© w3newbie.com, 2026. Terms of Use. Privacy Policy.
Scroll to top Scroll to top Scroll to top