HTML - The id Attribute

HTML - The id Attribute | Complete Guide with Examples

HTML - The id Attribute 

The HTML id attribute is one of the most important and powerful attributes in modern front-end development. It plays a crucial role in identifying unique elements, enabling JavaScript manipulation, applying CSS styling, linking within the same webpage, creating navigation, improving accessibility, and enhancing SEO-based user interaction. Because the id attribute is used widely in HTML, CSS, and JavaScript, mastering it is essential for beginners and experienced developers alike. This document provides a comprehensive explanation of the id attribute, complete working examples, and corresponding outputs, all formatted according to your requirements.

What Is the HTML id Attribute?

The id attribute in HTML is used to assign a unique identifier to an HTML element. This identifier must be unique within the entire HTML document so that the browser, CSS, or JavaScript can reference it without confusion. The id plays a major role in:

  • Applying CSS styling to a specific element
  • Accessing and manipulating elements using JavaScript
  • Creating bookmark links (fragment identifiers)
  • Improving webpage navigation and user interaction
  • Enhancing accessibility

Basic Syntax of the id Attribute


Content

Output: The browser renders a normal element, but it internally knows that the element has a unique identity.

Rules for Using the id Attribute

Although the id attribute is simple to use, several rules ensure correct implementation and avoid conflicts in web development:

  • The id value must be unique within the webpage
  • id names cannot start with a number (recommended best practice)
  • No spaces are allowed in id values
  • It is case-sensitive in JavaScript (though HTML treats it as case-insensitive)
  • id values must begin with a letter or underscore
  • Only one id per element is allowed

Example 1: Simple Use of the id Attribute

This example demonstrates a basic id used on a heading.






Welcome to HTML id Attribute Tutorial

This is a normal paragraph below the heading.

Output:

A heading appears reading β€œWelcome to HTML id Attribute Tutorial” followed by a normal paragraph. The id itself is invisible to the user.

Using the id Attribute with CSS

One of the most common uses of the id attribute is to apply CSS styles to a specific HTML element. Because id is unique, it allows precise control over styling without affecting other elements.

Example: Styling Using CSS and id Selector









This text is styled using CSS and an id selector.

Output:

A paragraph appears in blue color, bold weight, and larger font size (24px). The id β€œhighlight” applies styling only to that specific paragraph.

Using the id Attribute in Internal Page Navigation (Jump Links)

HTML id attributes allow users to jump directly to a section by clicking a link. This is extremely helpful in:

  • Table of contents pages
  • Long tutorials
  • Documentation
  • Blog navigation

Example: Creating Jump Links






Go to Section 3

Section 1

Content of section 1.

Section 2

Content of section 2.

Section 3

This is section 3. You jumped here using the id attribute!

Output:

A link appears reading β€œGo to Section 3”. When clicked, the page scrolls directly to Section 3 where the heading β€œSection 3” is located.

Using the id Attribute with JavaScript

The id attribute is extremely important in JavaScript because it allows the script to select and manipulate specific elements efficiently. JavaScript uses functions like:

  • document.getElementById()
  • document.querySelector()
  • element.style
  • element.innerHTML

Example: JavaScript Accessing an Element by id






Original Text

Output:

The page shows a paragraph and a button. When the button is clicked, JavaScript changes the paragraph text to β€œThe text has been changed using JavaScript!”

Using id for CSS vs JavaScript

Although both CSS and JavaScript use id attributes, their purpose differs:

  • CSS: Styling specific elements
  • JavaScript: Dynamic updates, manipulation, event handling

Example: Using id for Both CSS and JavaScript









Output:

A light gray box appears. When the button is clicked, the box changes its background color to yellow.

Difference Between id and class Attribute

A common question is about the difference between id and class in HTML. Both are used for identification but serve different purposes.

  • id: Should be unique; used for JavaScript targeting and unique styling
  • class: Can be repeated; used for grouping elements for styling

Example Demonstrating id vs class









This is a unique heading

First item with class

Second item with class

Output:

The heading appears in red color and larger font size. Both paragraphs appear in green color because they share the same class.

Using id Attribute in Forms

Form elements frequently use id attributes alongside label elements to improve accessibility and allow screen readers to link labels with inputs.

Example: id with Form Label











Output:

A text field appears for email input. Clicking the label automatically activates the input field.

Using id Attribute for Fragment URLs

You can link directly to a specific part of a webpage using a fragment identifier such as:

example.com/page.html#targetSection

Example: URL Fragment Linking






Detailed Section

This part can be opened using a URL containing #details

Output:

When you navigate to the URL ending with #details, the page scrolls directly to this section.


The HTML id attribute is an essential tool in front-end development. It provides a unique identity to elements, making it possible for CSS and JavaScript to interact precisely with specific parts of the webpage. From styling and navigation to JavaScript manipulation and form labeling, the id attribute is deeply integrated into the structure and behavior of modern webpages. Mastering its usage is a key step in becoming a proficient HTML developer.

logo

HTML

Beginner 5 Hours
HTML - The id Attribute | Complete Guide with Examples

HTML - The id Attribute 

The HTML id attribute is one of the most important and powerful attributes in modern front-end development. It plays a crucial role in identifying unique elements, enabling JavaScript manipulation, applying CSS styling, linking within the same webpage, creating navigation, improving accessibility, and enhancing SEO-based user interaction. Because the id attribute is used widely in HTML, CSS, and JavaScript, mastering it is essential for beginners and experienced developers alike. This document provides a comprehensive explanation of the id attribute, complete working examples, and corresponding outputs, all formatted according to your requirements.

What Is the HTML id Attribute?

