Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting an element that doesn't have a child with a certain class

The structure of my HTML is like so

<div>
    <div>
        <h1>Something</h1>
    </div>
    <div>
        <h1 class='Handle'>Something</h1>
    </div>
</div>

In the event that the div > div does not have a child with the class "Handle" I want the the div > div to have the style cursor:move;. How would I go about doing this in pure CSS, is it even possible?

like image 206
user1763295 Avatar asked Aug 12 '13 14:08

user1763295


People also ask

What is the class used to select an element which has no child?

The :empty CSS pseudo-class represents any element that has no children.

How do you select an element that is not a class?

In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector. Since it is used to prevent a specific items from list of selected items.

How could you use a selector to choose a specific element with a specific class?

class selector selects elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class.

How do you select an element with an attribute class?

The [attribute^="value"] selector is used to select elements with the specified attribute, whose value starts with the specified value. The following example selects all elements with a class attribute value that starts with "top": Note: The value does not have to be a whole word!


2 Answers

No browser currently supports this

This would work, except no browser currently supports it as far as I am aware. jQuery does though, however.

div > div:not(:has(h1.Handle)) {
    cursor: move;
}
like image 84
Damien Bezborodow Avatar answered Oct 04 '22 06:10

Damien Bezborodow


There is no parent selector in CSS, so what you are asking is not possible. What you can do is put the cursor:move on every h1 that doesnt has the class "Handle" by using the attribute selector.

h1:not([class=Handle]) {
    cursor:move;
} 

http://jsfiddle.net/4HLGF/

Another option is to adjust your HTML, and move your h1 on the same level as the div.

<div>
    <h1>Something</h1>
    <div>
        dragable content
    </div>
    <h1 class='Handle'>Something</h1>
    <div>
        non dragable content
    </div>
</div>

Now you can do the same check on the h1, and target the div that comes after it.

h1:not([class=Handle]) + div {
    cursor:move;
}

http://jsfiddle.net/4HLGF/2/

like image 23
koningdavid Avatar answered Oct 04 '22 05:10

koningdavid