- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Does ID have to be unique in the whole HTML page?
Yes, IDs must be unique in the entire HTML page. Even the official HTML standards suggest the same −

Unique IDs with the id attribute
Example
Let us see an example. Here, we have used the id attribute −
<!DOCTYPE html> <html> <head> <style> #myHeader { border: 2px solid yellow; background-color: orange; padding: 50px; text-align: center; } </style> </head> <body> <h1 id="myHeader"> Demo Heading </h1> <p>This is a text outside.</p> </body> </html>
Output

Displaying four different unique IDs
Another example displaying four different unique IDs −
<!DOCTYPE html> <html> <head> <style> #container { width: 100%; font-size: 10px; text-align: center; } #left { float: left; width: 100px; border: 2px solid green; } #right { float: right; width: 100px; border: 2px solid orange; } #center { margin: 0 auto; width: 100px; border: 2px solid red; } </style> </head> <body> <div id="container"> <div id="left"> <h1>Left</h1> </div> <div id="right"> <h1>Right</h1> </div> <div id="center"> <h1>Center</h1> </div> </div> </body> </html>
Output

- Related Articles
- Why does the result of id() appear to be not unique in Python?
- How to specify a unique id for an element in HTML?
- How to add a unique id for an element in HTML?
- Can different HTML elements have the same ID?
- Make HTML5 Canvas fill the whole page
- Is it possible to have an HTML canvas element in the background of my page?
- How to access unique Android device ID?
- How to make page links in HTML Page?
- How to get innerHTML of whole page in selenium driver?
- Does a favicon have to be 32x32 or 16x16?
- Auto increment in MongoDB to store sequence of Unique User ID?
- How to create a unique ID for each object in JavaScript?
- HTML id Attribute
- How to specify that an option should be pre-selected when the page loads in HTML?
- How to specify that an element should be pre-selected when the page loads in HTML?

Advertisements