Element.className is a read/write property that reflects the element’s HTML class attribute as a string. One class, or several classes separated by spaces (MDN).
Read the notes below before you get or set this property. Prefer feature detection when support is limited.
Syntax
Use el.className. Start with this line.
JavaScript
el.className / el.className = "…"
Call
What it does
el.className / el.className = "…"
Primary signature.
Built into the browser. Read or assign el.className — no import required.
Library declarations
JavaScript
el.className / el.className = "…"
Parameters
Parameter
Description
classList
Optional. Safer for per-class edits
Return Value
Return Value
Description
*
The current value of el.className.
JavaScript Examples
Example 1 — Read the Class String
Read className from an element that has two classes.
Set three classes. The first assignment stores "one two three".
Replace the string."reset" removes one, two, and three.
Check the markup.outerHTML shows the updated class attribute.
Rule of thumb. Use full-string assign for resets; use classList to add one class.
Compatibility
Browser Support
Element.className is Baseline Widely available (MDN: since July 2015). Safe across modern and older evergreen browsers.
✓ Baseline · Widely available
Element.className
Get/set the class attribute as a string — long-standing DOM support on HTML elements.
99%Widely available
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
classNameExcellent
Bottom line: Safe for production on HTML elements. For SVG, prefer classList or getAttribute/setAttribute because SVGElement.className (SVGAnimatedString) is deprecated.
Frequently Asked Questions
It gets and sets the element's class attribute as a single string — one class name, or several names separated by spaces.
No. MDN marks Element.className as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. Note: SVGElement.className (SVGAnimatedString) is deprecated — prefer classList or getAttribute/setAttribute for SVG.
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, toggle, or check 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". To clear all classes, assign an empty string.
On SVGElement, className can be an SVGAnimatedString (and that SVG-specific form is deprecated). Prefer el.classList, or getAttribute("class") / setAttribute("class", ...).