
HTML ID
HTML ID is an attribute used to uniquely identify an HTML element on a web page. The ID attribute is defined using the id keyword and a unique identifier name that can contain letters, numbers, hyphens, and underscores.
In below example, we have assigned IDs to several HTML elements, such as the header
, nav
, and section
elements, as well as the a
elements inside the nav
and section
elements.
The id
attribute is used in the CSS and JavaScript code to manipulate or style specific HTML elements. For example, if we wanted to change the background color of the header
element, you can use CSS bellow.
This code selects the header
element with the ID of main-header
and applies the specified styles to it.
Similarly, in JavaScript, we can use the getElementById
method to select an HTML element with a specific ID and manipulate it. For example, if we wanted to change the text of the a
element with the ID of portfolio-link
, you can use JavaScript below.
Using HTML IDs allows developers to uniquely identify and manipulate specific HTML elements on a web page, making it easier to add dynamic functionality and interactivity to the site.