e01.org

Selector (CSS)

A CSS selector is the part of a CSS rule that describes what elements in a document the rule will match. The matching elements will have the rule's specified style applied to them.

A **CSS selector** is the part of a CSS rule that describes what elements in a document the rule will match. The matching elements will have the rule's specified style applied to them.

## Example

Consider this CSS:

```css
p {
color: green;
}

div.warning {
width: 100%;
border: 2px solid yellow;
color: white;
background-color: darkred;
padding: 0.8em 0.8em 0.6em;
}

#customized {
font:
16px "Lucida Grande",
"Helvetica",
"Arial",
sans-serif;
}
```

The selectors here are `"p"` (which applies the color green to the text inside any p element), `"div.warning"` (which makes any div element with the [class](/docs/Web/HTML/Reference/Global_attributes/class) `"warning"` look like a warning box), and `"#customized"`, which sets the base font of the element with the ID `"customized"` to 16-pixel tall Lucida Grande or one of a few fallback fonts.

We can then apply this CSS to some HTML, such as:

```html
<p>This is happy text.</p>

<div class="warning">
Be careful! There are wizards present, and they are quick to anger!
</div>

<div id="customized">
<p>This is happy text.</p>

<div class="warning">
Be careful! There are wizards present, and they are quick to anger!
</div>
</div>
```

The resulting page content is styled like this:

640

## See also

- [Learn more about CSS selectors](/docs/Learn_web_development/Core/Styling_basics/Basic_selectors) in our introduction to CSS.
- Basic selectors
- [Type selectors](/docs/Web/CSS/Reference/Selectors/Type_selectors) `elementname`
- [Class selectors](/docs/Web/CSS/Reference/Selectors/Class_selectors) `.classname`
- [ID selectors](/docs/Web/CSS/Reference/Selectors/ID_selectors) `#idname`
- [Universal selectors](/docs/Web/CSS/Reference/Selectors/Universal_selectors) `* ns|* *|*`
- [Attribute selectors](/docs/Web/CSS/Reference/Selectors/Attribute_selectors) `[attr=value]`
- [State selectors](/docs/Web/CSS/Reference/Selectors/Pseudo-classes) `a:active, a:visited`

- Grouping selectors
- [Selector list](/docs/Web/CSS/Reference/Selectors/Selector_list) `A, B`

- Combinators
- [Next-sibling selectors](/docs/Web/CSS/Reference/Selectors/Next-sibling_combinator) `A + B`
- [Subsequent-sibling selectors](/docs/Web/CSS/Reference/Selectors/Subsequent-sibling_combinator) `A ~ B`
- [Child selectors](/docs/Web/CSS/Reference/Selectors/Child_combinator) `A > B`
- [Descendant selectors](/docs/Web/CSS/Reference/Selectors/Descendant_combinator) `A B`

- Pseudo
- [Pseudo classes](/docs/Web/CSS/Reference/Selectors/Pseudo-classes) `:`
- [Pseudo elements](/docs/Web/CSS/Reference/Selectors/Pseudo-elements) `::`

source: MDN Web Docs mirror · page

Cite this

"Selector (CSS)". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Glossary/CSS_Selector

JSON record · API collection · citation policy