0

Can I select elements which do not contain certain other element with a CSS selector ?

Example: I want to select div 1 and 2, but not 3

<div>Text1</div>                           // <-- #1
<div>Text2</div>                           // <-- #2
<div><button>RELOAD</button></div>         // <-- #3
zaggi
  • 678
  • 4
  • 20
  • 1
    Unfortunately, until the [`:has()`](https://drafts.csswg.org/selectors-4/#relational) (selectors level 4) selector becomes available (if it ever does) I don't think that this is possible with just CSS; but with `:has()`, it would be something like `div:not(:has(button))`, I think. – David says reinstate Monica Oct 21 '18 at 18:08
  • You can't select (hence style) parents or previous siblings. CSS only parses forwards, never backwards. If it ever did, the internet would crawl to a standstill and you could hear the sound of browsers crashing. – tao Oct 21 '18 at 18:18
  • @AndreiGheorghiu please explain what you mean ? I need not the selection of the child, but the container element (maybe you refer to it as parent). How that would damage the browser (if that is what you mean). Thank you – zaggi Oct 25 '18 at 04:33
  • The answer to your initial question is *"Not using CSS."*. The answer to *"Why?"* is: *"Because the vast majority of technical experts have agreed, every time it was brought up, that making it possible for CSS to traverse the DOM backwards would be a terrible idea (sometimes compared to giving a loaded gun to an infant)"*. Anyways, it's not something you can ask on SO. Over here you can ask: — "Can I?" — *"Yes."* — "How?" — *"JavaScript."* — "But, CSS...!?" — *"Nope."* – tao Oct 25 '18 at 05:48

0 Answers0