0

In my wordpress post I have image and text mixed. I have the following code which targets content right AFTER an image to apply specific css.

#single-pg p{padding:0 20px; margin-top:0;}

#single-pg img + p{ margin-top:30px; background:red;}

#single-pg img + h1{ background:blue;}

#single-pg img + blockquote{ background:orange;}

I want to be able to do similar with content BEFORE an image (ex. apply margin-bottom: 30px to p) but only when its the last element before an image

Targeting its parent I don't think would solve the issue. I just want to target whatever element comes just before the image, I just cannot pre-determine what that is

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3550879
  • 3,389
  • 6
  • 33
  • 62
  • 1
    There is no previous sibling selector (yet) in CSS – j08691 Jul 05 '17 at 20:09
  • 1
    In short: you can't, at least not with just CSS. You can go deeper (form parent to child), but not the other way; you can go to the next, but not the previous. – domsson Jul 05 '17 at 20:10
  • Possible duplicate of [Is there a CSS parent selector?](https://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector) – sorayadragon Jul 05 '17 at 20:15

1 Answers1

1

You can't target a previous element, but as a workaround (if margins is what you want) you could apply the margin to the img if it follows a p

#single-pg p + img{margin-top:30px;}
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317