The ID selector in CSS is used to select a single element on a page by referencing its id attribute. This attribute must be unique within a page, meaning no two elements can have the same id. The ID selector is prefixed with a hash (#) symbol in CSS.
Basic ID Selector
The ID selector allows you to style a specific element by targeting its unique id attribute. This is perfect for applying custom styles to an element that only appears once on the page.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
#header {
color: blue;
font-size: 24px;
text-align: center;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<div id="header">Header Content</div>
</body>
</html>
<!--Driver Code Ends }-->
Syntax
#elementId {
/* styles */
}Where element Id is the value of the id attribute on the HTML element you want to target.
How Does the ID Selector Work
The ID selector is extremely specific because the id attribute is unique within a page. Therefore, using the #id selector is an efficient way to target a single element for styling. This specificity also means that the styles applied using an ID selector will generally override other, less specific selectors like class selectors.
1. ID Selector for Styling a Button
Use the ID selector to target buttons and apply unique styling, such as background colors and padding. This is useful for creating buttons with distinctive designs across your website.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
#submitBtn {
background-color: green;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
}
#submitBtn:hover {
background-color: darkgreen;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<button id="submitBtn">Submit</button>
</body>
</html>
<!--Driver Code Ends }-->
2. Styling Section with a Specific ID
ID selectors can be used to style entire sections of a webpage, making it easy to create consistent layouts. For example, you can apply a background color and padding to a specific section of content.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
#about {
background-color: #f4f4f4;
padding: 20px;
margin-top: 20px;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<section id="about">
<h2>About Us</h2>
<p>We are a leading company in the industry...</p>
</section>
</body>
</html>
<!--Driver Code Ends }-->
3. Combining ID Selector with Pseudo-Classes
By combining the ID selector with pseudo-classes like :hover, you can create interactive elements. For instance, changing the background color of a button when the user hovers over it.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
#submitBtn {
background-color: green;
color: white;
padding: 10px 20px;
}
#submitBtn:hover {
background-color: darkgreen;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<button id="submitBtn">Submit Again</button>
</body>
</html>
<!--Driver Code Ends }-->
4. Targeting Nested Elements with an ID Selector
ID selectors can also target nested elements within a specific container. This allows for precise styling of child elements like text or links within a div or section.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
#footer {
background-color: #333;
color: white;
padding: 20px;
}
#footer span {
font-weight: bold;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<div id="footer">
<p>Contact us: <span>info@example.com</span></p>
</div>
</body>
</html>
<!--Driver Code Ends }-->
5. ID Selector for Navigation
When styling navigation bars, the ID selector provides a way to apply unique styles to the entire navigation element. It helps define things like font size, background color, and alignment.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
#nav {
font-size: 18px;
background-color: #333;
color: white;
padding: 10px;
text-align: center;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<nav id="nav">Navigation Menu</nav>
</body>
</html>
<!--Driver Code Ends }-->
6. ID Selector for Targeting Forms
You can use the ID selector to style forms and their elements individually, applying padding, borders, and spacing. This is ideal for making forms visually appealing and easy to fill out.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
#contactForm {
background-color: #f9f9f9;
padding: 20px;
border-radius: 5px;
}
#contactForm input {
margin-bottom: 10px;
padding: 8px;
width: 100%;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<form id="contactForm">
<input type="text" name="name" placeholder="Your Name">
<input type="email" name="email" placeholder="Your Email">
<button type="submit">Submit</button>
</form>
</body>
</html>
<!--Driver Code Ends }-->
7. ID Selector for Elements with Specific Layout
The ID selector is perfect for styling elements that have unique layout requirements, such as footers. You can adjust margins, padding, and background colors to match the design of the page.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
#footer {
margin-top: 30px;
padding: 20px;
background-color: #222;
color: white;
text-align: center;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<div id="footer">Footer Content</div>
</body>
</html>
<!--Driver Code Ends }-->
8. Combining Multiple IDs with Other Selectors
You can combine an ID selector with other selectors like element or class selectors to target specific child elements. This provides more control over nested structures, such as styling only h1 tags inside a section with a specific ID.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
#header h1 {
color: red;
font-size: 24px;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<section id="header">
<h1>Welcome to My Website</h1>
</section>
</body>
</html>
<!--Driver Code Ends }-->
9. Using ID Selector for Unique Content
When you need to apply unique styles to specific pieces of content, use the ID selector to ensure that particular elements stand out. This is great for creating distinctive sections like call-to-action boxes.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
#uniqueBox {
background-color: lightblue;
padding: 15px;
border: 1px solid #ccc;
text-align: center;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<div id="uniqueBox">This is a unique content box.</div>
</body>
</html>
<!--Driver Code Ends }-->
10. Overriding Other Selectors Using ID Selector
Due to its high specificity, the ID selector can override other selectors like class-based styles. This is useful when you want to ensure a particular element’s style takes precedence.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
.box {
background-color: lightgray;
padding: 15px;
text-align: center;
}
#specialBox {
background-color: yellow;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<div class="box">This is a general box.</div>
<div id="specialBox" class="box">This is a special box with unique styling.</div>
</body>
</html>
<!--Driver Code Ends }-->
11. ID Selector for Animations or Transitions
The ID selector can be combined with CSS transitions or animations to add dynamic effects to elements. This allows you to smoothly animate properties like size or position when the element is hovered or clicked.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
#box {
background-color: lightcoral;
width: 100px;
height: 100px;
transition: transform 0.3s;
margin: 20px;
}
#box:hover {
transform: scale(1.2);
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<div id="box"></div>
</body>
</html>
<!--Driver Code Ends }-->
12. Overriding element selector by id selector
The ID selector has a higher specificity than element selectors, meaning it will override any styles applied to elements of the same type. This allows you to apply unique styles to specific elements without affecting others.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
p {
color: green;
font-size: 16px;
}
#specialText {
color: red;
/* Overrides the paragraph color */
font-size: 20px;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<p>This is a regular paragraph.</p>
<p id="specialText">This paragraph has overridden styles using an ID selector.</p>
</body>
</html>
<!--Driver Code Ends }-->
CSS #id Selector – FAQ’s
What is the #id selector in CSS?
The #id selector in CSS is used to target an HTML element with a specific id attribute. Each ID should be unique within a page, and the selector applies styles only to the element that matches the specified ID.
Can I use the same id on multiple elements?
No, an id should be unique within a page. Using the same id on multiple elements violates the principle of uniqueness and can cause unexpected styling issues. For targeting multiple elements, it’s better to use class selectors.
What is the specificity of the #id selector?
The #id selector has a higher specificity than class (.class) and element (div, p, etc.) selectors. It is one of the most specific selectors, meaning it will override styles from less specific selectors unless there are inline styles or other ID selectors with equal specificity.
Can I combine the #id selector with other selectors?
Yes, you can combine the #id selector with other selectors, such as element (#id p) or pseudo-classes (#id:hover). This helps target specific parts of an element with a certain ID or adds interactivity with pseudo-classes like :hover.
How does the #id selector affect performance in CSS?
The #id selector is generally very efficient because IDs are unique within a page, so the browser can quickly find and apply styles to the target element. However, performance is not typically an issue unless there are many complex selectors or an excessive amount of styles applied.
Similar Reads
CSS [attribute*=value] Selector
The [attribute*="str"] selector targets the elements whose attribute values contain a specified substring. This substring can appear anywhere within the attribute's value — beginning, end, or middle. Syntax: element [attribute*="str"] { // CSS Property} Example: In the following example, the <p
2 min read
CSS [attribute=value] Selector
The [attribute=value] selector in CSS is used to select those elements whose attribute value is equal to "value". Syntax: element [attribute = "value"] { // CSS Property}Note: <!DOCTYPE> must be declared for IE8 and earlier versions. Example 1: In this example, The selector h1[id="geeks"] targ
2 min read
CSS [attribute$=value] Selector
The [attribute$=”value”] selector is used to select those elements whose attribute value ends with a specified value "value". The value need not to be present as separate word. It may be a part of another word or expression but it needs to be present at the end. Syntax: [attribute$="value"] { // CSS
2 min read
CSS [attribute|=value] Selector
The [attribute|=value] selector is used to select those elements whose attribute value is equal to "value" or whose attribute value started with "value" immediately followed by a hyphen (-). Note: Use <!DOCTYPE> to run [attribute|=value] selector in IE8 or earlier versions. Syntax: [attributeT
2 min read
CSS [attribute~=value] Selector
The [attribute~="value"] selector is used to select those elements whose attribute value contains a specified word. The "value" must be present in the attribute as a separate word and not as part of the other word i.e. if [title~=Geeks] is specified then all elements with Geeks title get selected. S
2 min read
CSS [attribute^=value] Selector
The [attribute^=value] selector selects elements whose attribute value begins with a given attribute. Syntax: [attribute^=value] { // CSS Property}Example: In this example, The CSS selector p[class^="for"] targets <p> elements with a class attribute that starts with "for" and applies a green b
2 min read
CSS #id Selector
The ID selector in CSS is used to select a single element on a page by referencing its id attribute. This attribute must be unique within a page, meaning no two elements can have the same id. The ID selector is prefixed with a hash (#) symbol in CSS. Basic ID SelectorThe ID selector allows you to st
8 min read
CSS * (Universal) Selector
The universal selector (*) applies styles to all elements on a page or within a specified context, regardless of their type, class, or ID. It's commonly used for global resets or universal styling. * { /* styles */}1. Basic Use case of universal selectorThe universal selector applies styles to all e
4 min read
CSS :active Selector
The: active selector is used in styling an active link of the web page. Style display when the user clicks on the link. This selector is different from :link, :visited and: hover selectors. The main use of : active selector is on the links but it can be used on all elements. Syntax: :active{ //CSS p
2 min read
CSS ::after Selector
::after selector is used to add the same content multiple times after the content of other elements. This selector is the same as ::before selector. Syntax: ::after{ content:}Below HTMl/CSS code shows the functionality of ::after selector : [GFGTABS] HTML <!DOCTYPE html> <html> <head
2 min read
CSS ::before selector
The CSS ::before selector allows you to insert content before an element's actual content. It is often used to add decorative elements or symbols, enhancing the design without changing the structure of the HTML. The ::before selector is paired with the content property, which defines the inserted co
3 min read
CSS :checked Selector
The checked selector is used to select all checked elements in the input tag and radio buttons. This selector is used with radio buttons, checkbox and option elements. Syntax: :checked { // CSS property} Example 1: In this example, The CSS :checked selector targets checked input elements. It sets a
3 min read
CSS Class Selector
CSS class selectors are one of the most versatile tools in your front-end development toolkit. They allow you to apply styles to multiple elements on a webpage, ensuring your design remains consistent and easily maintainable. Syntax .class-name{ property: value;}1. Basic class selectorThe basic clas
5 min read
CSS :default Selector
The :default selector is used to set a default element in a group of same type of element in a form. This selector is used on button, option, radio button and check-box. Syntax: :default { //property}Example: [GFGTABS] HTML <!DOCTYPE html> <html> <head> <style> input[type=
2 min read
CSS :disabled Selector
The: disabled selector is used to select the disabled element. This property is mostly used on the form elements. Syntax: :disabled { // CSS property} You can also Set a background color for all disabled input elements of type="text": input[type=text]:disabled { background: #dddddd;}Example 1: Here
2 min read
CSS element Selector
The element selector in CSS is used to select HTML elements that are required to be styled. In a selector declaration, there is the name of the HTML element and the CSS properties which are to be applied to that element is written inside the brackets {}. Syntax: element { \\ CSS property}Example 1:
2 min read
CSS Element Element Selector
The element element selector (without the > symbol) selects all descendant elements of a specified parent element. It applies styles not only to direct children but also to any deeper nested elements, making it more general than the direct child selector. Descendant selection: Targets all nested
4 min read
CSS element,element Selector
The element, element selector in CSS is used to style all comma-separated elements with the same style. Syntax: element,element { /* CSS Property */}Example 1: [GFGTABS] html <!DOCTYPE html> <html> <head> <title> element, element Selector </title> <style> /* Add s
2 min read
CSS element1~element2 Selector
The general sibling selector (~) in CSS is a powerful combinator used to target elements that are siblings of a specified element and appear after it in the DOM tree. Unlike the adjacent sibling selector (+), which only targets the immediately following sibling, the general sibling selector applies
4 min read
CSS :empty Selector
The: empty selector is used to select that element that does not contain any children (including text node). Syntax: :empty { /* CSS property */}Example: In this example, we are using the above-explained selector. [GFGTABS] html <!DOCTYPE html> <html> <head> <title>empty sele
2 min read
CSS :enabled Selector
The :enabled selector is used to set the background color in enabled element in a form. Syntax: :enabled { //property}Example: [GFGTABS] HTML <!DOCTYPE html> <html> <head> <style> input[type=text]:enabled { background: limegreen; } input[type=text]:disabled { background: silv
1 min read
CSS First Child Selector
CSS :first-child selector is used to select the elements that are the first child of the parent element. Syntax: :first-child { /* CSS property */ }Example 1: This example shows the use of the CSS first child selector. [GFGTABS] html <!DOCTYPE html> <html> <head> <title>first
2 min read
CSS :first-of-type Selector
The :first-of-type Selector is used to targeting the first child of every element of it's parent. if we want to style the first child of an element without giving a class, we can use it. Syntax: :first-of-type { //property}Example: [GFGTABS] HTML <!DOCTYPE html> <html> <head> <s
2 min read
CSS ::first-letter Selector
The ::first-letter selector CSS Pseudo-element is used to apply the style to the first letter of the first line of a block-level element, the condition is it should not be preceded by other content ( such as images or inline tables). Syntax: ::first-letter { // CSS Property}Accepted Properties: back
2 min read
CSS ::first-line Selector
The ::first-line selector in CSS is used to apply style to the first line of a block-level element. The length of the first line depends on many factors, including the width of the element, the width of the document, font-size of the text, etc. Syntax: ::first-line { //CSS Property}Example: [GFGTABS
2 min read
CSS :focus Selector
The :focus CSS pseudo-class Selector is used to target the focused element ie., it selects an element that is currently focused by the user. This selector works on user input elements, generally used in forms, and is triggered as soon as the user clicks on it or taps on an element, or selects any ke
2 min read
CSS :hover Selector
The CSS :hover selector is used for creating interactive and responsive designs. It allows you to apply styles to an element when the mouse pointer hovers over it. This effect is commonly used for buttons, links, images, and other UI elements, enabling you to enhance user experience through visual f
3 min read
CSS :indeterminate Selector
The :indeterminate selector in CSS is used to select any form elements that are in indeterminate state i.e a state that is neither checked nor unchecked. Elements targeted by this selector are: <input = "checkbox"> elements whose indeterminate property is set to true by JavaScript<input = "
2 min read
CSS :in-range Selector
The :in-range selector is used to selecting all elements that are given within a particular range value. It is mostly used in an input element with the type min/max attribute. Syntax: :in-range { // CSS property} Example: [GFGTABS] html <!DOCTYPE html> <html> <head> <title>in
2 min read
CSS :invalid Selector
This :invalid selector is used to select every form elements which does not validate according to the elements. This selector works only for some form elements with limitations such as input elements with min and max attributes, email fields with a legal email, or number fields with a numeric value,
2 min read
CSS :lang Selector
The :lang Selector is used to target the element which is based on language attributes for a specific value. This selector work with the help of language-code. Syntax: :lang(lang-code) { //property}Language Codes: en: for English.hi: for Hindi.fr: for French.de: for Germanit: for Italianca: for Cana
2 min read
CSS :last-child Selector
The CSS :last-child selector targets the last child element within its parent. It applies styles exclusively to the last child based on its position within the parent container, regardless of the element type or class. If the parent element is missing or incorrectly structured, the selector will not
3 min read
CSS :last-of-type Selector
The :last-of-type Selector is used to target the last child element of the same type of it's parent for styling. This selector is same as ":nth-last-of-type". Syntax: :last-of-type { //property}Example: [GFGTABS] HTML <!DOCTYPE html> <html> <head> <style> p:last-of-type { bac
2 min read
CSS :link Selector
The :link is used to target the unvisited link for styling not the links which is already visited. For styling on visited use ":visited", for styling on click use ":active" and for styling move mouse on link use ":hover". Syntax: :link { //property}Example: [GFGTABS] HTML <!DOCTYPE html> <h
2 min read
CSS :not Selector
The :not(selector) selector is used to style every element that is not specified by the given selector. Known as the negation pseudo-class, it allows developers to exclude specific items from being selected. Syntax: :not(selector) { //CSS Property}Usage ExamplesExample 1This example demonstrates the
2 min read
CSS :nth-child() Selector
The :nth-child() pseudo-class selector targets elements based on their position among siblings, making it ideal for dynamic styling. It can select elements using specific positions (e.g., first, odd, or even), or patterns like An+B. This flexible CSS tool improves web design by simplifying how you a
3 min read
CSS :nth-last-child() Selector
The nth-last-child() selector in CSS is used for selecting elements based on their position among sibling elements, but starting the count from the end rather than the beginning. This selector is particularly useful for styling elements dynamically based on their order in reverse, allowing for flexi
3 min read
CSS :nth-last-of-type() Selector
The:nth-last-of-type() Selector in CSS is used to style only those elements which are the nth number of child of the parent element. The selector counts from the last-child element. A n could be a number, a keyword, or a formula. Syntax: :nth-last-of-type(number) { //css Property;} Where number is t
3 min read
CSS :nth-of-type() Selector
The :nth-of-type() in css Selector is used to style only those elements which are the nth number of child of its parent element. An n may be a number, a keyword, or a formula. Syntax: :nth-of-type(number) { // CSS Property;} Where number is the argument that represents the pattern for matching eleme
2 min read
CSS optional Selector
The: optional selector in CSS is used to select and style the form input elements which are optional. That is it can select those input elements from an HTML form that are not declared as "required". Syntax: :optional { /* css declarations; */}Example: [GFGTABS] HTML <!DOCTYPE html> <html
2 min read
CSS :only-child Selector
The:only-child selector in CSS is used to match every element that is the only child of its parent. It represents an element without any siblings. Syntax: :only-child { // CSS property} Example 1: [GFGTABS] html <!DOCTYPE html> <html> <head> <title>:only-child selector</
2 min read
CSS :only-of-type Selector
The :only-of-type selector in CSS represents only those elements that have no siblings of the given type. It is used to set the CSS property to that element. Syntax: :only-of-type { // CSS property}Example 1: In this example, we are using the above-explained selector. [GFGTABS] html <!DOCTYPE ht
2 min read
CSS :out-of-range Selector
The:out-of-range selector is used to select the elements that are lying outside the given range. This selector only works for an input element with max and min attributes. Example Application: The marks of a student should be from 0 to 100. If an instructor tries to enter an outside range, we can hi
2 min read
CSS ::placeholder Selector
The placeholder selector in the CSS pseudo-element is used to design the placeholder text by changing the text color and it allows to modification the style of the text. The ::first-line pseudo-element is the only subset of CSS properties that is applicable & can be applied in a rule using ::pla
2 min read
CSS :read-only Selector
The :read-only selector is used to select an element that is readable only. The read-only is used to disable the fields. Mainly read-only pseudo-class represents an element that can’t be edited by the user. Syntax: :read-only { // CSS property}Example 1: [GFGTABS] html <!DOCTYPE html> <htm
2 min read
CSS :read-write Selector
The :read-write selector is used to select an element (such as an input text) that is editable by the user. The elements with no readonly and disabled attribute are defined as readable and writable. Syntax: :read-write { // CSS Property}Example 1: [GFGTABS] HTML <!DOCTYPE html> <html>
2 min read
CSS :required Selector
The required selector is used to select the required element with the required attribute and set the CSS property. Required attributes of Form elements are defined as required. Note: This selector is used with form elements, eg. <input>, <select>, and <textarea>. Syntax: :required
2 min read
CSS :root Selector
The :root selector is a CSS pseudo-class that matches the root element of the document. In an HTML document, this is the <html> element. While it behaves similarly to the html selector, the :root selector has higher specificity and is commonly used to define global styles or CSS custom propert
3 min read
CSS ::selection Selector
The ::selection CSS pseudo-element allows you to style the part of a webpage that users highlight, such as when they drag the mouse over text. It’s a simple way for developers to customize the appearance of selected text, improving the overall user experience. However, only a few CSS properties can
2 min read
CSS :target Selector
The target selector is used to represent a unique element (the target element) with an id matching the URL's fragment. It can be used to style the current active target element. URLs with a # followed by an anchor name link to a certain element within a document. The element being linked to is the t
2 min read
CSS : valid Selector
The :valid selector in CSS is used to select form elements that are valid according to their specific validation rules. It applies to input elements like text fields, email fields, number fields, and more. For example, it works on input elements with min and max attributes, email fields with valid e
2 min read
CSS :visited Selector
The :visited selector in CSS is used to select the visited links. For example, visit some link on the website and again see it, then it will find the changed color of that link. This color change is done by :visited the selector. The allowed CSS property of : visited selectors are listed below: colo
2 min read