Languages
[Edit]
EN

CSS - style only elements with multiple classes

3 points
Created by:
Palpys
764

In this article, we would like to show you how to style only those elements that have multiple classes using CSS.

Quick solution:

.class-1.class-2.class-N {  /* <div class="class-1 class-2 class-N"></div> */
    color: red;
}

Where:

  • class-1, class-2, ... , class-N are class names assigned to the HTML element at once.

 

Practical example

In this example, we present how to style only those elements, that have assigned both class-1 and class-2 to them.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    .class-1.class-2 {
        background: yellow;
    }

  </style>
</head>
<body>
  <div class="class-1 class-2">Element with "class-1" and "class-2"</div>
  <div class="class-1">Element with "class-1"</div>
  <div class="class-2">Element with "class-2"</div>
</body>
</html>

 

See also

  1. JavaScript - get handle to HTML element with multiple classes

Alternative titles

  1. CSS - style only those elements that have multiple classes
  2. CSS - style only elements with two or more classes
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join