Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • HTML Tutorial
  • HTML Exercises
  • HTML Tags
  • HTML Attributes
  • Global Attributes
  • Event Attributes
  • HTML Interview Questions
  • HTML DOM
  • DOM Audio/Video
  • HTML 5
  • HTML Examples
  • Color Picker
  • A to Z Guide
  • HTML Formatter
Open In App
Next Article:
What is the use of integrity attribute in HTML ?
Next article icon

What are valid values for the id attribute in HTML?

Last Updated : 08 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The id attribute is a unique identifier which is used to specify a document. The id attribute is used using #(hash) symbol followed by id. The value must be unique amongst all the IDs in the element’s home subtree. Syntax:

  <tag id = #Values>

Permitted values for ID attribute: As of HTML5, id must satisfy these three conditions:

  • Must be unique in the document.
  • Must not contain any space characters.
  • Must contain at least one character.

So the value can be all digits, just one digit, include special characters, etc. Just no whitespace. In HTML 5 these values of ids are valid:

  <tag id = "#999" > .... </tag >    <tag id = "#%LV-||" > .... </tag >    <tag id = " ____V" > .... </tag >    <tag id = "{}" > .... </tag >    <tag id = " ©" > .... </tag > 

Note: Using numbers or special characters in the value of an ID may cause trouble in other contexts (CSS, JavaScript). Example:

 <tag id = ".\1gfg" > .... <\tag >

This ID is valid in HTML 5 But in CSS, javaScript identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9]. Now we will see examples of valid and invalid ID values in HTML and CSS. Example 1: The value of id is 1gfg and 1geeks which are valid in HTML 5 but not in CSS. So we just get simple output instead of styled output because the value of ID is invalid in CSS. 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>Id Attributes</title>
    <style>
        #1gfg {
            color: #009900;
            font-size: 50px;
            font-weight: bold;
            text-align: center;
        }
         
        #1geeks {
            text-align: center;
            font-size: 20px;
        }
    </style>
</head>
 
<body>
    <div id="1gfg">GeeksforGeeks</div>
    <div id="1geeks">
      A computer science portal for geeks
  </div>
</body>
 
</html>
 
 

Output: Example 2: Now we will change the value of IDs but code remains same as before. The value of id is gfg and geeks which are valid in HTML 5 as well as valid in CSS. So we will get styled output this time, Because the value of ID is valid in CSS. 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>Id Attributes</title>
    <style>
        #gfg {
            color: #009900;
            font-size: 50px;
            font-weight: bold;
            text-align: center;
        }
         
        #geeks {
            text-align: center;
            font-size: 20px;
        }
    </style>
</head>
 
<body>
    <div id="gfg">GeeksforGeeks</div>
    <div id="geeks">
      A computer science portal for geeks
  </div>
</body>
 
</html>
 
 

Output: We just saw an example of valid ID values in HTML and CSS. Now we see another example of valid and invalid ID values in HTML and javaScript. Example 3: We will take ID value .\1gfg which is valid in HTML 5 but invalid in javaScript. So After clicking change text button nothing will happen cause value of ID is invalid for javaScript. 

html




<html>
 
<body>
 
    <h1 id=".\1gfg">Hello Geeks!</h1>
    <button onclick="displayResult()">
      Change text
  </button>
 
    <style>
        #.\1gfg {
            color: green;
        }
    </style>
 
    <script>
        function displayResult() {
            document.getElementById(
              ".\1gfg").innerHTML = "GeeksForGeeks!";
        }
    </script>
 
</body>
 
</html>
 
 

Output: Now we will see another example in which we will change the value of ID from .\1gfg to gfg and change button will work Hello Geeks! will replace by GeeksForGeeks with Green color, because value of ID is valid for JavaScript. Example 4: 

html




<html>
 
<body>
 
    <h1 id="gfg">Hello Geeks!</h1>
    <button onclick="displayResult()">
      Change text
  </button>
    <style>
        #gfg {
            color: green;
        }
    </style>
    <script>
        function displayResult() {
            document.getElementById(
              "gfg").innerHTML = "GeeksForGeeks!";
        }
    </script>
 
</body>
 
</html>
 
 

Output:



Next Article
What is the use of integrity attribute in HTML ?
author
light_yagami07
Improve
Article Tags :
  • HTML
  • Web Technologies
  • HTML-Attributes
  • HTML-Misc

Similar Reads

  • What are custom attributes in HTML5 ?
    Custom attributes are attributes that are not part of the standard HTML5 attributes but are explicitly created. They allow us to add our information to HTML tags. These attributes store private information specific to a page or application, providing a way to add custom data to HTML elements Any att
    3 min read
  • What is the use of integrity attribute in HTML ?
    In this article, we will learn the purpose of using the integrity attribute in HTML & will also understand its implementation through an example. HTML introduced a new attribute called "integrity". The purpose of the integrity attribute is used to give permission to the browser to check the fetc
    2 min read
  • What is formmethod Attribute in HTML Form ?
    What is formmethod Attribute?The form method attribute in HTML is used to define a HTTP technique that specifies how to send form-data to the backend server. This attribute is apply on <button> , <input type= "submit"> and <input type="image">. It overrides the feature of the metho
    2 min read
  • HTML input value Attribute
    The HTML input value attribute defines the initial value of an <input> element, such as text fields, checkboxes, or radio buttons. It sets the default content or state for the input before user interaction or form submission. It has different meanings for different input types: The "button", "
    2 min read
  • Placeholder vs Value Attributes in HTML
    Placeholder Attribute:The placeholder attribute specifies a short hint that describes the expected value of an input field/textarea. The short hint is revealed in the field before the user enters a value. It is just a temporary hint and has nothing to do with logical executions in the backend. Synta
    3 min read
  • HTML | <li> value Attribute
    The <li> value attribute in HTML is used to specify the initial value of the list item. It is only applicable on the ordered list. Syntax: <li value="number">list items </li> Attribute Value: This attribute contains single value number which is used to specify the value of the list
    1 min read
  • What are HTML Attributes ?
    HTML attributes are the entities that provide the extra information about the tags. Attributes are specified using name and value pair. Some HTML tags are used without attributes while for some tags it's important to specify attributes along with them. In paired tags attributes are specified in the
    3 min read
  • HTML <input> form Attribute
    The HTML <input> form Attribute is used to specify interactive input fields for web-based forms. A form can contain multiple input fields to accept inputs from the users. It is the most powerful element in HTML. Syntax: <input form="form_id"> Attribute Value: This attribute contains a si
    1 min read
  • HTML | <input> list Attribute
    The HTML <input> list Attribute is used to identify a list of pre-defined options for an element to suggest the user. Syntax: <input list="datalist_id"> Attribute Values: datalist_id: It is used to specify the Id of the datalist that will used to make a link up with the input element. Ex
    1 min read
  • HTML <param> value Attribute
    The HTML <param> value Attribute defines the value of a <param> element, used with the name attribute to specify parameters for plugins within <object> tags. Syntax:  <param value="value"> Note: This attribute is depreciated from HTML 5. Attribute: Name Description value It i
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences