The Precision Tool: CSS ID Selectors

Master the powerful ID selector to target unique elements, understand specificity, and create robust page structures.

Lesson ProgressStep 1 of 8

My Website

This is a paragraph.

0 EXP

Welcome! Let's learn to style *one* specific element using a CSS ID selector.

/* Let's target a unique element! */

ID Selector Syntax (#)

To select an element by its ID, you use the **hash (`#`) symbol** followed by the ID's name.

/* This CSS selects the HTML element <div id="container"> */
#container {
  width: 90%;
}

This is different from a class selector, which uses a dot (`.`), or a tag selector, which uses no symbol.

System Check

What symbol is used to begin a CSS ID selector?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🏆
Hash Slinger

Correctly use the '#' syntax to target an ID.

🎯
Specificity Sniper

Understand the high specificity and uniqueness of an ID.

🎨
Unique Stylist

Apply a unique style to an element using its ID.

Mission: Style the Logo

Write a CSS rule to select the element with the ID `main-logo`. Give it a `color` of `red` and a `font-size` of `24px`.

A.D.A. Feedback:

> Awaiting input...

Challenge: Construct the Rule

Drag the parts into the correct order to form a valid CSS rule.

{ color: blue; }
#header-title

Challenge: Complete the Syntax

Fill in the missing characters to complete the CSS ID rule.

footerbackground-color: #333;

Consult A.D.A.

Unlock with Premium

Community Holo-Net

Peer Project Review

Submit your "Styled Logo" project for feedback from other Net-Runners.

The Precision Tool: A Deep Dive into CSS ID Selectors

In the world of CSS, selectors are your tools for targeting HTML elements. While tag selectors (like `p`) are broad and class selectors (like `.button`) are reusable, the **ID selector** is your precision instrument. It's designed to target **one, and only one, specific element** on your page.

This unique nature makes IDs incredibly powerful but also means they must be used with care. Understanding when—and when not—to use an ID is a critical skill for writing clean, maintainable, and scalable CSS.

1. Syntax and The Rule of Uniqueness

The syntax for an ID selector is the **hash symbol (`#`)** followed by the ID name. This name must exactly match the `id` attribute in your HTML.


<div id="main-header">...</div>

/* CSS */
#main-header {
  background-color: #333;
  color: white;
}

The most important rule of `id` attributes is that they **must be unique** within an HTML document. You cannot have two elements with `id="main-header"`. While a browser might still apply the style if you break this rule, it is invalid HTML and will cause unpredictable bugs, especially with JavaScript.

2. The King of Specificity

CSS uses a concept called **specificity** to decide which style rule applies if multiple rules target the same element. ID selectors have the highest specificity among common selectors.

Think of specificity as a score (ID - Class - Element):

  • ID Selector (`#my-id`): Score (1-0-0)
  • Class Selector (`.my-class`): Score (0-1-0)
  • Element Selector (`div`): Score (0-0-1)

An ID's score (1-0-0) will always beat any combination of classes and elements.

Example: Specificity


<h1 id="page-title" class="important-text">Hello</h1>

/* CSS */
#page-title { color: blue; }   /* (1-0-0) - WINS! */
.important-text { color: red; }  /* (0-1-0) */
h1 { color: green; }           /* (0-0-1) */

The text will be **blue** because the ID selector is the most specific.

3. When to Use ID Selectors (The Use Cases)

Given their high specificity, you should use IDs sparingly. Their best use cases are:

  • JavaScript Hooks: This is the most common and recommended use. `document.getElementById('element-id')` is the fastest and most reliable way for JavaScript to find and manipulate a single element.
  • Anchor Links: IDs are used to create "in-page" links. An `<a href="#section-2">` will scroll the page directly to the element with `id="section-2"`.
  • Page Structure: Styling unique, high-level structural elements like `<header id="main-nav">`, `<main id="content">`, or `<footer id="page-footer">`.

4. When *Not* to Use IDs (The Anti-Patterns)

Using IDs incorrectly can lead to a messy and unmaintainable codebase. Avoid these patterns:

  • Never use IDs for reusable styles. If you want to make 10 buttons on your page look the same, you need a **class** (e.g., `.btn-primary`), not an ID.
  • Avoid "Specificity Wars". If you use IDs to style everything, you'll soon find yourself needing to override an ID style. The only way to beat an ID is with *another ID* or the dreaded `!important` keyword. This makes your CSS fragile and hard to debug.
Key Takeaway: Use **Classes** for reusable styles (the 90% case). Use **IDs** for unique exceptions: JavaScript hooks, page anchors, and styling main structural blocks.

CSS Selectors & Specificity Glossary

Selector
A CSS pattern that "selects" the HTML element(s) you want to style. Examples include `p`, `.my-class`, and `#my-id`.
ID Selector
Selects a single, unique element based on its `id` attribute. The syntax is a hash symbol (`#`) followed by the ID name (e.g., `#main-nav`).
`id` (Attribute)
An HTML attribute that defines a unique identifier for an element. Its value must be unique across the entire document.
Class Selector
Selects all elements that have a given `class` attribute. The syntax is a period (`.`) followed by the class name (e.g., `.button`). A class can be reused on many elements.
Type Selector
Selects all elements of a given HTML tag name (e.g., `h1`, `p`, `div`). This is also known as a Tag Selector.
Specificity
The set of rules a browser uses to determine which CSS style declaration is applied to an element when multiple, conflicting rules exist.
Specificity Hierarchy
The order of "power" for selectors. From highest to lowest: Inline Styles > ID Selectors > Class Selectors > Type (Tag) Selectors.
Uniqueness
The core rule for IDs. An `id` attribute's value must be used only once per HTML page.
JavaScript Hook
A common term for using an ID (or class) specifically to allow JavaScript to find an element, often using `document.getElementById()`.
Anchor Link
An HTML link (`<a>`) with an `href` attribute that starts with a `#`. When clicked, it scrolls the page to the element with the corresponding `id`.

Credibility and Trust

About the Author

Author's Avatar

Todotutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching CSS and building robust and accessible web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest CSS specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!