How to apply two CSS classes to a single element ?
Last Updated :
16 Sep, 2024
In HTML, you can apply multiple CSS classes to a single element, allowing you to style it differently. In this article, we will stick to only two classes. However, the concepts used in assigning two classes can also be extended to multiple classes.
Assigning Classes to an HTML Element
To assign classes to an HTML element, you use the class attribute. You can list multiple classes by separating their names with spaces.
Note: The names of the classes must be space-separated.
Syntax:Â
<tag_name class="class_1 class_2">
Styling Elements with CSS Classes
Once you’ve assigned classes to an element, you can style them in CSS. You have two options:
- Style each class individually using their class names.
- Style elements that contain both classes together.
1. Styling Classes Individually
When you style classes individually, each class gets its own set of CSS rules, and these styles are applied wherever those classes are used.
Syntax:Â Â
<style>
.class_1{
/* some styles */
}
.class_2{
/* some styles */
}
</style>Example:Â In this example, we will apply two CSS classes to a single element in HTML.
html
<style>
.para {
font-size: larger;
margin-bottom: 35px;
background-color: lightgreen;
}
.second_para {
color: red;
}
</style>
<body>
<p class="para">
Hello there.
</p>
<p class="para second_para">
Welcome to GeeksForGeeks.
</p>
</body>
Output:Â

In the above example, the style of class “para” is applied to both of the paragraphs while the style of the class “second_class” is only applied to the second paragraph.
2. Styling Elements Containing Both Classes
If you want to style an element that has both classes, you can combine the class selectors without spaces. This style will only apply to elements that have both classes.
Note: In the CSS selector, there is no space between the class names.
Syntax:Â
<style>
.class_1.class_2{
/* some styles */
}
</style>Example:Â This example shows the use of the above-explained approach.
html
<style>
.para.second {
font-size: larger;
margin-bottom: 35px;
margin-top: 35px;
background-color: lightgreen;
color: red;
}
</style>
<body>
<p class="para">
Hello there.
</p>
<p class="para second">
Welcome to GeeksForGeeks.
</p>
<p class="second">
Like our platform?
</p>
</body>
Output:Â
Â

In the above example, the style is only applied to the second paragraph as it is the only tag that contains both of the classes.Â
Assigning Classes Using JavaScript
You can also add and remove classes dynamically using JavaScript. The classList property of an element allows you to manipulate its classes using methods like add().
add(class_1, class_2, …): It is used to assign a class or multiple classes to an element inside HTML.Â
In this example, we will assign the classes “para” and “second” to the paragraph with ID “to_be_styled”. The styling technique is the same as described above.
Example:Â This examples shows the use of the above-explained approach.
html
<style>
.para.second {
font-size: larger;
margin-bottom: 35px;
margin-top: 35px;
background-color: lightgreen;
color: red;
}
</style>
<script>
function myFunc() {
var element = document.getElementById(
"to_be_styled");
element.classList.add("para", "second");
}
</script>
<body>
<p>Hello there.</p>
<p id="to_be_styled">
Welcome to GeeksForGeeks.
</p>
<p>Like our platform?</p>
<button onclick="myFunc()">
Click Me to see Effects
</button>
</body>
Output:Â

How to apply two CSS classes to a single element ?
Supported Browser:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari