Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select an element that has no leading or trailing text nodes?

Tags:

css

I experimented with using the :only-child pseudo-class but unfortunately this does not seem to consider the text nodes:

<style type="text/css">
  div span:only-child {
    color: red;
  }
</style>

<div>
  Test
  <span>This still becomes red :(</span>
</div>

<div>
  <span>This becomes red, as it should!</span>
</div>
<div>
  <span>This does not become red - great!</span>
  <span>This does not become red - great!</span>
</div>

I am trying to find a way to detect when a specific element is completely alone within its container element in a situation where I am unable to introduce new classes.

Is there a way to achieve this with CSS?

like image 539
Lea Hayes Avatar asked Oct 18 '22 06:10

Lea Hayes


1 Answers

Is there a way to achieve this with CSS?

Unfortunately, not.

Included in an old revision of the CSS Working Group "mistakes" list is missing the idea that..

No naked text mixing with elements. All raw text should have an addressable, stylable element wrapping it, created by CSS if necessary.

Current list

Text Nodes are not element and CSS can't select (or ignore) elements that don't exist.

So, it's probably best practice to always use a text element when incorporating text in a page...you never know when you might need to style it.

like image 98
Paulie_D Avatar answered Nov 01 '22 13:11

Paulie_D