Trusted answers to developer questions

How to use multiple classes on the same element in CSS

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

CSS (Cascading Style Sheets) define a webpage element’s appearance by hooking into the attributes that are applied to that element. These attributes can be either an ID or a class and, like all attributes, they must add helpful information to the elements that they are assigned to.

An element is usually only assigned one class. The corresponding CSS for that particular class defines the appearance properties for that class.

However, we can also assign multiple classes to the same element in CSS. In some cases, assigning multiple classes makes page styling easier and much more flexible.

Syntax

To assign multiple classes to an element, separate each class with a space within the element’s class attribute.

<p class = "firstclass secondclass thirdclass"> Some text... </p>

Code

The first div gets the properties of class1, the second div gets those of class2, while the third gets the properties of both class1 and class2.

RELATED TAGS

css
classes
multiple
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?