HTML id Attribute

HTML id Attribute

HTML id attribute specifies unique element within HTML document. Keep in mind that, same id attribute of HTML element can not assign to other HTML element.

Note HTML id attribute specifies unique HTML element means no other HTML element can put same id.

<h2 id="main_heading">This is HTML heading tag</h2>

Example

Source Code

Try it now

Note:Here,HTML element <h2> having unique id(main_heading). That will be unique inside the HTML document.

Writing CSS Style To HTML Id Element

To write a cascade style sheet for the specific Id of the HTML element, write a hash character followed by an id name. Then define the CSS rule inside curly bracket{}.

#main_heading { background-color:orange; color: black; padding: 40px; text-align: center; }

Example

Source Code

Try it now

Note: The CSS rule has been written with respect to the unique id of the HTML element is only applicable to a single HTML element.

Javascript:Fetching HTML Element Through Id

Example

Source Code

Try it now

Note:Javascript is used getElementById() method to fetch HTML element having a unique id.

Difference Between Id And CLass

Example

Source Code

Try it now

Note:An HTML element has a unique id means this id can not assign to other HTML elements while a class to be assigned more than one HTML element.

Online Test / Quiz