id Attribute in HTML

id Attribute in HTML ka use kisi HTML element ke liye unique id specify karne ke liye hota hai. Ek HTML document me ek hi id ko ek se zyada elements me use nahi kiya ja sakta.


The id Attribute

id Attribute in HTML ek HTML element ko unique identity deta hai.

Important points:

  • id ki value poore HTML document me unique honi chahiye

  • CSS me specific style apply karne ke liye use hota hai

  • JavaScript me specific element ko access aur manipulate karne ke liye use hota hai

Example:

id Attribute in HTML

Yahaan <h1> element myHeader id ko point kar raha hai aur head section me defined style ke according style hoga.


Syntax of id in CSS

id Attribute in HTML ko CSS me refer karne ke liye hash symbol (#) use hota hai.

Syntax:

Syntax of id in CSS
Syntax of id in CSS (2)

Yahaan #myHeader ek id selector hai.


Important Rules for id

id Attribute in HTML use karte waqt kuch rules follow karne chahiye:

  • id name case-sensitive hota hai

  • id name kam se kam ek character ka hona chahiye

  • id number se start nahi ho sakta

  • id name me spaces (whitespaces) allowed nahi hote

Example:

Important Rules for id

Difference Between Class and ID

id Attribute in HTML aur class attribute me main difference yeh hai:

Feature Class ID
Usage Multiple elements me use ho sakta hai Sirf ek element me use hota hai
CSS symbol . (dot) # (hash)
Purpose Group styling Unique element identify karna

Example:

Difference Between Class and ID

HTML Bookmarks with ID and Links

id Attribute in HTML ka use bookmarks create karne ke liye bhi hota hai.

Bookmarks readers ko webpage ke specific part par jump karne me help karte hain, especially jab page bahut lamba ho.

Step 1: Bookmark create karo

Bookmark create karo

Step 2: Bookmark ka link add karo

Bookmark ka link add karo

Link par click karne par page direct Chapter 4 par scroll ho jayega.


Using the id Attribute in JavaScript

id Attribute in HTML JavaScript me bhi use hota hai taaki specific element par action perform kiya ja sake.

JavaScript ka getElementById() method specific id wale element ko access karta hai.

Example:

Using the id Attribute in JavaScript

Is example me button click karne par JavaScript text ko change kar deta hai.


Is tarah id Attribute in HTML ek HTML element ko unique identity dene ke liye use hota hai, jisse CSS aur JavaScript ke through specific elements ko easily style aur control kiya ja sakta hai.

Leave a Comment