Home CSS Selectors #id CSS ID Selector (#id) Definition
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.
Syntax
CSS Syntax The value must be a valid CSS identifier (or escaped). It must match the HTML id exactly, including case.
Patterns
Selector Patterns Pattern Description Try it #heroElement with id="hero" Try itdiv#boxA div that also has that id Try it#nav aLinks inside the element with id="nav" Try it#a, #bGroup two unique ids in one rule Try it#id vs .classUnique target vs reusable style Try it[id="demo"]Attribute form — same match, lower specificity Try it
Approach
How to Use #id Add a unique id in HTML, then select it with # in CSS.
Give one element a unique id attribute (letters preferred). Write #that-id { … } in your stylesheet. Use ids for landmarks and anchors; use classes for shared design. Avoid starting ids with digits unless you plan to escape them in CSS. Piece Required? Role HTML id Yes Unique name on the element # + nameYes Selects that exact id value Valid identifier Yes (or escaped) Must parse as a CSS id selector Uniqueness Yes in HTML One id value per document
Try it
Live Preview Switch which id rule is active and watch only that unique element update.
Code
CSS Examples Each snippet focuses on the #id selector. Use Try It Yourself for a live editor.
1. Basic #id 2. type#id 3. #id vs .class Example 1 — Style One Unique Element Match the element with id="blue".
#blue {
background-color: skyblue;
} This paragraph has id="blue"
Regular paragraph
How It Works Set the HTML id. One element uses id="blue".Select with #. #blue matches that exact value.Apply styles. Only that paragraph gets the skyblue background.Leave others. Elements without that id stay unchanged.Example 2 — Combine Type and ID Require both a tag name and an id.
div#box {
border: 2px solid #2563eb;
padding: 0.75rem;
} div#box matches
A paragraph without that id
div#box requires both the tag div and id="box".
How It Works Write type then id. div#box means a div with id="box".Raise clarity. Useful when documenting intent in a large stylesheet.Still unique. The id itself should appear once in the HTML.Prefer simple #box. The type is optional when the id is unique.Example 3 — #id vs .class One unique target versus many shared ones.
#banner {
background: #dbeafe;
}
.note {
background: #fef3c7;
} #banner (one)
.note
.note again
Id styles one landmark; class styles every matching element.
How It Works Use #banner once. Only the unique banner matches.Reuse .note. Every element with that class shares the style.Pick by intent. Landmark → id; repeated design → class.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 specificityIt can select the same element, but the attribute form is weaker than #x. See the attribute selector .
Compatibility
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).
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.
Remember
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 What does the CSS ID selector do? It matches the one element whose id attribute equals the name after #. Example: #hero matches <div id="hero">.
Must HTML ids be unique? Yes. In a valid HTML document each id value should appear once. Duplicate ids break uniqueness for CSS, links, and scripts.
How does #id compare to .class? #id is for one unique element and has higher specificity. Prefer .class for reusable styles across many elements.
Is #demo the same as [id="demo"]? They match the same element, but #demo has higher specificity than the attribute selector [id="demo"].
Can an id start with a number? 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.
Is the ID selector a runtime API? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion