Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select HTML element with single class but do not select others with multiple classes?

Tags:

html

css

Suppose I have a component .table-view that has a .table-cell child, and I want to keep my classes clean, how do I select a .table-cell that has no other classes and is a direct descendant of table-view?

Open to any kind of advanced selector that is implemented in relatively new Webkit.

<div class="table-view">
    <div class="table-cell">
       <!-- How to select this HTML element without selecting the others? -->
    </div>
    <div class="table-cell split">
        <div class="table-cell">

        </div>
        <div class="table-cell">

        </div>
    </div>
</div>
like image 340
Wolfr Avatar asked Feb 17 '23 15:02

Wolfr


2 Answers

You can use element - attribute selector (fiddle)

.table-view > div[class=table-cell] {
  /* Styles goes here */
}
like image 71
Mr. Alien Avatar answered Apr 27 '23 05:04

Mr. Alien


You can use the direct descendant selector combined with the attribute selector to match exactly again the class attribute. The following will match all elements whose parent is .table-view where the class attribute is exactly "table-cell".

jsFiddle

.table-view > [class="table-cell"] {
    background-color:red;
}

Alternatively you can do this with the :not() pseudo-class

jsFiddle

.table-view > .table-cell:not(.split)

This will consider .table-cell elements that do not contain the .split class but may contain other classes.

like image 43
Daniel Imms Avatar answered Apr 27 '23 03:04

Daniel Imms