CSS ID Selector (#id)

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The ID selector matches an element whose id attribute equals the name after #. Example: #demo styles <div id="demo">.

Use it for a unique landmark on the page (header, hero, main). For reusable styles, prefer a class selector — ids are unique and carry high specificity.

Baseline Widely available.

Core CSS selector, supported everywhere modern browsers run.

Warning — keep ids unique and rare in CSS.

HTML requires unique id values. Overusing #id for styling makes overrides hard because of high specificity. Prefer .class for shared looks.

CSS Syntax

syntax.css
# id_value { … }

The value must be a valid CSS identifier (or escaped). It must match the HTML id exactly, including case.

Selector Patterns

PatternDescriptionTry it
#heroElement with id="hero"Try it
div#boxA div that also has that idTry it
#nav aLinks inside the element with id="nav"Try it
#a, #bGroup two unique ids in one ruleTry it
#id vs .classUnique target vs reusable styleTry it
[id="demo"]Attribute form — same match, lower specificityTry it

How to Use #id

Add a unique id in HTML, then select it with # in CSS.

  1. Give one element a unique id attribute (letters preferred).
  2. Write #that-id { … } in your stylesheet.
  3. Use ids for landmarks and anchors; use classes for shared design.
  4. Avoid starting ids with digits unless you plan to escape them in CSS.
PieceRequired?Role
HTML idYesUnique name on the element
# + nameYesSelects that exact id value
Valid identifierYes (or escaped)Must parse as a CSS id selector
UniquenessYes in HTMLOne id value per document

Live Preview

Switch which id rule is active and watch only that unique element update.

Matched id targets get a blue border and tinted background.

ID preview
#hero
no id

Rule: #hero — only the hero box matches

CSS Examples

Each snippet focuses on the #id selector. Use Try It Yourself for a live editor.

Example 1 — Style One Unique Element

Match the element with id="blue".

CSS
#blue {
  background-color: skyblue;
}
Try It Yourself

How It Works

  1. Set the HTML id. One element uses id="blue".
  2. Select with #. #blue matches that exact value.
  3. Apply styles. Only that paragraph gets the skyblue background.
  4. Leave others. Elements without that id stay unchanged.

Example 2 — Combine Type and ID

Require both a tag name and an id.

CSS
div#box {
  border: 2px solid #2563eb;
  padding: 0.75rem;
}
Try It Yourself

How It Works

  1. Write type then id. div#box means a div with id="box".
  2. Raise clarity. Useful when documenting intent in a large stylesheet.
  3. Still unique. The id itself should appear once in the HTML.
  4. Prefer simple #box. The type is optional when the id is unique.

Example 3 — #id vs .class

One unique target versus many shared ones.

CSS
#banner {
  background: #dbeafe;
}

.note {
  background: #fef3c7;
}
Try It Yourself

How It Works

  1. Use #banner once. Only the unique banner matches.
  2. Reuse .note. Every element with that class shares the style.
  3. Pick by intent. Landmark → id; repeated design → class.
  4. Mind specificity. An id rule beats a class rule when both apply.

Edge Cases & Pitfalls

Most id bugs are duplicates, invalid identifiers, or specificity fights.

unique

Duplicate ids are invalid HTML

Browsers may style the first match only, or behave inconsistently. Keep each id value once per page.

escape

Special characters need escaping

Ids like item?one or 123item need CSS escapes (for example #item\?one). Prefer simple names.

specificity

High specificity is hard to override

An #id rule beats classes and elements. Overuse leads to !important wars — use classes for shared UI.

case

Matching is case-sensitive in CSS

#Hero and #hero are different selectors. Match the HTML id exactly.

vs attr

[id="x"] is not the same specificity

It can select the same element, but the attribute form is weaker than #x. See the attribute selector.

Browser Support

The ID selector is a browser CSS selector. Node.js, Deno, and Bun do not evaluate it.

✓ Baseline · Widely available

#id

Supported in all major browsers for decades (Baseline Widely available since about July 2015).

99% All modern browsers
Google Chrome All
Full
Mozilla Firefox All
Full
Apple Safari All
Full
Microsoft Edge All
Full
Opera All
Full
Samsung Internet All
Full
Internet Explorer All
Full
#id Universally supported

Bottom line: Production-ready. Prefer classes for reusable styles; reserve ids for unique landmarks. Not a server runtime API.

Key Takeaways

  • Role: select the element with a matching unique id.
  • Form: #name must match id="name" exactly.
  • vs class: unique + high specificity vs reusable styles.
  • Tip: keep ids unique; escape special characters or avoid them.

One line: #id styles the single element with that unique id.

Frequently Asked Questions

It matches the one element whose id attribute equals the name after #. Example: #hero matches <div id="hero">.
Yes. In a valid HTML document each id value should appear once. Duplicate ids break uniqueness for CSS, links, and scripts.
#id is for one unique element and has higher specificity. Prefer .class for reusable styles across many elements.
They match the same element, but #demo has higher specificity than the attribute selector [id="demo"].
HTML allows it, but in CSS you must escape it (for example #\31 23item for id="123item"). Prefer ids that are valid CSS identifiers without escaping.
No. It is a browser CSS selector, not a Node.js feature.

Next: Adjacent sibling (+)

Select an element only when it immediately follows another sibling.

+ combinator tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful