HTML class Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The class global attribute is a list of the element’s class names, separated by ASCII whitespace. CSS and JavaScript use those names to style or find elements — for example .note or getElementsByClassName("note").

Prefer names that describe purpose (note, warning) over looks (red-text). For a single unique hook, use id. Inline one-offs belong in style sparingly; shared looks belong in a <style> or stylesheet.

Baseline · Widely available.

Not deprecated. Available on every HTML element. classList is widely available for toggling classes in script.

Valid CSS identifiers help.

Odd names like 1234 or my?class need escaping in selectors. Prefer letters, digits, hyphens, and underscores that work as plain CSS identifiers.

HTML Syntax

syntax.html
<element class=" name name …">

Separate multiple class names with spaces. Empty class="" means no classes.

Attribute Values

Common patterns for the value string:

ValueDescriptionTry it
One namee.g. class="note" — matched by CSS .noteTry it
Multiple namesSpace-separated list, e.g. class="note editorial"Try it
ReusableSame class on many elements that share a roleTry it
Toggle in JSUse classList.add / remove / toggleTry it

Live Preview

Apply one or more class names to a sample paragraph. Watch the class attribute and styling update.

Demo styles: .note is italic; .editorial adds a light highlight.

Paragraph

Narrator: This is the beginning of the play.

class=""

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Style with One Class

Reuse a class name anywhere the same look or role applies.

HTML
<style>
  .note {
    font-style: italic;
  }
</style>

<p class="note">[Lights go up; Caspian enters stage right]</p>
<p>Narrator: The story continues…</p>
Try It Yourself

How It Works

  1. class="note" tags the paragraph for CSS.
  2. .note in CSS selects every element with that class.
  3. Reuse freely — many notes can share one rule.
  4. Name by role (note), not by current font style.

Example 2 — Multiple Classes

Combine independent roles with a space-separated list.

HTML
<style>
  .note { font-style: italic; font-weight: bold; }
  .editorial { background: rgb(255 0 0 / 0.2); padding: 0.5rem; }
</style>

<p class="note editorial">Above point sounds a bit obvious. Remove/rewrite?</p>
Try It Yourself

How It Works

  1. Two tokens — note and editorial — in one attribute.
  2. Each CSS rule applies when its class is present.
  3. Compose styles without duplicating class names on every element.
  4. Spaces only — commas are not valid separators in class.

Example 3 — Toggle a Class with JavaScript

Use classList instead of rewriting the whole class string.

HTML
<style>
  .is-active { outline: 2px solid currentColor; padding: 0.25rem; }
</style>

<p id="box" class="card">Click the button to toggle <code>is-active</code>.</p>
<button type="button" id="btn">Toggle</button>

<script>
  document.getElementById("btn").addEventListener("click", function () {
    document.getElementById("box").classList.toggle("is-active");
  });
</script>
Try It Yourself

How It Works

  1. class="card" is the starting token list.
  2. classList.toggle adds or removes is-active safely.
  3. Other classes stay — you do not rebuild the whole string.
  4. CSS reacts whenever the class list changes.

Edge Cases & Pitfalls

Most bugs come from presentational names, commas, or fighting id uniqueness rules.

spaces

Spaces separate classes — not commas

class="a, b" is one odd token (or invalid for CSS). Use class="a b".

vs id

Do not reuse an id like a class

id must be unique. Shared styling and hooks belong on class.

naming

Prefer semantic names

warning ages better than red-bold when the design changes.

selectors

Escape awkward identifiers

Names starting with digits or containing ? need CSS.escape() or careful escaping in selectors.

JS

Prefer classList over string surgery

Manual className concat often duplicates or drops tokens. add / remove / toggle are safer.

Browser Support

class is a global HTML attribute. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

class

Supported in every modern browser. Element.classList is widely available for add/remove/toggle since about October 2017.

99% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All modern
Full support
Internet Explorer All (classList IE10+)
Supported
class Widely supported

Bottom line: Safe everywhere. Use class for reusable hooks; use classList in script; keep names valid CSS identifiers when you can.

Key Takeaways

  • Role: space-separated names for CSS and JavaScript selection.
  • Global: valid on any HTML element; reusable across many nodes.
  • Multiple: class="note editorial" — spaces, not commas.
  • JS: prefer classList; use id only when unique.

One line: class names reusable hooks — style with .name, toggle with classList.

Frequently Asked Questions

It lists space-separated class names on an element so CSS and JavaScript can select that element (for example .note or getElementsByClassName).
Yes. You can put class on any HTML element.
Yes. Separate names with spaces, for example class="note editorial". Order in the attribute does not set CSS priority by itself.
id must be unique in the document and is meant for one specific element. class can be reused on many elements that share the same role or style.
Prefer element.classList (add, remove, toggle, contains). element.className is the whole space-separated string.
No. It is a core global attribute and Baseline widely available.

Next: classid

Legacy object attribute for plugin class IDs — obsolete today.

classid tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful