In modern web development, using multiple CSS classes on a single HTML element is a powerful and common practice. It allows you to reuse styles, combine functionality, and keep your CSS modular.
In this guide, you’ll learn how to apply two or more CSS classes to a single HTML element, how it works, and why it’s good practice in real-world projects.
✅ The Syntax: How to Apply Multiple Classes
To apply two or more CSS classes to an element, simply separate the class names with spaces in the class
attribute.
📌 Example:
<div class="box shadow">Hello World</div>
Here:
box
is one classshadow
is another class- Both are applied at the same time
✅ Step-by-Step Example
🔹 HTML:
<button class="btn primary">Click Me</button>
🔹 CSS:
.btn {
padding: 10px 20px;
font-size: 16px;
}
.primary {
background-color: blue;
color: white;
}
✅ Result:
The button has both padding (from .btn
) and a blue background (from .primary
).
🧠 Why Use Multiple Classes?
Using multiple classes helps you:
- Reuse common styles (
.btn
,.text-center
,.card
) - Keep CSS modular and maintainable
- Add conditional styling based on logic (e.g.,
.active
,.error
)
🧪 Advanced: Combine Classes in CSS Selectors
You can also target elements with multiple classes in your CSS.
📌 Example:
.card.featured {
border: 2px solid gold;
}
This rule only applies when an element has both .card
and .featured
.
❌ Don’t Use Commas or Dots in HTML Class Attribute
Many beginners mistakenly try:
<div class="box, shadow"> <!-- ❌ Invalid -->
or
<div class=".box .shadow"> <!-- ❌ Invalid -->
✅ Correct syntax: Use space-separated class names only.
<div class="box shadow"> <!-- ✅ Correct -->
🧾 Conclusion
To apply multiple CSS classes to an element, use a space-separated list in the class
attribute. This allows for clean, flexible, and scalable CSS architecture.
Pro Tip: Combine utility classes (like in Tailwind or Bootstrap) with custom classes for maximum efficiency.