How to apply two CSS classes to a single element ?

Multiple classes can be applied to a single element in HTML and they can be styled using CSS. In this article, we will stick to only two classes. But the concepts used in assigning two classes can be extended to multiple classes as well.

Assigning classes to an element in HTML:
The names of the classes can be written within the “class” attribute.
Note: The names of the classes must be space separated.

Syntax:

<tag_name class="class_1 class_2">

Then the classes can be either styled individually using “.class_1” and “.class_2” or that element could be styled only that contains both of the classes using “.class_1.class_2“.

Styling Individually: Both of the classes in the following example are styled individually.



Syntax:

<style>
    .class_1{
        /* some styles */
    }
  
    .class_2{
        /* some styles */
    }
</style>

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <title>
        How to apply two CSS classes
        to a single element ?    
    </title>
  
    <style>
        .para {
            font-size: larger;
            margin-bottom: 35px;
            background-color: lightgreen;
        }
  
        .second_para {
            color: red;
        }
    </style>
</head>
  
<body>
    <p class="para">
        Hello there.
    </p>
      
    <p class="para second_para">
        Welcome to GeeksForGeeks.
    </p>
</body>
  
</html>

chevron_right


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.

Styling element containing both classes: The element containing both of the classes will be styled only. All the other elements containing either one or zero of the two classes will not be styled.

Note: In CSS selector, there is no space between the class names.

Syntax:

<style>
    .class_1.class_2{
        /* some styles */
    }
</style>

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <title>
        How to apply two CSS classes
        to a single element?
    </title>
  
    <style>
        .para.second {
            font-size: larger;
            margin-bottom: 35px;
            margin-top: 35px;
            background-color: lightgreen;
            color: red;
        }
    </style>
</head>
  
<body>
    <p class="para">
        Hello there.
    </p>
      
    <p class="para second">
        Welcome to GeeksForGeeks.
    </p>
      
    <p class="second">
        Like our platform?
    </p>
</body>
  
</html>

chevron_right


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: We can also add and remove classes using JavaScript. We will use “classList” property of a tag that returns the class names as a DOMTokenList object. We will use “add()” method to add multiple classes to an element dynamically.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
      
    <title>
        How to apply two CSS classes
        to a single element?
    </title>
  
    <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>
</head>
  
<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>
  
</html>

chevron_right


Output:
Before Clicking:

After Clicking:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.