Add margin-bottom to Image Before footer

I’m trying to see if there’s a way to add a margin-bottom to an img element only if it’s followed by a footer element. At the moment I’m using a special class (image-before-footer) on my last img element to acheive this, but was wondering if there’s a way to do it dynamically in CSS?


<div class="container">
  <h1>Title goes here</h1>
  <img src=""/>
  <img class="image-before-footer" src=""/>
  <footer class="footer">
    <p>Here are my footnotes</p>


article {
  margin: 3em 0;

img {
  width: 100%;
  vertical-align: middle;
  margin: 0;

.image-before-footer {
  margin-bottom: 3em;

Any help with this is appreciated. Thanks in advance!


Since you can’t select previous sibling elements in CSS, why not just add a margin-top to the footer element if it follows an img element using the adjacent sibling combinator, +:

Example Here

img + footer {
  margin-top: 3em;

You could also just select the last img element using the :last-of-type pseudo class.

Of course, this assumes that the last img element will be succeeded by the footer, though.

Example Here

img:last-of-type {
  margin-bottom: 3em;