class
Reusable names for components, states and styling groups.
Learn class, id as part of the HTML attribute system: what they configure, where they belong and which mistakes to avoid.
Class and ID
The class attribute groups elements. The id attribute identifies one exact element. They look similar in HTML, but they are used very differently in CSS, JavaScript, anchors, labels and automated tests.
A strong project uses class names for repeatable patterns and ids only when uniqueness matters. That keeps the page flexible as the design grows.
class, id. Reusable CSS hooks, unique anchors and label targets. What belongs here
classReusable names for components, states and styling groups.
idOne unique name for a section, form field, anchor or script target.
for + idA label uses for to point to the matching input id.
href="#id"A link can jump to an element with a matching id.
Syntax in context
Use class for shared styling. Use id when one element must be targeted directly or when a label or anchor needs a destination.
<section id="contact" class="content-panel"> <h2>Contact</h2> <label for="email">Email address</label> <input id="email" class="field" name="email" type="email"> </section>
Good versus weak
<section id="contact" class="content-panel"> <h2>Contact</h2> <label for="email">Email address</label> <input id="email" class="field" name="email" type="email"> </section>
<section id="box"> <input id="box" type="email"> <p class="blue large shadow margin-top">Email</p> </section>
Rules that matter
If you need the same styling on many elements, use class instead.
Prefer names such as card, lesson-title or nav-link over color-only names.
A label with for="email" must match an input with id="email".
Changing an id can break bookmarks and internal links.
Production thinking
Attributes are small, but they change how an element works. A good attribute can make a link usable, an image understandable, a form easier to complete or a script safer to load.
A correct label/id relationship makes form fields easier to use with screen readers and larger click targets.
Class and id names become part of your frontend contract. Choose names that can survive design changes.
Live code lab
Edit the HTML or CSS, then use Run to refresh the preview. The preview is isolated, so links and forms stay inside this practice area.
Mini assignment
Practice assignment
Try it yourself
Self-check
Do not only read this page. Answer these questions out loud or write the answers in your own notes. If one answer feels vague, revisit the examples before moving on.