id
AttributeThe HTML id
attribute is a powerful feature used to assign a unique identifier to an HTML element. This identifier helps developers easily target specific elements in a web page for styling with CSS, interaction with JavaScript, or linking within the same page.
id
Attribute?The id
attribute provides a unique identifier for an element. No two elements on the same web page should have the same id
. It ensures precise selection and targeting of an element for various purposes, including styling, scripting, and navigation.
id
Attribute
<element id="unique-id">
Content goes here...
</element>
id
Attributearia-labelledby
or aria-describedby
.id
Attribute
<!DOCTYPE html>
<html lang="en">
<head>
<style>
#main-header {
color: blue;
text-align: center;
}
</style>
</head>
<body>
<h1 id="main-header">Welcome to Vasusoft</h1>
<p>Learn programming and digital marketing.</p>
</body>
</html>
id
with JavaScript
<!DOCTYPE html>
<html lang="en">
<body>
<button id="click-btn">Click Me</button>
<p id="response"></p>
<script>
document.getElementById('click-btn').addEventListener('click', function() {
document.getElementById('response').innerText = 'Button clicked!';
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<body>
<a href="#section2">Go to Section 2</a>
<h2 id="section2">Section 2</h2>
<p>You are now in Section 2 of the page.</p>
</body>
</html>
id
Attributeid
within a page.header-banner
or footer-links
.id
across multiple elements.id
alone for styling; prefer class
for reusable styles.id
Attributeid
to an HTML element.#id-name
selector in CSS to style the element.getElementById()
.id
Attributeid
?id
must be unique within the same HTML document.id
different from class
?id
attribute is unique and applies to a single element, while the class
attribute can apply to multiple elements.id
attribute for styling?id
with CSS, but class
is more appropriate for reusable styles.