🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

ID Selectors

AI & DATA SCIENCE // id-selectors

ID selectors, written with a leading hash like #header, target the single element carrying that specific id attribute, since id values are meant to be unique within a page.

Syntax

#idname {
  property: value;
}

Deep Dive Course

An id selector matches the one element in the document whose id attribute equals the given name — because id values are meant to be unique per page, id selectors are inherently meant for one-off, page-specific targeting rather than reusable styling, and they carry considerably higher specificity than a class selector, which makes them noticeably harder to override later from elsewhere in a stylesheet. IDs also serve non-styling purposes, like being the target of an in-page anchor link (#section) or a target for JavaScript's getElementById(), which is one reason many style guides recommend reserving id attributes for those functional purposes and using classes for styling instead.

1Understanding ID Selectors

An id selector matches the one element in the document whose id attribute equals the given name — because id values are meant to be unique per page, id selectors are inherently meant for one-off, page-specific targeting rather than reusable styling, and they carry considerably higher specificity than a class selector, which makes them noticeably harder to override later from elsewhere in a stylesheet. IDs also serve non-styling purposes, like being the target of an in-page anchor link (#section) or a target for JavaScript's getElementById(), which is one reason many style guides recommend reserving id attributes for those functional purposes and using classes for styling instead.

💡

Prefer classes over ID selectors for styling in most cases — an ID selector's high specificity makes it considerably harder to override later, and since an id is meant to be unique, it structurally can't be reused across multiple elements the way a class can.

editor.html
#main-header {
  background: navy;
  color: white;
}
localhost:3000

2Practical Example

Here is a real-world application of ID Selectors showing how it is used in production CSS code.

editor.html
#main-header { color: white; }
.header { color: black; }

<header id="main-header" class="header">Title</header>
localhost:3000

3Best Practices

Follow these guidelines when working with ID Selectors:

1. Reserve ID selectors mainly for genuinely unique, page-specific elements, or avoid using them for styling altogether in favor of classes

2. Remember an id's high specificity makes it hard to override later — a single ID selector can require an equally- or more-specific selector, or !important, to override

3. Use id attributes for their non-styling purposes, like anchor-link targets and JavaScript's getElementById(), even in codebases that otherwise avoid ID selectors for styling

⚠️

Tip: Prefer classes over ID selectors for styling in most cases — an ID selector's high specificity makes it considerably harder to override later, and since an id is meant to be unique, it structurally can't be reused across multiple elements the way a class can.

editor.html
#main-header {
  background: navy;
  color: white;
}
localhost:3000

Examples

Example 01Basic Usage
#main-header {
  background: navy;
  color: white;
}
Example 02Advanced Example
#main-header { color: white; }
.header { color: black; }

<header id="main-header" class="header">Title</header>

Best Practices

  • Reserve ID selectors mainly for genuinely unique, page-specific elements, or avoid using them for styling altogether in favor of classes
  • Remember an id's high specificity makes it hard to override later — a single ID selector can require an equally- or more-specific selector, or !important, to override
  • Use id attributes for their non-styling purposes, like anchor-link targets and JavaScript's getElementById(), even in codebases that otherwise avoid ID selectors for styling

Interview Question

Why is overriding a style set by an ID selector generally harder than overriding one set by a class selector?

Hint: Think about how specificity is calculated for each selector type, and what it takes for a competing rule to actually beat a higher-specificity one.

CSS specificity is calculated as a weighted score based on the types of selectors used in a rule, and an ID selector is specifically weighted higher than a class selector, meaning any rule containing an ID selector automatically outranks a competing rule that relies only on class selectors for that same element, no matter how many class selectors that competing rule chains together, since class selectors alone can never mathematically reach an ID selector's specificity tier through sheer repetition of classes without also using an ID or !important. This means overriding an ID-selector rule later requires either another selector that also includes an ID, effectively matching or exceeding its specificity tier, or the blunt, generally discouraged !important escape hatch, both of which are more awkward than simply writing a normal class-based override further down the stylesheet, the approach that works fine when the original rule was class-based instead. This structural difficulty in overriding is exactly the practical reason many style guides recommend avoiding ID selectors for styling in the first place.

Exercises

MediumPractice using ID Selectors in a real scenario.
View Solution
#main-header {
  background: navy;
  color: white;
}

Frequently Asked Questions

Why is overriding a style set by an ID selector generally harder than overriding one set by a class selector?

CSS specificity is calculated as a weighted score based on the types of selectors used in a rule, and an ID selector is specifically weighted higher than a class selector, meaning any rule containing an ID selector automatically outranks a competing rule that relies only on class selectors for that same element, no matter how many class selectors that competing rule chains together, since class selectors alone can never mathematically reach an ID selector's specificity tier through sheer repetition of classes without also using an ID or !important. This means overriding an ID-selector rule later requires either another selector that also includes an ID, effectively matching or exceeding its specificity tier, or the blunt, generally discouraged !important escape hatch, both of which are more awkward than simply writing a normal class-based override further down the stylesheet, the approach that works fine when the original rule was class-based instead. This structural difficulty in overriding is exactly the practical reason many style guides recommend avoiding ID selectors for styling in the first place.

Related Functions

Class-SelectorsBasic-SelectorsCSS-Rules