CSS Selector

A CSS selector is the pattern that tells CSS which HTML elements a rule should apply to. Selectors can target elements by tag name, class, id, attribute, or relationship.

Web

The most common selectors are the element selector (p), the class selector (.btn), and the id selector (#header). You can also combine them to target elements precisely.

Selectors are the first half of every CSS rule; the part in curly braces that follows defines the styles to apply.

Examples

CSS

p { color: gray; }
.highlight { background: yellow; }
#main { padding: 20px; }

Output:

Styles all paragraphs gray, elements with class 'highlight' yellow, and the #main element with padding.

Learn it in a lesson

See also

Back to the Dictionary