html

Can I Select Elements That Have Multiple Classes?

A CSS selector selects the HTML element to apply some CSS properties. It includes id, class, pseudo-elements, etc. By doing this, the same styles can be implemented to more than one class that save developer’s effort. This article demonstrates the steps to choose elements having more than one class in CSS.

Can I Select Elements That Have Multiple Classes?

Yes, users can select HTML elements that have the same classes and then assign CSS properties to them. This can be done by writing all the class names on the same line separated with a comma and then simply assigning the properties. Let follow the below steps:

Step 1: Create Div Elements in the HTML File

In the HTML file, create three sibling div elements and assign two div’s same class. Inside each div create a “<h1>” element:

<div style="margin-left:20px">

<div class="heading1">

<h1>Hello</h1>

</div>

<div>

<h3>Welcome</h3>

</div>

<div class="heading2">

<h1>to the Linuxhint</h1>

</div>

</div>

Step 2: Add CSS Properties

Inside the “<style>” tag, apply CSS properties to “heading1” and “heading2” classes. For the sake of example, different “font colors” are set to the element of both classes:

<style>

.heading1

{

color: red;

}

.heading2

{

color: blue;

}

</style>

After executing the above code, the webpage looks like this:

The output confirms that styles have been applied to both classes.

Step 3: Applying CSS Properties to Multiple Classes

Now, write all classes on a single line on which the same styles need to apply. In this step, two classes having the same styles are applied. Make sure there is no extra space, and each class name is separated via a “comma”:

<style>

.heading1

{

color: red;

}

.heading2{

color: blue;

}

.heading1,.heading2

{

font-size:30px; font-family:Times New Roman;

}

</style>

The “font-size” property sets the font size to 30px, and “font-family” makes the font style set to “Times New Roman” for better visualization.

The output looks like this:

That’s how users can select elements that have multiple classes and assign them CSS properties at once.

Conclusion

To select multiple HTML elements at once and apply CSS properties to it. First, assign HTML elements classes and in the “<style>” tag. After that, write those classes on the single line that are divided with “comma (,)”. In this way, users can assign CSS properties at once. This article has successfully demonstrated how users can choose elements having multiple classes.

About the author

Abdul Moeed

I'm a versatile technical author who thrives on adaptive problem-solving. I have a talent for breaking down complex concepts into understandable terms and enjoy sharing my knowledge and experience with readers of all levels. I'm always eager to help others expand their understanding of technology.