CSS selector for class descendant within a class

前端 未结 3 1167
予麋鹿
予麋鹿 2020-12-12 01:23

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

相关标签:
3条回答
  • 2020-12-12 02:02

    Yes it is possible:

    Direct Descendants:

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

    All Descendants:

    .a .b { /* ... */ }
    
    0 讨论(0)
  • 2020-12-12 02:11

    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:

    • `CSS Selectors.
    0 讨论(0)
  • 2020-12-12 02:16

    For sure, it's a simple CSS selector:

    .classA .classB {}
    
    0 讨论(0)
提交回复
热议问题