GO-Ve01
ServicesDevelopersBooksDictionaryPapersWikidataEncyclopediaSearchBabelCareersDeck

ID selectors

The CSS ID selector matches an element based on the value of the element's id attribute. In order for the element to be selected, its id attribute must match exactly the value given in the selector.

The CSS **ID selector** matches an element based on the value of the element's [`id`](/docs/Web/HTML/Reference/Global_attributes/id) attribute. In order for the element to be selected, its `id` attribute must match exactly the value given in the selector.

```css
/* The element with id="demo" */
#demo {
border: red 2px solid;
}
```

## Syntax

```css
#id_value {
/* … */
}
```

Note that syntactically (but not specificity-wise), this is equivalent to the following [attribute selector](/docs/Web/CSS/Reference/Selectors/Attribute_selectors):

```css
[id="id_value"] {
/* … */
}
```

The `id_value` value must be a valid [CSS identifier](/docs/Web/CSS/Reference/Values/ident). HTML `id` attributes which are not valid CSS identifiers must be [escaped](/docs/Web/CSS/Reference/Values/ident#escaping_characters) before they can be used in ID selectors.

## Examples

### Valid ID selectors

#### HTML

```html
<p id="blue">This paragraph has a blue background.</p>
<p>This is just a regular paragraph.</p>
```

```html
<!-- The next two paragraphs have id attributes
that contain characters which must be escaped in CSS -->

<p id="item?one">This paragraph has a pink background.</p>
<p id="123item">This paragraph has a yellow background.</p>
```

#### CSS

```css
#blue {
background-color: skyblue;
}
```

```css
/* In the next two rules, the id attributes must be escaped */

#item\?one {
background-color: pink;
}

#\00003123item {
background-color: yellow;
}
```

#### Result

100%

### Invalid ID selectors

The ID selectors in the following rules are not valid CSS identifiers, and will be ignored.

```css example-bad
#item?one {
background-color: green;
}

#123item {
background-color: green;
}
```

## Specifications

## Browser compatibility

## See also

- [CSS Selectors](/docs/Web/CSS/Guides/Selectors)
- [Learn CSS: Basic selectors](/docs/Learn_web_development/Core/Styling_basics/Basic_selectors)

source: MDN Web Docs mirror · page

Cite this

"ID selectors". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/ID_selectors

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·