user1524238
user1524238

Reputation: 31

CSS selector for class descendant within a class

Is it possible to create a CSS selector for 'element with class b, which is a descendant of an element with class a'?

Thanks, Rasto

Upvotes: 3

Views: 2339

Answers (3)

David Thomas
David Thomas

Reputation: 253318

Given the mark-up:

<div class="elementClassA">
    <div class="elementClassB">first B element</div>
</div>
<div class="elementClassA">
    <div class="elementClassC">first C element
        <div class="elementClassB">Second B element</div>
    </div>
</div>​

Yeah, for all descendants:

.elementClassA .elementClassB {
}

The above will target both the first B element and the Second B element: JS Fiddle demo.

For immediate descendants:

.elementClassA > .elementClassB {
}

This will target only the first B element: JS Fiddle demo.

References:

Upvotes: 3

Dmitri Farkov
Dmitri Farkov

Reputation: 9671

For sure, it's a simple CSS selector:

.classA .classB {}

Upvotes: 0

Alex
Alex

Reputation: 35409

Yes it is possible:

Direct Descendants:

.a > .b { /* ... */ }

All Descendants:

.a .b { /* ... */ }

Upvotes: 6

Related Questions