Home JavaScript Element className JavaScript Element className Property Overview
What You’ll Learn Element.className is a read/write instance property that gets and sets the element’s class attribute as a string. Learn why it is not called class, when to prefer classList , and how to replace the whole class string safely—with five examples and try-it labs.
04
Reflects class attribute
Fundamentals
Introduction HTML uses the class attribute for CSS hooks. In JavaScript, the matching DOM property is className—a string that holds one class or several classes separated by spaces.
Assigning className replaces the entire class string. That is powerful for resetting state, but for adding or removing a single class, classList is usually clearer.
const el = document.getElementById("item");
el.className = el.className === "active" ? "inactive" : "active"; 💡
Beginner tip class is the HTML attribute name. className is the DOM property name. They usually stay in sync on HTML elements.
Concept
Understanding the Property MDN: the className property of the Element interface gets and sets the value of the class attribute of the specified element.
Get / set — readable and writable string.Named className — avoids the reserved class keyword.Full replace — assignment overwrites all previous classes.Baseline — widely available since July 2015 (MDN).Foundation
📝 Syntax Value A string representing the class or space-separated classes of the current element.
Item Detail Type string (HTML elements)Access Get and set HTML attribute classSafer per-class API classList
⚠️
SVG note On SVG elements, className may be an SVGAnimatedString. Prefer getAttribute("class") / setAttribute("class", ...) for SVG.
Pattern
📋 MDN Toggle Example Shape MDN flips between active and inactive by rewriting the whole string:
<div id="item" class="active">Status</div> const el = document.getElementById("item");
el.className = el.className === "active" ? "inactive" : "active";
console.log(el.className); Related learning: classList , getAttribute("class"), and setAttribute("class", ...).
Cheat Sheet
⚡ Quick Reference Goal Code / note Read el.classNameWrite / replace el.className = "card featured"Clear all el.className = ""Per-class edits el.classList.add / remove / toggleSVG-friendly el.setAttribute("class", "my-class")MDN status Baseline Widely available
Snapshot
🔍 At a Glance Four facts about Element.className.
Type stringFull class text
Hands-On
Examples Gallery Examples follow MDN Element: className . Labs use a simple element with an id so you can read and rewrite the class string.
read MDN toggle replace all vs classList snapshot 📚 Getting Started Read the string and follow MDN’s active/inactive swap.
Example 1 — Read className Log the current class string from an element.
const el = document.getElementById("item");
console.log(el.className); How It Works The property returns the same space-separated text as the HTML class attribute on typical HTML elements.
Example 2 — MDN Active / Inactive Toggle Replace the whole string based on the current value.
const el = document.getElementById("item");
el.className = el.className === "active" ? "inactive" : "active";
console.log(el.className); How It Works This pattern works when the element has exactly one class. With multiple classes, prefer classList.toggle.
📈 Replace All, Compare APIs & Snapshot See full-string replace versus classList edits.
Example 3 — Replace the Entire Class String Assignment overwrites every previous class.
const el = document.createElement("div");
el.className = "one two three";
console.log(el.className);
el.className = "reset";
console.log(el.className);
console.log(el.outerHTML); one two three
reset
<div class="reset"></div> How It Works Use this when you intentionally want a clean slate. Do not use it to “add one more class.”
Example 4 — className vs classList Same element, two styles of updates.
const el = document.createElement("div");
el.className = "card";
el.classList.add("featured");
console.log({
className: el.className,
viaAttribute: el.getAttribute("class"),
hasFeatured: el.classList.contains("featured")
}); {
"className": "card featured",
"viaAttribute": "card featured",
"hasFeatured": true
} How It Works Start with className for a full string, then use classList for incremental changes. Both update the same class attribute.
Example 5 — Support Snapshot Feature-detect and remember when to choose each API.
console.log({
supported: "className" in Element.prototype,
type: "string (HTML elements)",
tip: "Use classList for add/remove/toggle; className to replace all",
status: "Baseline Widely available (MDN)"
}); {
"supported": true,
"type": "string (HTML elements)",
"tip": "Use classList for add/remove/toggle; className to replace all",
"status": "Baseline Widely available (MDN)"
} How It Works Both APIs are baseline. Pick the one that matches the job: whole string vs individual tokens.
Applications
🚀 Common Use Cases Reading the full class string for logging or cloning. Resetting an element to a known class set in one assignment. Simple two-state swaps when only one class is present. Initializing a new element with several classes at once. Teaching the difference between HTML attributes and DOM properties. Under the Hood
🔧 How It Works 1
HTML has a class attribute class="card featured" in markup.
Attribute
2
DOM exposes className A string property (named to avoid the class keyword).
Property
3
You read or assign a string Assignment replaces the entire class value.
Get / set
4
✓ Styles and classList stay synced The attribute, className, and classList update together.
Important
📝 Notes Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available). Named className because class is a reserved keyword in many languages. Prefer classList for adding/removing one class without wiping others. Related: classList , clientHeight , EventTarget , JavaScript hub . Compatibility
Browser Support Element.className is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.className Get / set — string form of the element's class attribute.
Baseline Widely available
Google Chrome Supported
Yes
Microsoft Edge Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Opera Supported
Yes
Internet Explorer Supported
Yes className Baseline
Bottom line: Use className to read or replace the full class string. Use classList for safe per-class changes. For SVG, prefer getAttribute/setAttribute.
Wrap Up
Conclusion className is the string view of an element’s classes. Use it to read or fully replace the class attribute, and reach for classList when you need precise token edits.
Continue with clientHeight , classList , or the JavaScript hub .
Pro Tips
💡 Best Practices ✅ Do Use className to set a complete class string Use classList for add / remove / toggle Remember assignment replaces every class Prefer setAttribute("class", ...) for SVG Keep class names readable and consistent ❌ Don’t Concatenate spaces onto className to “add” a class Expect el.class to work as a property in JS Assume SVG className is always a plain string Wipe unrelated classes by accident with full replace Mix unclear one-off class names into shared UI states Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about className Get/set the full class attribute string—named className to avoid the class keyword.
📄 01
Get / set on Element
Kind 📝 02
String full class text
Type 🔍 03
Replace all one assignment
Use ✅ 04
Baseline widely available
Status 🎯 05
classList for token edits
Tip ❓ Frequently Asked Questions What does element.className do? It gets and sets the element's class attribute as a single string (one class or space-separated classes).
Is className deprecated or experimental? No. MDN marks Element.className as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
Why is it called className instead of class? Because class is a reserved keyword in many languages used to work with the DOM, so the DOM property was named className.
When should I use classList instead? Use classList to add, remove, or toggle individual classes. Use className when you want to read or replace the entire class string at once.
Does assigning className replace all classes? Yes. el.className = "active" replaces every previous class with just "active".
What about SVG elements? On SVGElement, className can be an SVGAnimatedString. Prefer getAttribute("class") / setAttribute("class", ...) for SVG.
Did you know?
The property is called className (not class) because class is a reserved keyword in many languages that manipulate the DOM.
Next: clientHeight Learn how to measure an element’s inner height in pixels.
clientHeight → 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.
5 people found this page helpful
Helpful Share Copy link Suggestion