Getting Started Lesson 4 of 33
CSS Selectors — Element, Class, Id, Group and Descendant
Learn how to pick exactly the elements you want to style using element, class, id, universal, group, descendant and child selectors.
The selector is the first part of every CSS rule. It answers the question "which elements should this style apply to?" Good selectors let you style one button, all buttons, or only the buttons inside the menu, without touching anything else.
Element selector
Write the name of an HTML element to select every element of that type.
p { color: #333; } /* every paragraph */
h1 { font-size: 32px; } /* every h1 */
Class selector: a dot
A class selector starts with a dot . and selects every element with that class. This is the selector you'll use most.
<style>
.sale {
color: white;
background-color: #CE1126;
padding: 2px 8px;
border-radius: 6px;
}
</style>
<h3>Ukay-ukay Price List</h3>
<p>Denim jacket – ₱250 <span class="sale">SALE</span></p>
<p>Polo shirt – ₱120</p>
<p>Hoodie – ₱300 <span class="sale">SALE</span></p>
Any element can use the class: a span, a p, a div. One class can be reused as many times as you like, which is why classes are so useful.
Multiple classes
An element can have several classes, separated by spaces in the HTML. Each class adds its own styles:
<style>
.btn { padding: 10px 16px; border: none; border-radius: 8px; font-size: 16px; }
.green { background: #1d7a43; color: white; }
.red { background: #b4372b; color: white; }
.big { font-size: 22px; padding: 14px 24px; }
</style>
<button class="btn green">Save</button>
<button class="btn red">Delete</button>
<button class="btn green big">Submit Project</button>
Id selector: a hash
An id selector starts with # and selects the one element with that id. An id must be unique on the page.
<style>
#main-title {
color: #0f5c4d;
text-transform: uppercase;
letter-spacing: 2px;
}
</style>
<h1 id="main-title">Science Fair 2026</h1>
<h2>Grade 9 Entries</h2>
Class or id?
Prefer classes for styling. They're reusable, and they're easier to override later. Use ids mainly for links within a page (href="#contact") and for JavaScript. Many professional teams don't style with ids at all.
Element with class
Put an element name and a class together (no space) to select only that element type with that class:
p.note { font-style: italic; } /* only <p class="note">, not <div class="note"> */
Universal selector
The star * selects every element. It's mostly used for page-wide resets:
* {
box-sizing: border-box;
}
You'll understand that particular line in the Box Model lesson.
Grouping selector
Separate selectors with commas to give them the same style:
h1, h2, .title { font-family: Georgia, serif; }
Descendant selector: a space
A space between two selectors means "inside". nav a selects every link that is somewhere inside a nav, but not other links on the page.
<style>
nav a {
color: white;
background: #0038A8;
padding: 6px 12px;
text-decoration: none;
border-radius: 6px;
}
</style>
<nav>
<a href="#">Home</a>
<a href="#">Lessons</a>
<a href="#">Contact</a>
</nav>
<p>This <a href="#">normal link</a> in a paragraph is not styled, because it's not inside the nav.</p>
Child selector: >
ul > li selects li elements that are direct children of a ul, not ones nested deeper. The difference matters with nested lists:
<style>
.menu > li { font-weight: bold; color: #0f5c4d; }
</style>
<ul class="menu">
<li>Silog meals
<ul>
<li>Tapsilog</li>
<li>Tocilog</li>
</ul>
</li>
<li>Drinks</li>
</ul>
Only the top-level items ("Silog meals", "Drinks") are bold green. The nested items are grandchildren, so > skips them. Change > to a space and see the difference.
Attribute selector
Square brackets select elements by attribute. They're useful for forms:
input[type="text"] { border: 2px solid #0f5c4d; }
a[target="_blank"] { color: purple; }
Summary table
| Selector | Example | Selects |
|---|---|---|
| Element | p | All paragraphs |
| Class | .sale | All elements with class="sale" |
| Id | #logo | The element with id="logo" |
| Universal | * | Everything |
| Group | h1, h2 | All h1 and all h2 |
| Descendant | nav a | Links anywhere inside nav |
| Child | ul > li | li directly inside ul |
| Attribute | input[type="email"] | Email inputs |
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04