The id attribute in HTML is used to assign a unique identifier to an element. This unique ID can then be used by CSS for styling and JavaScript for scripting, allowing for targeted operations on specific elements within the document.
1. Uniqueness: Each id value must be unique within a page. No two elements can have the same id in a single HTML document.
2. CSS Styling: Use the id selector in CSS to apply styles specifically to the element with that id.
3. JavaScript Access: Use JavaScript to manipulate or retrieve the element with a specific id.
<tag id="value">
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML id Attribute Example</title>
<style>
#header {
color: green;
font-size: 28px;
}
#special-box {
background-color: lightblue;
border: 2px solid darkblue;
padding: 15px;
}
</style>
</head>
<body>
<h1 id="header">This is a heading with an id</h1>
<div id="special-box">This is a special box with a unique id.</div>
</body>
</html>