- HTML Tutorial
- HTML - Home
- HTML - History and Evolution
- HTML - Overview
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Headings
- HTML - Paragraphs
- HTML - Quotations
- HTML - Comments
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Style Sheet
- HTML - CSS Classes
- HTML - CSS IDs
- HTML - Images
- HTML - Image Map
- HTML Tables
- HTML - Tables
- HTML - Headers & Caption
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Iframes
- HTML - Blocks
- HTML Backgrounds
- HTML - Backgrounds
- HTML Colors
- HTML - Colors
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Code Builder
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - id Attribute
An HTML element that is used to identify an HTML element on a webpage is the id attribute. It is a string that has been allocated to an element to make it accessible using JavaScript and CSS. To enable for accurate element targeting, each id within the HTML document needs to be unique.
This attribute is frequently used for style, managing events, and changing the structure of documents. In order to create interactive and dynamic web pages, it gives developers the ability to target particular parts and provide them specialized behavior and appearance.
Syntax
Following is the syntax of the HTML id attribute −
<tag id = ‘id_name’></tag>
Example
In the following example, we are using the 'id' attribute within the 'h1' element to define a unique identifier (id) for it. Using that ID, we can access it in the stylesheet for styling.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML id attribute</title> <style> #demo { font-size: 25px; font-style: italic; color: green; font-weight: bold; } </style> </head> <body> <!-- Example of the HTML 'id' attribute --> <p>Example of the HTML 'id' attribute</p> <h1 id='demo'>This is h1 heading.</h1> </body> </html>
When we run the above code, it will generate an output consisting of the h1 text applied CSS displayed on the webpage.
Example
Considering the another scenario, where we are going to use the id attribute with the div element.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML id attribute</title> <style> /*access element using #id_name */ #demo { width: 310px; height: 120px; background-color: green; align-items: left; border-radius: 10px; } #demo p { color: white; letter-spacing: 1px; text-align: center; } </style> </head> <body> <!-- Example of the HTML 'id' attribute --> <p>Example of the HTML 'id' attribute</p> <div id="demo"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maiores nostrum ducimus, iure quaerat hic nisi ipsam architecto sequi blanditiis harum beatae magnam iste obcaecati dolore. Ut commodi voluptates labore aliquam.</p> </div> </body> </html>
On running the above code, the output window will pop up, displaying the div text applied with CSS on the webpage.
Example
Let's look into the another example, where we are going to use the id attribute with the form element
<!DOCTYPE html> <html lang="en"> <head> <title>HTML id attribute</title> <style> /*access element using #id_name */ #myForm { width: 300px; height: 210px; background-color: aqua; border-radius: 10px; text-align: center; } #myForm h1 { text-align: center; font-family: sans-serif; margin-top: 5px; } #myForm input { padding: 8px; margin: 5px 0px; } #myForm button { width: 105px; padding: 8px; } </style> </head> <body> <!-- Example of the HTML 'id' attribute --> <p>Example of the HTML 'id' attribute</p> <form id='myForm'> <h1>Login</h1> <br> Username: <input type="text"> <br> Password: <input type="password"> <br> <button>Login</button> </form> </body> </html>
When we run the above code, it will generate an output consisting of the form applied with CSS displayed on the webpage.
Example
Following is the example, where we are going to run the script to perform the arthimetic operation
<!DOCTYPE html> <html lang="en"> <head> <title>HTML id attribute</title> <style> /*access element using #id_name */ #myForm { width: 300px; height: 210px; background-color: aqua; border-radius: 10px; text-align: center; } #myForm h1 { text-align: center; font-family: sans-serif; margin-top: 5px; } #myForm input { padding: 8px; margin: 5px 0px; } #myForm button { width: 105px; padding: 8px; } </style> </head> <body> <!-- Example of the HTML 'id' attribute --> <p>Example of the HTML 'id' attribute</p> <form id='myForm'> <h1>Login</h1> <br> Username: <input type="text"> <br> Password: <input type="password"> <br> <button>Login</button> </form> </body> </html>
On excuting the above script, the output window will pop up displaying the input field along with a click button on the webpage. when the user fills the data and clicks the button it will perform addition between them and displays as alert.
To Continue Learning Please Login
Login with Google