The #id selector styles a single element by its unique id attribute. It is one of the most specific basic selectors in CSS.
01
# prefix
Hash + id name.
02
Unique
One per page.
03
Specificity
Beats .class.
04
Landmarks
Header, main.
05
vs .class
When to use each.
06
Hooks
JS & anchors.
Fundamentals
Introduction
The CSS #id selector is used to style an element based on its unique id attribute. IDs are meant to be unique within a webpage, and this selector lets you target one specific element by its id value.
This provides a precise way to apply styles to a single element, distinguishing it from other elements that may share the same tag or class.
Definition and Usage
In CSS, prefix the id name with a hash (#): #main-title, #header, or #highlight. The HTML element must have a matching id attribute: id="main-title".
💡
Beginner Tip
Prefer .class for styles you want to reuse on many elements. Reserve #id for one-of-a-kind sections like #site-header or a single highlighted paragraph.
Foundation
📝 Syntax
The syntax for the #id selector is:
syntax.css
#elementID{/* CSS properties */}
#elementID refers to the id of the HTML element you want to style.
In CSS, the id selector is prefixed with # followed by the id name (no quotes).
In HTML, the id is set with id="elementID" on the opening tag.
Each id must be unique on the page — never duplicate it.
Id names are case-sensitive: #Header and #header are different.
Combine with elements: div#wrapper targets a div with that id.
Ids cannot contain spaces; use hyphens for multi-word names: main-content.
Do not include the # in the HTML id attribute — only in CSS.
Related Selectors
.class — targets multiple elements sharing a class name
element — targets all elements of a tag type
[attribute] — targets elements with a given attribute
#id .class — descendant: class inside a specific id container
#id > element — direct child inside an id container
Cheat Sheet
⚡ Quick Reference
Question
Answer
Selector type
Basic selector (id)
CSS syntax
#idName
HTML attribute
id="idName"
Uniqueness
One id per page (per document)
Specificity score
100 (1,0,0) — beats classes and elements
Browser support
All browsers
Context
When to Use #id
#id is best for unique, one-of-a-kind elements:
Page landmarks — Style #header, #footer, or #main-content once per layout.
Single highlighted block — Emphasize one special paragraph or callout with #highlight.
Anchor targets — Link to a section with <a href="#section-id"> and style that section.
JavaScript hooks — When scripts need a reliable single-element reference.
Unique widgets — One search box, one hero banner, or one modal container.
Preview
👀 Live Preview
The heading uses #id-demo-title and the highlighted paragraph uses #id-demo-highlight:
Welcome to My Blog
This is a regular paragraph about the blog.
This paragraph is uniquely styled with an id.
Hands-On
Examples Gallery
Practice #id with page titles, highlighted text, unique headers, and id vs class specificity.
📜 Core Patterns
Target unique elements with the hash selector.
Example 1 — Page title and highlighted paragraph
Style a main heading and one special paragraph using their unique ids.
id-blog.html
<style>#main-title{font-size:2rem;color:#1e3a8a;text-align:center;}#highlight{background:#fef9c3;font-weight:700;padding:0.5rem;}</style><h1id="main-title">Welcome to My Blog</h1><p>This is a paragraph about the blog.</p><pid="highlight">This paragraph is uniquely styled.</p>
The id="email" attribute uniquely identifies this input. The #email selector styles only that field, leaving other inputs unchanged.
Tips
💬 Usage Tips
Uniqueness — Never assign the same id to multiple elements; it breaks CSS, accessibility APIs, and JavaScript lookups.
High specificity — #id overrides .class and element selectors. Use this power deliberately, not excessively.
Reserve for unique cases — Use ids for #header, #footer, or anchor targets; use classes for reusable styles.
Meaningful names — Choose descriptive, hyphenated ids like main-content instead of vague names like div1.
Label association — Pair form field ids with <label for="id"> for accessibility.
Watch Out
⚠️ Common Pitfalls
Duplicate ids — Multiple elements with the same id produce invalid HTML and unpredictable styling.
Overusing ids — Styling everything with ids makes CSS rigid and hard to maintain; prefer classes for shared styles.
Specificity wars — Heavy id usage forces you to add more ids or !important to override styles later.
Hash in HTML — Write id="header" in HTML and #header in CSS — do not put # in the attribute.
Starting with a digit — Id names should start with a letter; avoid id="1section".
A11y
♿ Accessibility
Skip links — Use id="main-content" as a target for “Skip to content” navigation links.
Form labels — Connect <label for="email"> to <input id="email"> so screen readers announce the field name.
ARIA vs id — Ids hook labels and ARIA references (aria-labelledby); do not rely on id alone for semantics.
Landmark regions — Prefer semantic tags (<header>, <main>) plus ids only when needed for navigation.
Unique requirement — Duplicate ids break assistive technology that references elements by id.
🧠 How #id Works
1
HTML assigns a unique id
One element gets id="main-title" in the markup.
HTML
2
CSS targets with #
The rule #main-title { } matches that exact element.
Match
3
Styles apply to one element
Font, color, layout, or other properties affect only that node.
Style
=
🎯
Precise, unique styling
One selector, one element, high specificity.
Compatibility
🖥 Browser Compatibility
The #id selector is supported in all browsers, including very old versions.
✓ Baseline · Universal support
ID selectors everywhere
#id has worked since the earliest CSS implementations and is reliable on every platform.
99%Universal support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll versions
Full support
#id selector99% supported
Bottom line:#id is one of the safest, most widely supported selectors in CSS.
Wrap Up
🎉 Conclusion
The CSS #id selector is an essential tool for targeting and styling unique elements on a webpage. Its high specificity makes it powerful, but it should be used with care to keep your CSS clean and maintainable.
Use ids for one-of-a-kind landmarks and hooks, classes for reusable styles, and always keep each id unique per document.
The #id selector targets a single HTML element by its unique id attribute value. You write a hash (#) followed by the id name, such as #main-title or #header.
No. The id attribute must be unique within a document. Duplicate ids cause invalid HTML and unpredictable CSS and JavaScript behavior.
An id identifies one unique element; a class can be shared by many elements. In CSS, #id has higher specificity than .class.
Use an id for a single, unique landmark or hook — such as #main-content, #site-header, or an anchor target. Use classes for reusable styles shared across multiple elements.
Yes. The id selector has been part of CSS since the earliest versions and works in every modern and legacy browser.