On this page11 sections
  1. Learning Objectives
  2. 1. The class Attribute
  3. 2. Several Classes on One Element
  4. 3. The id Attribute
  5. 4. Ids as Bookmarks
  6. 5. Ids and JavaScript
  7. 6. Class or Id?
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 017: Classes & Id

Category4: Page Structure
Lecture017 of 048
Estimated video length12 min
TargetHTML5 · any modern browser
LevelBeginner

class and id give elements names. CSS uses those names to style them, JavaScript uses them to find them, and links can jump to an id. 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 id for styling, bookmarks and JavaScript
  • Choose between class and id

1. The class Attribute

Example: three cities share one class, so one rule styles them all:

html
<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:

html
<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

html
<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

html
<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

html
<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

html
<p id="greeting">Hello!</p>
<button onclick="document.getElementById('greeting').textContent = 'Assalam-o-Alaikum!'">Change greeting</button>

6. Class or Id?

classid
How many elements?ManyOne
CSS selector.name#name
Typical useStylingBookmarks, 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

  1. How many elements can share one class? One id?
  2. Write the CSS selector for the class card and for the id logo.
  3. Give a paragraph both the classes warning and big.
  4. Write a link that jumps to the element with id="contact".
  5. Which JavaScript method finds an element by id?
  6. Should you usually style with classes or ids? Why?

Practice Answers

  1. Any number of elements can share a class; an id belongs to just one.
  2. .card and #logo.
  3. <p class="warning big">…</p>
  4. <a href="#contact">Contact</a>
  5. document.getElementById('…')
  6. Classes: they're reusable, and their lower specificity makes styles easy to override.

Key Takeaways

  • class names a group; id names one unique element.
  • CSS: .class and #id.
  • One element can have many classes, separated by spaces.
  • Ids power bookmarks (#id) and getElementById.