The Wayback Machine - https://web.archive.org/web/20241127001919/https://www.geeksforgeeks.org/how-to-apply-two-css-classes-to-a-single-element/
Open In App

How to apply two CSS classes to a single element ?

Last Updated : 16 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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: 

How to apply two CSS classes to a single element ?

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: 
 

How to apply two CSS classes to a single element ?

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 ?

How to apply two CSS classes to a single element ?

Supported Browser:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


Previous Article
Next Article

Similar Reads

How to apply box-shadow to display elements like single element to user in CSS ?
In CSS, the box-shadow property allows you to add a shadow effect to an element. However, when it comes to applying box shadow to a combination of elements such as a <span> and <input>, it can be a bit tricky to get the desired effect. In this article, we will discuss how to apply box-shadow to a <span> <input> combination s
2 min read
How to apply Multiple Classes to an Element in HTML ?
In HTML, applying multiple classes to an element allows you to combine styles from different class definitions. This is useful when you want to reuse styles and create more modular and maintainable code. To apply multiple classes to an element, you can simply add the class names, separated by spaces, within the class attribute. Syntax<div class=
1 min read
Tailwind CSS 3 Classes doesn't Override Previous Classes
Tailwind CSS is basically a Utility-first CSS framework for building rapid custom UI. It is a highly customizable, low-level CSS framework that gives you all of the building blocks that you need. Also, it is a cool way to write inline styling and achieve an awesome interface without writing a single line of your own CSS. Syntax<div class="bg-blu
2 min read
How to apply styles to multiple classes at once ?
Applying styles to multiple classes at once means using a single CSS rule to style multiple elements that share different class names. This can be achieved by separating class selectors with commas, allowing for efficient styling of various elements without redundant code. Here we have some common approaches to applying styles to multiple classes a
2 min read
How to apply multiple transform property to an element using CSS ?
There is a variety of procedures to apply multiple transform property in an element. In this article we will discuss the easiest and popular ones. In the 1st method we will be combining the transform property itself. In the 2nd method, we will use the nested classes to apply other transforms. Method 1: In this method, we will combine the transform
3 min read
How to apply css property to a child element using JQuery?
The task is to apply CSS property to a child element using jQuery. To do that, first we select the child element with the help of children() method in jQuery and then apply CSS property to it with the help of css() method in jQuery. Syntax: // Note : children method selects the direct child to parent $(selector).children("selector").css("property-n
2 min read
How to apply no shadow in an element using CSS ?
In this article, we are going to learn How to apply no shadow in an element using CSS, To remove the box shadow from an element in CSS, you can use the box-shadow property with a value of none, like this: box-shadow: none, This will effectively remove any existing shadow or prevent a shadow from being applied to the element. In this article, we wil
1 min read
How to apply styles only to the first child of an element using CSS ?
The :first-child selector is a pseudo-class in CSS that enables you to target the initial child element within a parent container. This selector handpicks elements that are the first immediate child of their parent. It serves as a powerful selector that makes it easier to apply unique styles or modifications to that specific element. <div class=
1 min read
How to apply CSS in a particular div element using jQuery ?
In this article, we will set the CSS of a div element using jQuery. We will use the CSS property to set the CSS. Approach: We will create a div element with some text and hyperlinks. Then we will use jQuery to apply CSS to the div element. jQuery comes with the function .css() where we can get and set CSS of any element. We will use it for the div
2 min read
How to add and remove CSS classes to an element using jQuery ?
In this article, we will see how to add or remove the CSS classes to an element using jQuery. To add the CSS classes to an element we use addClass() method, and to remove the CSS classes we use removeClass() method. Syntax: The syntax for adding CSS classes to an element: $('selector').addClass(class_name); The syntax for Removing CSS classes to an
2 min read
Copy all the attributes of one element and apply them to another with JavaScript
The task is to copy all attributes of one element and apply them to another with the help of JavaScript. Here 2 approaches are discussed. Approach 1: Select both elements(target and source element).Get the properties of source element by using el.prop('attributes') method.Use .each() method on each object property and set that property to the targe
2 min read
How to apply styles on an element using jQuery ?
In this article, we will learn how one can apply a style on an element using jQuery. Suppose you are building a website or game where you need the user interface to be interactive. In such cases styles can play a huge role there in this post we are going to learn how can we change styles when certain events got triggered with the help of JQuery. We
3 min read
How to apply an id attribute to a child element of a ReactJS component ?
To apply the id attribute to a child element of the React JS component, we can use the React JS states and props. Also, using hooks we can directly access the Dom elements and modify the attributes as required. PrerequisitesReact JSuseState HookuseRef HookTable of Content Passing id as a prop to child componentUsing useRef hookSteps to create React
3 min read
How to apply CSS page-break to print a table with lots of rows?
When printing a table with lots of rows the problem can arise in keeping the data together when the page ends. Since the data presented continuously makes more sense. Here we will track down ways by which one can print contents of a table with lots of rows when the situation of page-break is encountered. The most logical property that can be used f
3 min read
How to apply Amazing Hover Effect over Button using HTML and CSS ?
You have visited many websites and you have seen many hover buttons present in those websites. So, such Hover Button animation effect can be easily generated by using HTML and CSS. By using HTML we will design the basic structure of the button and then by using the properties of CSS, we can create the hover animation effect. A sample video is provi
3 min read
How to apply CSS Padding Shorthand Property ?
Padding is the space between the element and its border. The shorthand property of padding sets the padding area in all four sides of the element. The padding value can be in px, em, rem or %. If we want to apply padding on a specific side we can use the following properties: padding-left: Sets padding on the left side of the element.padding-right:
2 min read
How to apply inline CSS ?
In this article we will learn how to apply inline CSS, Inline CSS contains the CSS property in the body section attached to the element is known as inline CSS. This kind of style is specified within an HTML tag using the style attribute. It is used to apply a unique style to a single HTML element. Syntax: <tag style = " "></tag> Example
1 min read
What is @apply in CSS ?
In this article, we will see the @apply rule property in CSS. This rule is used to implement a bunch of CSS properties which is already been declared in a variable like a method in JAVA. In simple words, we will make a set of CSS properties and store them into a variable for later use and we can use these properties as much time as we want. Syntax:
2 min read
How to apply background image with linear gradient using Tailwind CSS ?
In this article, we will see how to apply background images with a linear gradient using Tailwind CSS. Tailwind CSS is a highly customizable, utility-first CSS framework from which we can use utility classes to build any design. To apply background images with linear gradients we use the background image utility of Tailwind CSS. It is the alternati
2 min read
How to apply CSS style using jQuery ?
In this article, we will explore how we can apply CSS styles to HTML elements. It is recommended that you have some basic knowledge of HTML, CSS, JavaScript, and jQuery before beginning this article. It is possible to change the CSS property of an element by using a simple JavaScript API, but we try to complete this challenge using jQuery css() met
2 min read
How to apply CSS in last child of parent using jQuery ?
In this article, we will see how to set styles in the last child of the parent element using jQuery. To set the style on the last child of parent, we use jQuery :last-child selector. The :last-child selector is used to select every element that is the last child of its parent element. Syntax: $("Selector:last-child") Approach: First, we create an H
1 min read
How to apply CSS in even childs of parent node using jQuery ?
In this article, we will see how to set styles in even the child of the parent element using jQuery. To set the style on the even child of the parent, we use jQuery :nth-child(even) selector. The :nth-child(even) selector is used to select every element that is the even child of its parent element. Syntax: $("Selector:nth-child(even)") Approach: Fi
1 min read
How to apply CSS in odd childs of parent node using jQuery ?
In this article, we will see how to set styles in the odd child of the parent element using jQuery. To set the style on the odd child of the parent, we use jQuery :nth-child(odd) selector. The :nth-child(odd) selector is used to select every element that is the odd child of its parent element. Syntax: $("Selector:nth-child(odd)") Approach: First, w
1 min read
How to apply CSS style to the different elements having same class name in HTML ?
In this article, We will learn to apply CSS style to the different elements having the same class name in the HTML. The class attribute is used to create a class for the HTML element and multiple elements can have the same class. The class name is often used to apply CSS style and also used in javascript to manipulate elements. The CSS class select
2 min read
How to use @apply directive in Tailwind-CSS ?
Tailwind CSS is a popular utility-first CSS framework. It is a set of pre-defined CSS classes that can be used to quickly and easily style your HTML elements without having to write custom CSS code. The classes are organized into a set of utility classes that can be used to control the layout, spacing, typography, and other aspects of your design.
3 min read
How to dynamically create and apply CSS class in JavaScript ?
To dynamically create a CSS class and apply it to the element dynamically using JavaScript, first, we create a class and assign it to HTML elements on which we want to apply CSS property. We can use className and classList properties in JavaScript. ApproachThe className property is used to add a class in JavaScript. It overwrites existing classes o
2 min read
How to apply gradient as the mask using CSS ?
In this article, we will learn how to include the gradient as the mask layer in CSS, along with understanding their basic implementation with the help of suitable examples. The Mask Property with a Gradient is used to create a mask layer, that can be utilized for rendering or hiding the area of the content of an element. There are mainly 2 types of
3 min read
Apply Glowing Effect to the Image using HTML and CSS
While surfing most of the websites you have seen some special effects which can be seen on various images while putting your cursor over them. So, in this article, we are going to implement the same. We can use such images as a card for our website.  In this article, you’re going to learn how to apply the glowing effect to a particular image, when
2 min read
How to apply the Clearfix Hack in CSS ?
The "Clearfix" hack is a technique used in CSS to clear or contain floated elements within a container. It addresses issues where a container does not fully encompass its floated child elements, causing layout problems, especially with collapsing margins. There are various ways to implement the clearfix hack, but a common method involves adding a p
1 min read
How to apply the @media rule in CSS ?
The @media rule in CSS allows you to apply different styles for different media types or devices. It's particularly useful for creating responsive designs that adapt to various screen sizes, resolutions, or other media characteristics. The @media rule is used in combination with media queries to conditionally apply styles. Here are some things you
1 min read
three90RightbarBannerImg