
- CSS - Home
- CSS - Roadmap
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Types
- CSS - Measurement Units
- CSS - Selectors
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS - Cascading
- CSS - Universal Selectors
- CSS - ID Selectors
- CSS - Group Selectors
- CSS - Class Selectors
- CSS - Child Selectors
- CSS - Element Selectors
- CSS - Descendant Selectors
- CSS - General Sibling Selectors
- CSS - Adjacent Sibling Selectors
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Advanced Features
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS Interview Questions
- CSS Online Quiz
- CSS Online Test
- CSS Mock Test
- CSS - Quick Guide
- CSS - Cheatsheet
- CSS - Properties References
- CSS - Functions References
- CSS - Color References
- CSS - Web Browser References
- CSS - Web Safe Fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS - ID Selectors
ID Selectors in CSS
CSS ID selector selects a single element with a particular value for the id attribute. An id in CSS is denoted by the "#" (hash) symbol. The same class can be applied to multiple elements, but an id is unique for an element.
Syntax
The syntax for the CSS id selector is as follows:
#id { /* property: value; */ color: #04af2f }
ID Selector for Styling a div Element
CSS ID selector can be used to select various elements such as div, p , section, etc.
Example
In this example, we have used an ID selector to style the div element. We have used background-color, color, padding, border, and text-align properties to style and center the text in the div element.
<!DOCTYPE html> <html lang="en"> <head> <title>Document</title> <style> #myId { background-color: #04af2f; color: white; font-size: 20px; font-family: Verdana, sans-serif; padding: 10px; border: 1px solid black; text-align: center; } </style> </head> <body> <div id="myId"> This is a div element having various styles. </div> </body> </html>
ID Selector with Pseudo-Classes
CSS ID selector can be used with pseudo classes to add interactivity to the element such as changing text color on hovering any link.
Example
In this example, we have used hover pseudo-class to change the link text color upon hovering over it.
<!DOCTYPE html> <html lang="en"> <head> <title>Document</title> <style> #myId { color: #04af2f; } #myId:hover { color: #031926; } </style> </head> <body> <a href="#" id="myId"> Hover over this link to change its color. </a> </body> </html>
Using ID Selector with Forms
CSS ID selector can be used with forms to design the form elements.
Example
In this example, we have used an id selector on form elements like buttons and input fields. We have designed the input field and changed the appearance of the submit button.
<!DOCTYPE html> <html lang="en"> <head> <title>Document</title> <style> #user { border: 2px solid black; border-radius: 5px; font-size: 16px; font-family: Verdana, sans-serif; } #btn { background-color: #031926; color: white; padding: 10px 15px; border: none; border-radius: 5px; cursor: pointer; } </style> </head> <body> <form> <label for="username">Username:</label> <input type="text" id="user" name="username" placeholder="Enter your username"> <br><br> <button type="submit" id="btn">Submit</button> </form> </body> </html>
ID Selector for Animations
CSS ID selector can be used with CSS animations for creating smooth animation effects.
Example
In this example, we have used an id selector to animate a bouncing ball. CSS keyframes specify the frame sequence and translateY() function of the transform property to control the bouncing of the ball.
<!DOCTYPE html> <html lang="en"> <head> <title>Document</title> <style> #animated { width: 100px; height: 100px; background-color: #04af2f; font-family: Verdana, sans-serif; text-align: center; line-height: 100px; color: white; border-radius: 50%; animation: bounce 1s infinite; } @keyframes bounce { 0%, 100% { transform: translateY(20px); } 50% { transform: translateY(0); } } </style> </head> <body> <div id="animated">Bounce</div> </body> </html>
Id Selector with Transition
CSS ID selector can be used with CSS transition for creating a smooth transition from one state to another.
Example
In this example, we have used scale() function and background-color property to change the size and color of div box when hovered over it.
<!DOCTYPE html> <html lang="en"> <head> <title>Document</title> <style> #box { width: 400px; height: 100px; background-color: #04af2f; text-align: center; font-family: Verdana, sans-serif; line-height: 100px; color: white; border-radius: 10px; transition: all 0.5s ease; margin: auto; } #box:hover { background-color: #031926; transform: scale(0.95); cursor: pointer; } </style> </head> <body> <div id="box">Hover Over this div Element</div> </body> </html>