CHAPTER 08
Beginner
HTML Classes and IDs
Updated: May 10, 2026
5 min read
# HTML Classes and IDs
1. Introduction
While HTML provides structure, we need a way to specifically identify elements so we can style them with CSS or manipulate them with JavaScript. That's exactly whatclass and id attributes do. They act as name tags for your elements.
2. Learning Objectives
-
Differentiate between
classandid.
- Apply multiple classes to a single element.
- Use IDs for internal page navigation (bookmark links).
3. Detailed Explanations
The class Attribute
A class is used to identify a group of elements. You can use the exact same class name on as many elements as you want.
html
Multiple Classes
You can apply multiple classes to a single element by separating them with a space.
html
The id Attribute
An id must be unique within the entire HTML document. No two elements can share the same ID. It is used to identify a single, specific element.
html
Bookmark Links (Fragment Identifiers)
IDs have a magical secondary use: you can link directly to them to scroll the page!
html
4. Mini Project: Single-Page Navigation
Let's build a page that scrolls to different sections.
html
5. MCQs
Q1: Which of the following is TRUE about the id attribute? A) Multiple elements can share the same id. B) It can only be used on <div> tags. C) It must be unique across the entire webpage. D) It is used to add multiple classes. *Answer: C*6. Summary
Remember the golden rule: Classes are for many, IDs are for one. Use classes for styling reusable components (like cards or buttons). Use IDs for unique sections (like#header or #footer) and for "Jump to" navigation links.