Why React uses className over class attribute ?
Last Updated :
30 Oct, 2023
To all the regular DOM and SVG elements like <button>, <li>, <a>, <div>, etc., if we want to apply the CSS classes, we use the className attribute instead of class in React. That's why it warns you every time when you mistakenly write class instead of className.
There are very few scenarios where the DOM property for a given HTML attribute uses a different name. For example class as className. But nothing has changed with it, the semantic meaning of both className and class is the same, when JSX is rendered, the className attribute is automatically rendered as a class attribute.
Why React uses className over class attribute ?
The only reason behind the fact that it uses className over class is that the class is a reserved keyword in JavaScript and since we use JSX in React which itself is the extension of JavaScript, so we have to use className instead of class attribute. Also, when the JSX is transpiled to JavaScript if we are using a class attribute it will conflict with the JavaScript classes. That's why to achieve better compatibility and consistency with other libraries React use className instead of the class attribute.
In earlier times before React 16, if you wrote JSX with an unknown element that was not recognized by React, it would simply skip it.
For example, The below line of code would render an empty div to the DOM in React 15,
// React 15 output
<div myatrribute="xyz" /> => <div />
But in React 16, this unknown attribute "xyz" will end up in the DOM as well.
// React 16 output
<div myatrribute="xyz" />
That's why, in React 15, when you use class to specify a CSS class to any element, it just warns you and ignores that. But now due to the new DOM attributes handling in React 16, it still warns but converts values to strings and passes them through as well.
Creating React Application
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Project Structure
It will look like the following.
Project StructureExample 1: Now, Let's understand this with some practical implementation, suppose we simply render a heading <h1> with some text in its inner HTML from our default component App.js.
JavaScript
// Filename - App.js
import "./App.css";
function App() {
return (
<h1 class="heading1">
This is an example code
</h1>
);
}
export default App;
Steps to Run: Use this command in terminal.
npm start
Output: In the above code, we have used class instead of className and hence in the console, we received a warning which says: "Invalid DOM property 'class', Did you mean 'className'? " But it only warns you in React 16, and that is why output in above code has not get affected by it.

Example 2: You can get rid of the above example's warning by simply using the className in place of class, as done in below example:
JavaScript
// Filename - App.js
import "./App.css";
function App() {
return (
<h1 className="heading1">
This is an example code
</h1>
);
}
export default App;
Output: As we can see no worning are there in console window for that existed for using class attribute.

Similar Reads
ReactJS className Attribute
In React, the className attribute is used to apply CSS classes to elements, allowing developers to style their components effectively. While standard HTML uses the class attribute for this purpose, React requires the use of className instead. This distinction is due to the way JSX (JavaScript XML) h
2 min read
Why are dashes preferred for CSS selectors / HTML attributes ?
CSS is a hyphen-delimited syntax language which means while writing HTML attributes we can use hyphen(-). Example: font-size, line-height, background-color, etc... There are various reasons to preferring hyphen(-): In CSS, one can use underscore(_) instead of hyphen(-) for CSS selectors(class, id, s
2 min read
Python Attributes: Class Vs. Instance Explained
In Python, attributes are properties associated with objects. They can be variables or methods that are defined within a class or an instance of a class. Understanding the difference between class and instance attributes is fundamental in object-oriented programming. Here's an explanation: Python At
4 min read
HTML | <select> name Attribute
The HTML <select> name Attribute is used to specify a name for the drop-down list. It is used to reference the form-data after submitting the form or to reference the element in a JavaScript. Syntax: <select name="text"> Attribute Values: It contains the value i.e name which specify the
1 min read
HTML | <object> name Attribute
The HTML <object> name Attribute is used to specify the name of the embedded file. This attribute is also used as a reference for an object element in the Javascript. Syntax: <object name="name"> Attribute Values: name: It specify the name of the embedded file. Example: html <!DOCTYPE
1 min read
Why do we pass __name__ to the Flask class?
The __name__ is a built-in special variable that evaluates the name of the current module. If the source file is executed as the main program, the interpreter sets the __name__ variable to have a value â__main__â. If this file is being imported from another module, __name__ will be set to the module
2 min read
HTML | param name Attribute
The HTML param name Attribute is used to specify a name of a <param> element. This attribute is used together with the value attribute to define a parameter for plug-ins which is associated with <object> element. Syntax: <param name="name"> Note: This attribute is depreciated from
1 min read
What is the aria-labelledby attribute ?
The aria-labelledby attribute is an inherent attribute in hypertext markup language that's wont to produce relationships between objects and there labels. Once any component containing each the attribute aria-labelledby and aria-label attribute the browsers high priority are going to be aria-labelle
3 min read
Which characters are valid in CSS class names/selectors?
It is very easy to choose a valid class name or selectors in CSS just follow the rule. A valid name should start with an underscore (_), a hyphen (-) or a letter (a-z)/(A-Z) which is followed by any numbers, hyphens, underscores, letters.It cannot start with a digit, starting with the digit is accep
2 min read
What is the Difference Between Design Classes and Analysis classes?
In System Design, understanding the difference between design classes and analysis classes is crucial. Analysis classes are like detectives they investigate and understand the problem at hand. They focus on what the system needs to do, without diving into how it will be done. These classes help deve
7 min read