Selectors — class and id
Halaman ini memerlukan browser terbaru (dengan dukungan SharedArrayBuffer). Silakan perbarui Chrome, Edge, Firefox, atau Safari ke versi terbaru.
English
Three kinds of selector
- A selector says which elements a rule changes.
- By tag:
p { ... }changes every<p>. - That is the kind you used last lesson.

English
By class and by id
- By class:
.note { ... }changes every element withclass="note". A dot.means class. - By id:
#title { ... }changes the one element withid="title". A hash#means id. - A class can be on many elements; an id should be on one.
English
<style>
.note { color: green; }
#title { color: purple; }
</style>
<h1 id="title">My page</h1>
<p class="note">This is a note.</p>
<p>This is normal.</p>
English
Common mistakes
- A class selector starts with
., an id selector with#. - Reuse a class on many elements; an id appears once per page.
English
Now you try
- Match the dot
.to a class, and the hash#to an id. - Style a class, then style an id.
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
Style the note. Add a rule for the note class: .note { color: green; } (a dot means class).
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
Style the title. Add a rule for the title id: #title { color: purple; } (a hash means id).
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
A class can be shared. Give keduanya tip paragraphs class="tip", then add one rule .tip { color: orange; } that styles them together.