Imagine you are building the menu page for Mrs. Tini's stall. You want the ordinary dishes in black text, the dishes on promo in red, and one special dish that looks different from the rest. Writing the CSS for the colours is easy. The harder part: how do you tell the browser which dishes are on promo and which one is special?
That is the selector's job. The selector is the part of a CSS rule that picks which elements the rule is for. There are three basic selectors: picking by tag name, by class and by id.
A rule that picks every paragraph
Here is the menu page of Mrs. Tini's stall. At the bottom there is one CSS rule, inside a <style> tag:
<h2>Mrs. Tini's Stall Menu</h2>
<p class="promo">Fried rice</p>
<p id="special">Chicken satay</p>
<p class="promo">Sweet iced tea</p>
<div class="promo">Free crackers every Friday</div>
<style>
p {
color: red;
}
</style>
pis the selector. Here the selector is a tag name, so the rule applies to every<p>on the page.{and}(curly braces) hold the declarations for that selector.color: red;is the declaration: the propertycolor(text colour), a colon, the valuered, then a semicolon to end it.
The result: Fried rice, Chicken satay and Sweet iced tea turn red, because all three use the <p> tag. The heading and the <div> stay black, because their tags aren't p.
A rule with a selector goes inside a <style> tag. A style="..." attribute on a tag has no selector, because the attribute only applies to the tag it is written on.
Tag, class and id
How to write the three basic selectors:
- Tag: only the tag's name, without
<and>.h2picks every<h2>, andppicks every<p>. This is called an element selector. - Class: a dot
.then the class name..promopicks every element that hasclass="promo", whatever its tag. So<div class="promo">is picked too. One class can be used on many elements. - Id: a hash sign
#then the id name.#specialpicks the element that hasid="special". Only one element per page may use an id. If two elements share an id, CSS still styles both, but the HTML is not valid. A link to#specialjumps only to the first one, and JavaScript also finds only the first one.
The dot and the hash are only written in the CSS. In the HTML you still write class="promo" and id="special", with no symbol.
One pair that people often confuse: #header picks the element with id="header", not the <header> tag. To pick the <header> tag, write header with no hash.
⚠️ Why does the selector miss?
- Angle brackets in the selector.
<h2> { }isn't valid. Writeh2 { }. - A dot written in the HTML.
class=".promo"means the selector.promodoesn't match, because the class name now includes the dot. - The same id on two elements. If a style is for many elements, use a class.
- Different capital letters.
.Promodoesn't pickclass="promo". The browser also shows no error message when your selector matches no element, so check the spelling.
💡 When rules clash, the more specific one wins. One element can get a tag rule, a class rule and an id rule at once. If those rules set different colours, the id beats the class, and the class beats the tag. The Specificity lesson explains the details.
TL;DR: A selector is the part of a CSS rule that picks which elements the rule is for. The rule goes inside a <style> tag, in the form selector { property: value; }. There are three basic selectors. A tag name (p) picks every element with that tag. A dot then a class name (.promo) picks every element with that class. A hash then an id name (#special) picks the element with that id, and only one element may use an id. The dot and the hash are only written in the CSS, not in the HTML.
⚡ Pick every paragraph
Write:
p {
color: red;
}
Meaning: p is the selector, the part of the rule that picks which elements the rule is for. Here the selector is a tag name, written without < and >. This rule makes the text of every <p> on the page red. Elements with other tags, such as <h2> or <div>, stay black.
⚡ A dot for a class, a hash for an id
Write:
.promo {
color: red;
}
Meaning: A dot . then a class name picks every element that has class="promo", whatever its tag. So a paragraph and a <div> that have that class are both picked. A hash # then an id name, such as #special, picks the element that has id="special", and that id is for one element only.
⚡ The symbols are for CSS only
Guess first: which element does #header pick?
The element with id="header", not the <header> tag. To pick the <header> tag, write header with no hash.
Meaning: The dot and the hash are only written in the CSS. In the HTML you still write class="promo" and id="special", with no symbol. If the HTML says class=".promo", the selector .promo no longer matches that element.