Definition and Usage
The class attribute specifies one or more class names for an element.
Multiple elements can share the same class names,
which are used by CSS and JavaScript to classify, identify, select, style, and manipulate elements.
Basic Example
<p class="alert">This is an announcement.</p>
<p class="alert caution">Proceed with caution.</p>
Practical Uses
The class attribute is a global attribute.
You can specify multiple class names for a single element.
For this reason, the class attribute is also described as specifying a list of class names.
To specify multiple class names for a single element, separate the class names with spaces in the value of the class attribute.
<p class="alert caution">Proceed with caution.</p>
In addition, multiple elements can share the same class names.
alert is used by two elements.
<p class="alert">This is an announcement.</p>
<p class="alert caution">Proceed with caution.</p>
The class attribute is used for the following purposes.
- In CSS, class names are used as selectors to classify, identify, select, style, and manipulate elements.
- Like CSS, in JavaScript, class names are also used to classify, identify, select, style, and manipulate elements.
Using Class in CSS
<p class="alert">This is an announcement.</p>
<p>Paragraph element.</p>
<p class="alert caution">Proceed with caution.</p>
<p>Paragraph element.</p>
.alert { /* Selects the alert class: .class selector */
padding: 0.5em;
background-color: gold;
}
.caution { /* Selects the caution class: .class selector */
color: orangered;
}
Additional Explanation
The .class selector
selects all elements that have the specified class name.
If you place a class name immediately after ., it represents all elements that have that class name. For example, .alert is a selector that selects all elements with the class name alert.
The CSS .alert selector targets and styles elements whose class name is alert. In addition, the CSS .caution selector targets and styles elements whose class name is caution.
The <p class="alert caution">Proceed with caution.</p> element has two class names: alert and caution.
Because this element matches both the .alert and .caution selectors, the styles defined by both selectors are applied.
Different elements can share the same class names
This is an announcement.
Paragraph element.
Proceed with caution.
Paragraph element.
Using Class in JavaScript
When the button is clicked, JavaScript identifies only the elements with the class name box and changes their text from "box class" to "Hello from the box class."
<style>
div { /* Styles all div elements */
padding: 0.5em;
border: 1px solid red;
margin-bottom: 0.5em;
}
.box { /* Adds a background color only to div elements with the box class */
background-color: gold;
}
</style>
<div class="box">box class</div>
<div class="box">box class</div>
<div class="box">box class</div>
<div class="box">box class</div>
<div>div</div>
<div>div</div>
<div class="box">box class</div>
<div class="box">box class</div>
<div>div</div>
<div class="box">box class</div>
<button id="highlight-button" type="button">Click to Change Text</button>
<script>
const highlightButton = document.getElementById("highlight-button");
highlightButton.addEventListener("click", () => {
// document.querySelectorAll(".box") : Selects all elements with the class name "box"
const boxes = document.querySelectorAll(".box");
boxes.forEach(box => {
box.innerHTML = "Hello from the box class.";
});
});
</script>
As shown in this example, JavaScript uses class names to identify, select, style, and manipulate elements.
Can Also Be Applied to a Single Element
In addition to the reasons explained earlier for using the class attribute, you can also apply a specific class name from the class attribute to only one element.
This is similar to using the id attribute to establish a unique identifier within a web page, but there is a fundamental difference.
Additional Explanation
The id attribute specifies a unique identifier (ID) for an element.
The value of this attribute must be unique and distinct within the document (web page).
Using a class name from the class attribute on only one element simply means that the element happens to be the only element classified with that class name.
It does not mean that the element is being used to establish a unique identifier within the web page like the id attribute.
If you use the value of an id attribute as a selector only for applying CSS to a single element, the styling will still apply correctly. However, if the element using the id attribute does not actually serve the important purpose of being a unique identifier, it may make the context of the code harder to understand during future maintenance (for example, someone may wonder why an unnecessary unique identifier was used).
Things to Keep in Mind
To specify multiple class names for a single HTML element, you must separate the class names with spaces in the value of the class attribute.
You should not use multiple class attributes on a single HTML element.
class attributes on a single HTML element.
<p class="a" class="b">You can specify multiple class names for a single HTML element.</p>
class attribute.
<p class="a b">You can specify multiple class names for a single HTML element.</p>
Browser Compatibility
| Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
class
|
1 | 12 | 1 | 4 |
Specifications
| Specification | |
|---|---|
class
|
HTML Standard #global-attributes:classes-2 |