HTML allows you to assign multiple classes to a single element using the class attribute. This technique is useful when an element needs to share different styles, such as a common button design and a separate color or size.
Using multiple classes helps developers reuse CSS rules, avoid duplicate code, and maintain a consistent website design.
What Are Multiple Classes in HTML?
Multiple classes mean assigning two or more class names to the same HTML element. You separate each class name with a space inside the class attribute.
Here is the basic syntax:
<p class="highlight important">
This paragraph uses multiple classes.
</p>
In this example, the paragraph has two classes: highlight and important. Each class can provide a different CSS style.
HTML Multiple Classes Example
The following example demonstrates how multiple classes work together.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multiple HTML Classes</title>
<style>
.highlight {
background-color: lightyellow;
padding: 10px;
}
.important {
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<h1 class="highlight">HTML Classes</h1>
<p class="highlight important">
This is an important message.
</p>
<p class="important">
This text is also important.
</p>
</body>
</html>
In this example, the first paragraph uses both classes. The highlight class adds a background and padding, while the important class changes the text color and weight.
The second paragraph uses only the important class. This demonstrates how classes can be combined or reused independently.
How CSS Selects Multiple Classes
CSS can target an element using one class or a combination of classes.
.highlight {
background-color: yellow;
}
.important {
color: red;
}
.highlight.important {
border: 2px solid black;
}
The .highlight.important selector applies a border only to elements that contain both classes. There is no space between the two class selectors.
By contrast, .highlight .important selects an element with the important class inside another element with the highlight class.
Benefits of Using Multiple Classes
Multiple classes make CSS easier to organize and reuse.
- Reusable styles: Apply the same styles to different elements.
- Flexible designs: Combine background, color, spacing, and typography styles.
- Less duplicate code: Avoid creating a separate class for every possible combination.
- Easy maintenance: Update a shared class to change the appearance of multiple elements.
For more information about reusable styles, explore HTML Classes.
Multiple Classes vs Multiple Class Attributes
An HTML element should use one class attribute containing all its class names, separated by spaces.
Correct:
<p class="highlight important">Hello!</p>
Incorrect:
<p class="highlight" class="important">Hello!</p>
The second example incorrectly repeats the class attribute. Put all class names in the same attribute instead.
Frequently Asked Questions
Can an HTML element have multiple classes?
Yes. An element can have multiple class names separated by spaces.
Does the order of class names matter?
The order of class names in the HTML attribute usually does not matter. When multiple CSS rules apply, the cascade and specificity determine which styles take effect.
Can multiple classes have conflicting CSS styles?
Yes. If two classes set different values for the same property, the CSS cascade determines the final value.
What is the difference between classes and IDs?
Classes can be reused across elements, while an ID should uniquely identify an element within a document. Learn more in HTML IDs and Class vs ID.