HTML Id

HTML id attributes are used to uniquely identify an element within an HTML document. Unlike classes, which can be applied to multiple elements, an id should be unique within the document. This uniqueness makes id attributes particularly useful for styling specific elements and for selecting elements with JavaScript.

Syntax

To add an id to an HTML element, use the id attribute:


<tag id="unique-id">Content</tag>

Where tag is HTML element like <div>, <p>, etc.

Example

Here’s an example demonstrating how to use id in HTML:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML Id Example</title>
    <style>
        #header {
            background-color: blue;
            color: white;
            padding: 10px;
            text-align: center;
        }
        #content {
            padding: 20px;
            background-color: lightgrey;
        }
        #footer {
            background-color: blue;
            color: white;
            padding: 10px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div id="header">
        <h1>Header Section</h1>
    </div>
    <div id="content">
        <p>This is the main content area.</p>
    </div>
    <div id="footer">
        <p>Footer Section</p>
    </div>
</body>
</html>

Explanation of the Example

1. HTML Structure: The HTML document includes three main sections: header, content, and footer.

2. Id Assignment: Each section is assigned an id (header, content, footer) using the id attribute.

3. CSS Styling: In the <style> block, each id is styled

  • #header: Blue background, white text, padding, and center-aligned text.
  • #content: Light grey background and padding.
  • #footer: Similar styling to the header.

Benefits of Using Ids

Uniqueness: Ensures that each id is unique within the document, providing a way to specifically target individual elements.

Specificity: Provides higher specificity in CSS, which can be useful for overriding other styles.

JavaScript Targeting: Easily select and manipulate elements with JavaScript using methods like getElementById.

JavaScript Example

Ids are commonly used in JavaScript to access and manipulate specific elements:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript Id Example</title>
    <style>
        #header {
            background-color: blue;
            color: white;
            padding: 10px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div id="header">
        <h1>Header Section</h1>
    </div>
    <button onclick="changeHeaderColor()">Change Header Color</button>

    <script>
        function changeHeaderColor() {
            document.getElementById('header').style.backgroundColor = 'green';
        }
    </script>
</body>
</html>

Explanation of the JavaScript Example

1. HTML Structure: The document includes a header and a button.

2. Id Assignment: The header div is assigned an id of header.

3. JavaScript Function: The changeHeaderColor function uses getElementById to select the header element and change its background color to green when the button is clicked.

By using id attributes effectively, you can create specific, targeted styles and behaviors for individual elements within your web pages.