09
6
2009

CSS Selector/Target for Div Element with Multiple Classes

Overview

There may be times when you need to add a custom attribute to a div element that has multiple classes assigned such as:

<div class="class1 class2">Need to target this div</div>

But you may be wondering how to write a new css selector to only target the elements that have both of those same classes as an attribute.

Solution

Just write the classes together without a space…

.class1.class2{background: #ccc;}

Get New Articles by Mail

Internet Marketing Social Media Web Design

Your email:

 

Comments

Speak Your Mind


  1. Additionally, the selectors don’t need to be in the same order as they are in the element’s class attribute.

    .class2.class1 {…} will work in exactly the same way.

    VA:F [1.1.2_437]
    Rating: 0.0/5 (0 votes cast)

    [Reply]

    Justin Blough
    Justin Blough Reply:

    Thanks for adding the additional insight Ryan

    VN:F [1.1.2_437]
    Rating: 0.0/5 (0 votes cast)

    [Reply]

Let It Out

Join the Conversation