How to only target text content of element?

I have a div that contains a number of spans as its children and also a bit of text. The text is not surrounded by any tags, something which I cannot change. How can I target (i.e. style) only the text contents of the div, but leave any children of the div untouched? (I hope I'm not overlooking something very obvious here...)
5 Replies
Jochem
Jochem10mo ago
only thing I can think of is to style the div, and then reverse the styling on the spans .divselector > span should target all of the spans as direct children of the div you're targeting
Victor Odoi
Victor Odoi10mo ago
Depends on how you want the content placed. Ideally, div's aren't used to hold text content or at least I don't do it that way. Either you use the above solution or you wrap the spans in another container.
Zoë
Zoë10mo ago
Any styles you apply to the div you can do that style initial before applying other styles, but it's probably a structure issue
Zoë
Zoë10mo ago
https://codepen.io/z-/pen/BabjXPm/02dc66e57d789af76a7792f1bc7a0e80?editors=1100 Just put the text node in a span, it'll work the same except possible
Zed Dash
CodePen
BabjXPm
...
clevermissfox
clevermissfox10mo ago
If you can’t change the markup (at least that’s what I’m understanding as the issue) I would override the divs innerHTML to set the content in p tags or spans with classes and then set the text content/styles in there
Want results from more Discord servers?
Add your server