Home HTML Attributes class HTML class Attribute Definition
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.
Syntax
HTML Syntax <element class=" name name …">
Separate multiple class names with spaces. Empty class="" means no classes.
Values
Attribute Values Common patterns for the value string:
Value Description Try it One name e.g. class="note" — matched by CSS .note Try itMultiple names Space-separated list, e.g. class="note editorial" Try itReusable Same class on many elements that share a role Try itToggle in JS Use classList.add / remove / toggle Try it
Try it
Live Preview Apply one or more class names to a sample paragraph. Watch the class attribute and styling update.
Code
HTML Examples Short, copy-ready snippets. Use Try It Yourself for a live editor.
1. One class 2. Multiple classes 3. Toggle with classList Example 1 — Style with One Class Reuse a class name anywhere the same look or role applies.
<style>
.note {
font-style: italic;
}
</style>
<p class="note">[Lights go up; Caspian enters stage right]</p>
<p>Narrator: The story continues…</p> [Lights go up; Caspian enters stage right]
Narrator: The story continues…
How It Works class="note" tags the paragraph for CSS..note in CSS selects every element with that class.Reuse freely — many notes can share one rule.Name by role (note), not by current font style.Example 2 — Multiple Classes Combine independent roles with a space-separated list.
<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> Above point sounds a bit obvious. Remove/rewrite?
How It Works Two tokens — note and editorial — in one attribute.Each CSS rule applies when its class is present.Compose styles without duplicating class names on every element.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.
<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> Click the button to toggle is-active.
Toggle How It Works class="card" is the starting token list.classList.toggle adds or removes is-active safely.Other classes stay — you do not rebuild the whole string.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.
Compatibility
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.
Remember
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 What does the HTML class attribute do? It lists space-separated class names on an element so CSS and JavaScript can select that element (for example .note or getElementsByClassName).
Is class a global attribute? Yes. You can put class on any HTML element.
Can an element have more than one class? Yes. Separate names with spaces, for example class="note editorial". Order in the attribute does not set CSS priority by itself.
class vs id? 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.
How do I change classes in JavaScript? Prefer element.classList (add, remove, toggle, contains). element.className is the whole space-separated string.
Is class deprecated? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion