Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select class that does not begin with string

I want to select a child element that does not contain a class that begins with z-depth-:

<div class="well">
    <div class="well"></div>
</div>

So that if the inner .well also contained a class like z-depth-1 it would not be selected.

This isn't working because the inner .well is always selected:

.well .well:not([class^="z-depth-"])

Is that even possible?

like image 862
rzb Avatar asked Feb 20 '17 19:02

rzb


People also ask

How do you exclude a class in CSS?

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 do you select an element with a specific class?

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. To do this, start with the element name, then write the period (.)

How do I target my CSS ID?

To use an ID selector in CSS, you simply write a hashtag (#) followed by the ID of the element. Then put the style properties you want to apply to the element in brackets.

How do you select in CSS?

The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.


1 Answers

You can't select a child element that does not contain a class that begins with z-depth- with CSS, you can only:

  1. Select all the child elements whose class attribute's values don't start from z-depth- substring:

.well .well:not([class^="z-depth-"]) {
    color: red;
}
<div class="well z-depth-1">Parent div
    <div class="z-depth-2 well">First child div</div>
    <div class="well z-depth-3">Second child div</div>
</div>
  1. Select all the child elements whose class attribute's values don't contain z-depth- substring:

.well .well:not([class*="z-depth-"]) {
    color: red;
}
<div class="well z-depth-1">Parent div
    <div class="z-depth-2 well">First child div</div>
    <div class="well z-depth-3">Second child div</div>
    <div class="well">Third child div</div>
</div>

You also could read more about all CSS Selectors on MDN.

like image 169
sergdenisov Avatar answered Sep 28 '22 20:09

sergdenisov