Aral Code

Page Structure Lesson 25 of 42

HTML class and id Attributes

Learn the difference between class and id in HTML, how to style them with CSS, use multiple classes, and link to an id. With easy school examples.

As your pages grow, you'll want to style some paragraphs differently from others, or point to one exact element. That's what class and id are for. They are names you give to elements so CSS (and later JavaScript) can find them.

A school analogy

  • An id is like your LRN (Learner Reference Number). Only one student has it. No two elements on a page should share an id.
  • A class is like your section, "Grade 10 – Rizal." Many students belong to it, and one student can belong to several groups at once (a section, a club, a sports team).

The class attribute

Give the same class name to all the elements that should look or behave alike. In CSS, select a class with a dot before its name.

Example Styling a class
<style>
  .reminder {
    background: #fdf3dc;
    border-left: 5px solid #f2b33d;
    padding: 10px 14px;
  }
</style>
<p class="reminder">Bring your science project on Monday.</p>
<p>This is a normal paragraph.</p>
<p class="reminder">The periodic test is on Thursday.</p>
<p class="reminder">No classes on November 30 — Bonifacio Day.</p>

Change one CSS line — say, the border color — and all three reminders update together. That's the power of classes: write the style once, use it everywhere.

Classes work on any element

A class isn't tied to one element type. The same .highlight class can go on a <p>, a <span>, an <li> or a <div>.

Multiple classes on one element

Separate class names with a space. The element gets the styles of all its classes.

Example Combining classes
<style>
  .badge { display: inline-block; padding: 4px 10px; border-radius: 99px; color: white; font: bold 14px Arial, sans-serif; }
  .green { background: #1d7a43; }
  .red { background: #b4372b; }
  .big { font-size: 20px; }
</style>
<p><span class="badge green">Passed</span> <span class="badge red">Failed</span> <span class="badge green big">With Honors</span></p>

class="badge green big" means the element belongs to three classes at once.

The id attribute

An id names one unique element. In CSS, select it with a hash #.

Example Styling an id
<style>
  #top-student {
    background: #0f5c4d;
    color: white;
    padding: 12px;
    text-align: center;
    border-radius: 10px;
  }
</style>
<h2 id="top-student">🏆 Top Student: Liza Ramos</h2>
<p>Congratulations to all the honor students of Grade 10!</p>

id for jump links

As you saw in the Links lesson, ids are also the targets for # links. This is probably their most useful job in plain HTML:

<a href="#requirements">See the requirements</a>
...
<h2 id="requirements">Enrollment Requirements</h2>

Ids are also used by JavaScript to find one exact element, and by forms to connect a <label> to its input (you'll see this in the Forms lessons).

Naming rules and good habits

  • Names are case sensitive: Reminder and reminder are different.
  • No spaces in a single name. Use hyphens: top-student, main-menu.
  • Don't start a name with a number. 1st-place causes trouble in CSS; use first-place.
  • Name things by what they are, not how they look. class="warning" stays correct even if you later change the color from red to orange. class="red-text" becomes confusing.

class vs. id: which should I use?

classid
How many elements?ManyOnly one per page
Per elementCan have severalOnly one
CSS selector.name#name
Best forStyling groups of thingsJump links, forms, JavaScript

Quick Quiz

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

1. In CSS, how do you select elements with class="note"?
2. How many elements on a page should share the same id?
3. How do you give an element two classes, "card" and "featured"?
4. Which class name follows good practice?

By Sir Reyel · Last updated 2026-12-04