JavaScript Element className Property

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance property

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.

01

Kind

Instance property

02

Access

Get / set

03

Type

String

04

Reflects

class attribute

05

Status

Baseline widely

06

Pairs with

classList

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.

JavaScript
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.

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).

📝 Syntax

JavaScript
className

Value

A string representing the class or space-separated classes of the current element.

ItemDetail
Typestring (HTML elements)
AccessGet and set
HTML attributeclass
Safer per-class APIclassList
⚠️
SVG note

On SVG elements, className may be an SVGAnimatedString. Prefer getAttribute("class") / setAttribute("class", ...) for SVG.

📋 MDN Toggle Example Shape

MDN flips between active and inactive by rewriting the whole string:

JavaScript
<div id="item" class="active">Status</div>
JavaScript
const el = document.getElementById("item");
el.className = el.className === "active" ? "inactive" : "active";
console.log(el.className);

Related learning: classList, getAttribute("class"), and setAttribute("class", ...).

⚡ Quick Reference

GoalCode / note
Readel.className
Write / replaceel.className = "card featured"
Clear allel.className = ""
Per-class editsel.classList.add / remove / toggle
SVG-friendlyel.setAttribute("class", "my-class")
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about Element.className.

Kind
get / set

Instance

Type
string

Full class text

Attribute
class

HTML name

Baseline
widely

Jul 2015+

Examples Gallery

Examples follow MDN Element: className. Labs use a simple element with an id so you can read and rewrite the class string.

📚 Getting Started

Read the string and follow MDN’s active/inactive swap.

Example 1 — Read className

Log the current class string from an element.

JavaScript
const el = document.getElementById("item");
console.log(el.className);
Try It Yourself

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.

JavaScript
const el = document.getElementById("item");
el.className = el.className === "active" ? "inactive" : "active";
console.log(el.className);
Try It Yourself

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.

JavaScript
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);
Try It Yourself

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.

JavaScript
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")
});
Try It Yourself

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.

JavaScript
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)"
});
Try It Yourself

How It Works

Both APIs are baseline. Pick the one that matches the job: whole string vs individual tokens.

🚀 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.

🔧 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.

📝 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.

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.

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.

💡 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

Key Takeaways

Knowledge Unlocked

Five things to remember about className

Get/set the full class attribute string—named className to avoid the class keyword.

5
Core concepts
📝 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

It gets and sets the element's class attribute as a single string (one class or space-separated classes).
No. MDN marks Element.className as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
Because class is a reserved keyword in many languages used to work with the DOM, so the DOM property was named className.
Use classList to add, remove, or toggle individual classes. Use className when you want to read or replace the entire class string at once.
Yes. el.className = "active" replaces every previous class with just "active".
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

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.

5 people found this page helpful