Sub topic 2
Type, Class, and ID Selectors
This chapter covers the three basic selectors that nearly every stylesheet is built on. It assumes you know basic HTML elements and attributes, how to link a stylesheet, and simple properties such as color and margin. You should also recognize the parts of a rule: a selector, then a block of declarations in braces.
The three basic selectors
A selector decides which elements a rule applies to. The three basic kinds match different things in the HTML.
| Selector | Matches | Syntax | Example |
|---|---|---|---|
| Type | Elements by tag name | `name` | `p` |
| Class | Elements whose `class` attribute contains the name | `.name` | `.note` |
| ID | The element whose `id` attribute equals the name | `#name` | `#main` |
Here are all three in one stylesheet:
p { margin: 0; }
.note { color: #555; }
#main { max-width: 60rem; }They match this HTML:
<main id="main">
<p class="note">Remember to save your work.</p>
<p>A plain paragraph.</p>
</main>p { margin: 0; }matches both paragraphs, because both are<p>elements..note { color: #555; }matches only the first paragraph, because only it hasclass="note".#main { max-width: 60rem; }matches the<main>element, because itsidismain.
Type selectors
A type selector matches every element of that tag name. It is useful for broad defaults, such as setting the margin on all paragraphs or the font on body. Because it matches so widely, it is also the weakest of the three when rules conflict.
Class selectors
A class selector begins with a dot and matches any element that lists that class. An element can have several classes separated by spaces, and the same class can appear on many elements:
<p class="note warning">Disk almost full.</p>This paragraph matches both .note and .warning. You can also chain classes with no space to require both: .note.warning matches only elements that have both classes. Classes are the usual tool for styling reusable pieces of a page.
ID selectors
An ID selector begins with a hash and matches the one element whose id equals that name. An id should be unique in a document, so an ID selector is meant to target a single element. IDs are also used for fragment links (href="#main") and for scripting, which is one reason to avoid reusing them for styling.
How they rank
When two rules set the same property on the same element, the browser compares selectors. The ranking by default is ID, then class, then type. One ID selector beats any number of class selectors, and one class beats any number of type selectors. The next chapters turn this into a precise calculation.
p { color: black; }
.note { color: green; }
#intro { color: purple; }For <p id="intro" class="note">, the text is purple. The ID rule wins even though it appears last only by coincidence. Reorder the three rules and the result stays the same.
Common errors
- Missing prefix.
note { ... }is a type selector for a<note>element, which does not exist. Write.note. - Space inside a compound.
p .notemeans a.noteinside a<p>.p.notemeans a<p>that has the classnote. Spacing changes the meaning, as the combinators chapter explains. - Duplicate IDs. Repeating an
idis invalid HTML. CSS may still style every match, but scripts and links will only find one. - Case. Tag names are case-insensitive in HTML, but class and ID names are case-sensitive (in standards mode).
.Notedoes not matchclass="note".
Exercise
An element is <h2 id="title" class="heading">. The stylesheet has h2 { color: blue; }, #title { color: red; }, and .heading { color: green; }, in that order. What color is the text, and why?
Show answer
Red. The ID selector #title outranks the class selector .heading, which outranks the type selector h2. Source order does not matter here because the selectors have different ranks.
Exercise
What is the difference between p.note and p .note?
Show answer
p.note matches a <p> element that itself has the class note. p .note matches any element with class note that is a descendant of a <p>.