Favorites

DOM Element.className Property

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Element.className
DOM Element PropertyThis property is supported by all modern browsers.
Updated 28th November 2026

The className property gets or sets an element's class attribute as a single string. classList is usually easier to work with.

Description

The className property gets or sets the value of the element's class attribute as one space-separated string. Setting it replaces every class. To add, remove or toggle individual classes without affecting the others, use classList. (It's called className because class is a reserved word in JavaScript.)

Reading className gives the attribute exactly as written, including any extra spaces. For SVG elements, className returns an SVGAnimatedString object rather than a string, which is another reason to prefer classList or getAttribute('class') in code that may handle SVG.

Setting className = '' removes every class at once, which is simpler than removing them one by one. Toggling individual classes is safer with classList, because it doesn't disturb classes added by other code.

Syntax

element.className
element.className = value

Value

A string of the element's classes, separated by spaces.

Example

<div id="el" class="card">Card</div>
const el = document.getElementById('el');
console.log(el.className);
el.className = 'card highlighted';
console.log(el.className, el.classList.length);

Browser Support

The following information will show you the current browser support for the Element.className property. Hover over a browser icon to see the version that first introduced support for this property.

This property is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee