Skip to main content

How to Select an Element by ID in CSS

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

An ID selector styles the one element with a given id: a #, then the id. #message matches the element with id="message".

Syntax​

Syntax
#<id> {
<declarations>
}
PartWhat it does
#Marks an ID selector.
<id>The value of the element's id attribute, such as message, without quotes.
<declarations>The styles the element gets.

MDN's ID selectors page says the element's id must match the value in the selector exactly.

The Example​

The example is a sign-in page with two lines: a greeting and a warning that the password expires soon. One rule makes the warning red and bold.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a style element holding a rule for the id message that sets the color c50f1f and a bold font weight, a paragraph Welcome back. and a paragraph with the id message.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ID selector</title>
<style>
#message {
color: #c50f1f;
font-weight: bold;
}
</style>
</head>
<body>
<p>Welcome back.</p>
<p id="message">Your password expires in 3 days.</p>
</body>
</html>

#message matches the paragraph with id="message". Its declarations color it #c50f1f and make it bold.

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: Welcome back. in black, then Your password expires in 3 days. in bold red.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The warning is red and bold. Chromium gave it the color rgb(197, 15, 31), which is #c50f1f, and a font weight of 700.
  • Welcome back. kept the default look, because it has no id.

Common Mistakes​

MistakeWhat happensDo this instead
Changing the case: #Message for id="message"The id must match exactly, so nothing changedCopy the id exactly
Using an id that starts with a digit: #1st#1st isn't a valid selector, so Chromium dropped the whole ruleStart ids with a letter, or escape the digit: #\31 st

In #\31 st, \31 is the code of the character 1, and the space after it ends the code. #\31 st styled the element with id="1st" red.

Troubleshooting​

SymptomLikely causeFix
A rule such as div#message does nothingThe element with that id isn't a div: on a p with id="message", div#message didn't apply, but p#message didDrop the element name, or use the right one
A rule for an id is missing from the page's stylesThe id starts with a digit, so the selector is invalidEscape the digit, or rename the id

FAQ​

When should I use an id instead of a class?​

Use an id for one element. MDN's page on the id attribute says an id must be unique within the entire document. When several elements share a style, give them a class instead.

Does the element need anything else besides the id?​

No. The id attribute in the HTML and the # selector in the CSS are all it takes.

Conclusion​

Give the element an id, then select it with # and the id, exactly as it's written. Start ids with a letter, so the selector stays valid without escaping.

Additional Resources​