CSS #id Selector

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Basic Selector

What You’ll Learn

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.

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.

📝 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.

Basic Example

id-basic.css
#main-title {
  font-size: 2rem;
  color: #1e3a8a;
  text-align: center;
}

#highlight {
  background-color: #fef9c3;
  font-weight: 700;
  padding: 0.5rem;
}
#main-title #header #footer #main-content

ID vs Class vs Element

SelectorSyntaxMatchesSpecificity
ElementpAll matching tagsLow (0,0,1)
Class.noteAll elements with that classMedium (0,1,0)
ID#headerOne unique elementHigh (1,0,0)

Syntax Rules

  • 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

⚡ Quick Reference

QuestionAnswer
Selector typeBasic selector (id)
CSS syntax#idName
HTML attributeid="idName"
UniquenessOne id per page (per document)
Specificity score100 (1,0,0) — beats classes and elements
Browser supportAll browsers

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.

👀 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.

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>

<h1 id="main-title">Welcome to My Blog</h1>
<p>This is a paragraph about the blog.</p>
<p id="highlight">This paragraph is uniquely styled.</p>
Try It Yourself

How It Works

Only the element with id="main-title" gets the large blue centered heading, and only id="highlight" gets the yellow background.

Example 2 — Unique site header

Style a page header once using #site-header.

id-header.css
#site-header {
  background: #1e3a8a;
  color: #fff;
  padding: 1rem 1.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

#site-header h1 {
  margin: 0;
  font-size: 1.25rem;
}
Try It Yourself

How It Works

Because there is only one site header, #site-header is the right choice. Child elements like h1 inside it can be styled with #site-header h1.

📄 Specificity & Forms

See how ids interact with classes and target unique form fields.

Example 3 — ID overrides class

Demonstrate that #id beats .class when both target the same element.

id-specificity.css
.text {
  color: #64748b;
}

#special {
  color: #dc2626;
  font-weight: 700;
}
Try It Yourself

How It Works

Both paragraphs have class text, but the second also has id="special". The id rule has higher specificity, so its red color wins.

Example 4 — Unique form field styling

Target one input by id for custom width or validation styling.

id-form.css
#email {
  width: 100%;
  max-width: 20rem;
  padding: 0.5rem 0.75rem;
  border: 2px solid #2563eb;
  border-radius: 0.4rem;
  font-size: 0.875rem;
}

label[for="email"] {
  font-weight: 600;
  color: #1e3a8a;
}
Try It Yourself

How It Works

The id="email" attribute uniquely identifies this input. The #email selector styles only that field, leaving other inputs unchanged.

💬 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.

⚠️ 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".

♿ 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.

🖥 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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All versions
Full support
#id selector 99% supported

Bottom line: #id is one of the safest, most widely supported selectors in CSS.

🎉 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.

💡 Best Practices

✅ Do

  • Keep each id unique on the page
  • Use ids for landmarks and anchor targets
  • Pair form ids with matching label for attributes
  • Use hyphenated, descriptive id names
  • Prefer classes for reusable styling

❌ Don’t

  • Reuse the same id on multiple elements
  • Style everything with ids instead of classes
  • Put # inside the HTML id attribute
  • Create specificity battles with too many ids
  • Use vague ids like box1 or item

Key Takeaways

Knowledge Unlocked

Five things to remember about #id

Use these points when targeting unique elements.

5
Core concepts
1 02

One per page

Must be unique.

Rule
100 03

High specificity

Beats .class.

Power
. 04

vs .class

Unique vs shared.

Compare
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

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.

Practice in the Live Editor

Open the HTML editor and experiment with #id, unique elements, and id vs class specificity.

HTML Editor →

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.

5 people found this page helpful