html - Using CSS to change a class background but only if it is not after a h4 -
i trying change background colour of paragraph 4 only. want leave paragraph 2 alone (because after h4). have tried not selector can't seem logic working right. not wanting use javascript, php or jquery. pure css please.
.widget-wrap > .widget-title { background-color: yellow; } .widget-title + .textwidget { background-color: red; } <div class="widget-wrap"> <h4 class="widget-title">paragraph 1 in div.</h4> <p class="textwidget">paragraph 2 in div.</p> <p>paragraph 3 in div.</p> </div> <div class="widget-wrap"> <p class="textwidget">paragraph 4 inside 2nd div.</p> <p>paragraph 5 inside 2nd div.</p> </div>
if first child of .widget-wrap either h4.widget-title, or p.textwidget (i.e. when h4 not present), use :first-child:
.widget-wrap > .widget-title { background-color: yellow; } .widget-wrap > .textwidget:first-child { background-color: red; } <div class="widget-wrap"> <h4 class="widget-title">paragraph 1 in div.</h4> <p class="textwidget">paragraph 2 in div.</p> <p>paragraph 3 in div.</p> </div> <div class="widget-wrap"> <p class="textwidget">paragraph 4 inside 2nd div.</p> <p>paragraph 5 inside 2nd div.</p> </div> if there other elements may appear before first p.textwidget absent h4.widget-title, complicate things slightly. use :not() sibling selector in case, if there can variable number of elements, won't able reliably.
Comments
Post a Comment