class
Reusable hook for CSS and JavaScript.
Learn Classes & ID with practical examples for structure, layout semantics, metadata, embedded content, scripts, accessibility, SEO and live practice.
HTML structure and layout
The class attribute is reusable. Many elements can share the same class, which makes it the normal hook for styling components and repeated patterns.
The id attribute should be unique on a page. It is useful for fragment links, labels, JavaScript targets and special one-off sections.
Professional HTML uses classes and IDs deliberately. Random names make CSS hard to maintain and JavaScript fragile.
Reusable hook for CSS and JavaScript.
Unique identifier for one element on the page.
A link such as #pricing that jumps to an id.
Clear names make components easier to understand.
Syntax and structure
A class can appear many times. An id should appear once per page.
<section id="pricing" class="section section--dark">
<article class="price-card">
<h2>Starter</h2>
</article>
</section>
<a href="#pricing">View pricing</a>
<div id="card">First card</div> <div id="card">Second card</div> <p class="green-big-bold-left">Text</p>
Rules that matter
Layout-focused HTML is strongest when meaning, grouping, metadata and behavior hooks all have a clear reason to exist.
Classes are perfect for repeated styling such as cards, buttons and sections.
Duplicate IDs break anchors, labels and JavaScript assumptions.
Prefer price-card over blue-box when the visual style may change.
A class should survive reasonable design changes.
Sections that need direct links can use stable IDs.
A class name does not create semantic meaning by itself.
Production thinking
This matters because classes and IDs are the contract between HTML, CSS, JavaScript, tests and analytics.
IDs often connect labels to inputs and controls to descriptions. Duplicate IDs can break those relationships.
Stable class naming is a team skill. A clean naming system prevents CSS from turning into an unsearchable pile of exceptions.
Descriptive IDs can create useful section URLs. They also help internal links point directly to meaningful parts of a page.
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.