A CSS rule has a selector and a block of declarations. The selector picks elements; each declaration sets one property, like color or font-size.
p {
color: navy;
}
.highlight {
background: yellow;
}
#main-title {
font-size: 2rem;
}Three basic selectors
pselects every paragraph (a type selector)..highlightselects elements withclass="highlight". Many elements can share a class.#main-titleselects the one element withid="main-title". An id must be unique on the page.
Prefer classes for styling. They are reusable and easier to manage than ids.
Code examples
<style>
.note { color: green; }
</style>
<p class="note">Saved!</p>
<p>Normal text</p>Expected output
"Saved!" appears in green; "Normal text" keeps the default color.
Common mistakes
Common mistake: Forgetting the dot for classes
Write .note in CSS for class="note". Without the dot, CSS looks for a <note> element instead.