Skip to content
Learn Netverks

Lesson

Step 22/58 38% through track

classes

The class attribute

Last reviewed May 28, 2026 Content v20260528
Track mode
iframe_html
Means
HTML preview sandbox
Reading
~2 min
Level
advanced

This lesson

This lesson teaches The class attribute—the ideas, syntax, and habits you need before moving on in HTML.

Without a solid grasp of The class attribute, you will repeat mistakes in HTML exercises and on real pages or scripts.

You will apply The class attribute in contexts like: Websites, hybrid apps, email templates, design systems, and CMS-driven content.

Read the lesson, edit HTML/CSS in the playground, press Run to preview, then answer the lesson MCQs. Also use the HTML reference desk when you need tag or attribute lookup.

When intermediate lessons feel comfortable and you are ready for production-style trade-offs.

The class attribute accepts a space-separated list of tokens. Classes power CSS selectors, testing hooks, and occasionally JavaScript queries—reuse them liberally compared to unique IDs.

<button class="btn btn-primary">Save</button>

Naming strategies

  • Components: .card, .card__title, .card--featured (BEM-inspired).
  • Utility stacks: multiple atomic classes composed together.
  • Scoped CSS frameworks: generated hashes prevent collisions automatically.

Multiple classes

Order among tokens rarely matters to CSS unless specificity tricks appear—still, keep conventions stable (component state ordering).

Don’t encode measurements

Avoid class names like mt-16px unless generated—design tokens change; semantics shouldn’t.

JavaScript hooks

Some teams prefix hook classes (js-modal-open) to signal behavioral coupling separate from styling layers.

What maintainers curse later

  • Components exporting dozens of unordered classes—“utility soup”—hard to grep meaningful state.
  • Using classes as versioning (.v2) tied to brittle CSS descendant selectors rather than encapsulation boundaries.

BEM / utilities / CSS-in-JS

Pick one primary strategy per surface area; hybrids exist but undocumented mixes confuse juniors during refactors.

Rendered — same element, different class stacks

<button class="rounded-lg bg-indigo-600 px-4 py-2 text-white">Save</button>
<button class="rounded-lg border-2 border-dashed border-zinc-400 px-4 py-2">Draft</button>

Important interview questions and answers

  1. Q: What is the practical difference between `id` and `class`?
    A: `id` must be unique and is used for fragments/labeling; `class` is reusable for styling and behavior grouping.
  2. Q: Why is `defer` commonly preferred for scripts?
    A: It preserves HTML parsing, executes after parse, and avoids blocking rendering unlike classic synchronous scripts.
  3. Q: How do `srcset` and `sizes` work together?
    A: `srcset` provides candidate files and `sizes` tells expected rendered width so the browser can pick an optimal resource.

Tip: Class names describe purpose (card__title), not appearance (red-text).

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs in your browser in a sandboxed frame. Backend runners appear when this track’s profile allows them.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • What confused you about this lesson?
  • How would you explain this to a teammate in 30 seconds?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump