Aral Code

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.

Example Class selector
<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:

Example Combining classes
<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.

Example Id selector
<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.

Example Descendant selector
<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:

Example Child vs. descendant
<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

SelectorExampleSelects
ElementpAll paragraphs
Class.saleAll elements with class="sale"
Id#logoThe element with id="logo"
Universal*Everything
Grouph1, h2All h1 and all h2
Descendantnav aLinks anywhere inside nav
Childul > lili directly inside ul
Attributeinput[type="email"]Email inputs

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which selector targets all elements with class="card"?
2. What does nav a select?
3. How should the class be written in the HTML for the CSS rule .price { }?
4. Which is the best choice for styling 30 product cards the same way?

By Sir Reyel · Last updated 2026-12-04