On this page11 sections
Lecture 017: Classes & Id
| Category | 4: Page Structure |
| Lecture | 017 of 048 |
| Estimated video length | 12 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
classandidgive elements names. CSS uses those names to style them, JavaScript uses them to find them, and links can jump to anid. The rule to remember: a class can be shared by many elements, but an id belongs to exactly one.
Learning Objectives
- Style groups of elements with a
class - Give an element several classes
- Use a unique
idfor styling, bookmarks and JavaScript - Choose between
classandid
1. The class Attribute
Example: three cities share one class, so one rule styles them all:
<style>
.city { background: #343a40; color: white; padding: 10px; margin-bottom: 8px; border-radius: 6px; }
</style>
<div class="city"><h3>Karachi</h3><p>The largest city in Pakistan.</p></div>
<div class="city"><h3>Lahore</h3><p>Famous for its food and gardens.</p></div>
<div class="city"><h3>Peshawar</h3><p>One of the oldest cities in South Asia.</p></div>In CSS, a class name starts with a dot: .city.
Classes work on any element, including inline ones:
<style>.note { color: #e8590c; font-weight: bold; }</style>
<p>Remember: <span class="note">the exam starts at 9am</span>.</p>2. Several Classes on One Element
Separate class names with spaces. The element gets the styles of all of them.
Example
<style>
.city { background: #343a40; color: white; padding: 10px; margin-bottom: 8px; }
.capital { border-left: 6px solid #fab005; }
</style>
<div class="city capital"><h3>Islamabad</h3><p>The capital.</p></div>
<div class="city"><h3>Multan</h3><p>The city of saints.</p></div>3. The id Attribute
An id must be unique: only one element on the page may use it. In CSS it starts with a hash: #header.
Example
<style>
#page-title { background: #1971c2; color: white; padding: 14px; text-align: center; }
</style>
<h1 id="page-title">My Travel Blog</h1>
<p>Stories from the road.</p>4. Ids as Bookmarks
A link to #something jumps to the element with that id (Lecture 010).
Example
<p><a href="#ch3">Jump to Chapter 3</a></p>
<h2>Chapter 1</h2><p style="height: 200px;">…</p>
<h2>Chapter 2</h2><p style="height: 200px;">…</p>
<h2 id="ch3">Chapter 3</h2><p>You made it.</p>5. Ids and JavaScript
JavaScript finds an element by its id with getElementById.
Example
<p id="greeting">Hello!</p>
<button onclick="document.getElementById('greeting').textContent = 'Assalam-o-Alaikum!'">Change greeting</button>6. Class or Id?
class | id | |
|---|---|---|
| How many elements? | Many | One |
| CSS selector | .name | #name |
| Typical use | Styling | Bookmarks, JavaScript, one-off elements |
Use classes for styling almost always. Their lower specificity makes them easy to combine and override.
Common Beginner Mistakes
1. Reusing an id. Two elements with id="title" is invalid, and getElementById finds only the first.
2. Forgetting the dot or hash. city { } looks for a <city> tag. Use .city or #city.
3. Spaces inside a class name. class="main title" is two classes. Use main-title for one.
4. Case mismatch. .City and .city are different. Keep names lowercase.
Practice Questions
- How many elements can share one class? One id?
- Write the CSS selector for the class
cardand for the idlogo. - Give a paragraph both the classes
warningandbig. - Write a link that jumps to the element with
id="contact". - Which JavaScript method finds an element by id?
- Should you usually style with classes or ids? Why?
Practice Answers
- Any number of elements can share a class; an id belongs to just one.
.cardand#logo.<p class="warning big">…</p><a href="#contact">Contact</a>document.getElementById('…')- Classes: they're reusable, and their lower specificity makes styles easy to override.
Key Takeaways
classnames a group;idnames one unique element.- CSS:
.classand#id. - One element can have many classes, separated by spaces.
- Ids power bookmarks (
#id) andgetElementById.