2

I would change the CSS rule for the background to li.li-class if it contain an h3. I have an HTML like this:

<div class="div-class">
    <ul>
        <li class="li-class">
            <span itemprop="something">
                <h3>Some title</h3>
            </span>
        </li>
    </ul>
</div>

and the CSS for li background is:

.div-class ul li:nth-child(2n+1) {  background: #f4f4f4; }

Where is the CSS selector for the li.li-class?

I have look this question Is there a CSS parent selector? and i try the code, but not work in this case. Thanks all

Community
  • 1
  • 1
kampageddon
  • 1,269
  • 13
  • 13
  • 5
    Not possible. There's no such thing – roberrrt-s Nov 23 '16 at 20:24
  • 2
    The question you link to explicitly states that the behaviour you desire is not possible, unfortunately. (For posterity: http://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector) – Serlite Nov 23 '16 at 20:24
  • would you perhaps be creating this html/css with a server side language? if so, you could test for the h3 contents and add a class to the li. – WEBjuju Nov 23 '16 at 20:25
  • Thank you @Serlite , you think this is possible by javascript? – kampageddon Nov 23 '16 at 20:36
  • Absolutely possible by JS! Jake gives a pretty good suggestion in his answer (if you're not adverse to jQuery, of course). – Serlite Nov 23 '16 at 20:38
  • Thanks @WEBjuju , yes the HTML code is created by php, but I do not believe it is possible to give a CSS rule, because I do not know which of the
  • must have the CSS rule. I am wrong?
  • – kampageddon Nov 23 '16 at 20:40
  • @Serlite i like jQuery, :-) but I thought I did not use it to be lighter on the web page. But i think it is a good idea! Thanks!!! – kampageddon Nov 23 '16 at 20:47
  • @kampageddon If this would be the only thing you loaded jQuery for, then maybe it would be a bit much - for a pure JS solution, you can try out [`Node.contains`](https://developer.mozilla.org/en-US/docs/Web/API/Node/contains). – Serlite Nov 23 '16 at 20:53