The id attribute in HTML is used to assign a unique identifier to an HTML element. This identifier must be unique within the entire HTML document so that the browser, CSS, or JavaScript can reference it without confusion. The id plays a major role in:

  • Applying CSS styling to a specific element
  • Accessing and manipulating elements using JavaScript
  • Creating bookmark links (fragment identifiers)
  • Improving webpage navigation and user interaction
  • Enhancing accessibility

Basic Syntax of the id Attribute

Content

Output: The browser renders a normal element, but it internally knows that the element has a unique identity.

Rules for Using the id Attribute

Although the id attribute is simple to use, several rules ensure correct implementation and avoid conflicts in web development:

  • The id value must be unique within the webpage
  • id names cannot start with a number (recommended best practice)
  • No spaces are allowed in id values
  • It is case-sensitive in JavaScript (though HTML treats it as case-insensitive)
  • id values must begin with a letter or underscore
  • Only one id per element is allowed

Example 1: Simple Use of the id Attribute

This example demonstrates a basic id used on a heading.

Welcome to HTML id Attribute Tutorial

This is a normal paragraph below the heading.

Output:

A heading appears reading “Welcome to HTML id Attribute Tutorial” followed by a normal paragraph. The id itself is invisible to the user.

Using the id Attribute with CSS

One of the most common uses of the id attribute is to apply CSS styles to a specific HTML element. Because id is unique, it allows precise control over styling without affecting other elements.

Example: Styling Using CSS and id Selector

This text is styled using CSS and an id selector.

Output:

A paragraph appears in blue color, bold weight, and larger font size (24px). The id “highlight” applies styling only to that specific paragraph.

Using the id Attribute in Internal Page Navigation (Jump Links)

HTML id attributes allow users to jump directly to a section by clicking a link. This is extremely helpful in:

  • Table of contents pages
  • Long tutorials
  • Documentation
  • Blog navigation

Example: Creating Jump Links

Go to Section 3

Section 1

Content of section 1.

Section 2

Content of section 2.

Section 3

This is section 3. You jumped here using the id attribute!

Output:

A link appears reading “Go to Section 3”. When clicked, the page scrolls directly to Section 3 where the heading “Section 3” is located.

Using the id Attribute with JavaScript

The id attribute is extremely important in JavaScript because it allows the script to select and manipulate specific elements efficiently. JavaScript uses functions like:

  • document.getElementById()
  • document.querySelector()
  • element.style
  • element.innerHTML

Example: JavaScript Accessing an Element by id

Original Text

Output:

The page shows a paragraph and a button. When the button is clicked, JavaScript changes the paragraph text to “The text has been changed using JavaScript!”

Using id for CSS vs JavaScript

Although both CSS and JavaScript use id attributes, their purpose differs:

  • CSS: Styling specific elements
  • JavaScript: Dynamic updates, manipulation, event handling

Example: Using id for Both CSS and JavaScript

Output:

A light gray box appears. When the button is clicked, the box changes its background color to yellow.

Difference Between id and class Attribute

A common question is about the difference between id and class in HTML. Both are used for identification but serve different purposes.

  • id: Should be unique; used for JavaScript targeting and unique styling
  • class: Can be repeated; used for grouping elements for styling

Example Demonstrating id vs class

This is a unique heading

First item with class

Second item with class

Output:

The heading appears in red color and larger font size. Both paragraphs appear in green color because they share the same class.

Using id Attribute in Forms

Form elements frequently use id attributes alongside label elements to improve accessibility and allow screen readers to link labels with inputs.

Example: id with Form Label

Output:

A text field appears for email input. Clicking the label automatically activates the input field.

Using id Attribute for Fragment URLs

You can link directly to a specific part of a webpage using a fragment identifier such as:

example.com/page.html#targetSection

Example: URL Fragment Linking

Detailed Section

This part can be opened using a URL containing #details

Output:

When you navigate to the URL ending with #details, the page scrolls directly to this section.


The HTML id attribute is an essential tool in front-end development. It provides a unique identity to elements, making it possible for CSS and JavaScript to interact precisely with specific parts of the webpage. From styling and navigation to JavaScript manipulation and form labeling, the id attribute is deeply integrated into the structure and behavior of modern webpages. Mastering its usage is a key step in becoming a proficient HTML developer.

Frequently Asked Questions for HTML

  • HTML stands for HyperText Markup Language.
  • It is used to create the structure of web pages and web applications.
  • HTML defines elements such as headings, paragraphs, links, images, and other content.

  • Block-level elements (like <div>, <p>, <h1>) start on a new line and take full width.
  • Inline elements (like <span>, <a>, <strong>) stay within the flow of the text.
  • Understanding this helps with layout and styling.

  • A basic HTML page includes a <!DOCTYPE html> declaration, followed by <html>, <head>, and <body>.
  • The <head> section contains metadata like the title and links to stylesheets.
  • The <body> section contains all the visible content of the webpage.

  • The <meta> tag provides metadata such as page description, keywords, and author.
  • It helps browsers and search engines understand the content of the page.
  • One common use is specifying the character encoding: <meta charset="UTF-8">.

  • Forms collect user input using the <form> tag.
  • Inside a form, use <input>, <textarea>, <select>, and <button>.
  • The action attribute specifies where to send the form data.

  • The <label> tag defines a label for an input element.
  • It improves accessibility and allows users to click the label to focus the input.
    Example: <label for="email">Email:</label><input id="email">.

Comments in HTML are written between <!-- and -->.

Example:
<!-- This is a comment -->.
Comments are not displayed on the webpage and are used for documentation.

HTML entities are used to display reserved or special characters.

For example, &lt; displays < and &amp; displays &.
Use them to avoid confusion with actual HTML syntax.