HTML Class and ID
class and id are common global attributes. They help you identify elements. A class is a reusable label. Many elements can have the same class, such as class="card". An id is a unique identifier for one element on a page. For example: id="contact". CSS often uses class and id to style elements. JavaScript can also use them to find elements. An id can also be used as a target for a link, such as <a href="#contact">Contact</a>.
Why this matters
- ·Classes let you group similar elements.
- ·IDs give one element a unique name.
- ·Both are useful for CSS, JavaScript, and navigation.
Common mistakes
- ·Giving many elements the same unique id creates invalid, confusing markup.
- ·Using id for every element makes reusable styling harder.
- ·Class and id names should be clear and meaningful.
Advertisement
Your codeHTML
Live preview
Advertisement
Frequently asked questions
Yes.
Advertisement
Practice questions
Question 1
Create two cards with the same class.
Your codeHTML
Live preview
Question 2
Create a contact section and a link that jumps to it.
Your codeHTML
Live preview
Quick check
Just for this topic answer honestly, then check yourself.
1 / 3
