CSS IDs /// SPECIFICITY /// UNIQUE DOM NODES /// FRAGMENT LINKS /// CSS IDs /// SPECIFICITY /// UNIQUE DOM NODES /// FRAGMENT LINKS ///

CSS IDs

The heavy hitters of CSS. Discover the power of completely unique selectors, overrides, and document fragment linking.

style.css
1 / 10
12345
🆔

Tutor:Every element on a web page can have a unique name tag. In HTML, we call this an 'id' attribute.


Skill Matrix

UNLOCK NODES BY MASTERING SELECTORS.

Syntax & Targeting

You define an ID in HTML with id="name" and target it in CSS using #name.

System Check

What is the correct CSS syntax to target <div id='nav'>?


Community Holo-Net

Discuss Specificity Architecture

ACTIVE

Having trouble overriding classes? Share your code and get feedback from peers!

CSS IDs: The Unique Identifiers

Frontend Instructor in CodeSylllabus

Pascual Vila

Frontend Instructor // Code Syllabus

While classes bring modularity and reuse to your styles, IDs bring precision. They are the single source of truth for an element, acting as styling hooks, navigation anchors, and direct pipelines to JavaScript logic.

The Golden Rule: Uniqueness

The most important rule when using the id attribute in HTML is that it must be absolutely unique within a document. You cannot have two elements on the same webpage with id="header".

In CSS, you target this unique element using the hash or pound symbol (#). Because the browser knows only one element exists with this ID, it can locate and style it incredibly fast.

Specificity Showdown

CSS applies rules based on a weight system known as Specificity. If two rules clash, the heavier rule wins.

An ID selector carries significantly more weight than a class selector. If an element has class="btn-red" but also id="btn-green", and both apply a background color, the ID's style will overwhelmingly defeat the class's style.

Beyond Styling: Anchors & JS

  • Fragment Links (Anchors): If you assign an ID to a section (e.g., id="contact"), you can link directly to it from anywhere using an anchor tag: <a href="#contact">. Clicking this link scrolls the browser directly to that element.
  • JavaScript Targeting: IDs are heavily used in JS via document.getElementById('my-id'). It is the most performant way to select a single DOM node.
Best Practices tip+

Limit IDs in CSS. Because of their massive specificity weight, heavily styling with IDs makes your CSS rigid and hard to override later. It is widely considered best practice to use Classes (.class) for styling, and reserve IDs for JavaScript hooks and page anchors.

Frequently Asked Questions

What happens if I use the same ID twice?

It invalidates your HTML document. While CSS *might* still apply the styles to both elements in modern browsers, JavaScript will fail (getElementById will only return the first occurrence), and fragment links will behave unpredictably.

How do I override an ID in CSS?

It is very difficult. You either have to use another ID with a class chained to it (e.g., #my-id.override), use an inline style in the HTML, or use the dreaded !important flag. This is why styling with classes is preferred!

ID Glossary

ID Selector (#)
A CSS selector that targets an element based on the value of its id attribute.
snippet.css
Class vs ID
Classes (.) can be reused on multiple elements. IDs (#) must be entirely unique per HTML document.
snippet.css
Specificity
The weight system CSS uses to resolve conflicts. An ID has a weight of 100, while a class has a weight of 10.
snippet.css
Fragment Identifier
Using an anchor tag with an href containing a hash (#) to jump to the matching ID on the same page.
snippet.css
getElementById
A core JavaScript DOM method that quickly retrieves the unique HTML element matching a specific ID.
snippet.css
Naming Rules
IDs should not start with a number. They should use letters, numbers, hyphens (-), and underscores (_).
snippet.css