Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
SCSS how do I stop styling multiple <p> tags in a row when an <img> follows

Basically, I have a list of

tags from a WYSIWYG editor that have a margin-bottom of 20px. However, if the sibling of the

tag has an tag, I would like to remove that margin so that the tag can set the margin. How would I do this?

This is my styling

.wysiwyg {
p {
    margin-bottom: 20px;
}
}

Basic markup

<div class="wysiwyg">
    <p></p>
    <p></p>
    <p></p>
    // Styling must stop here so that there is no bottom margin from the first above <p> tag
    <img />
    <p></p>
    <p></p>
    <p></p>
</div>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use tilde ~, it will select all element after the current selector, and then reset the rule of all p after the img:

.wysiwyg {
  p {
    margin-bottom: 20px;
  }
  img ~ p {
    margin-bottom: 0;
  }
}

Here a demo

.wysiwyg p {
  margin-bottom: 20px;
  background-color: red;
}

.wysiwyg img ~ p {
  margin-bottom: 0;
  background-color: transparent;
}
<div class="wysiwyg">
    <p>1</p>
    <p>2</p>
    <p>3</p>
    <img />
    <p>4</p>
    <p>5</p>
    <p>6</p>
</div>

More information can be found here

about 4 years ago · Juan Pablo Isaza Report

0

Just set a negative margin-top for img tags that are preceded by a p tag:

.wysiwyg {
    p {
        margin-bottom: 20px;
    }
    p + img {
        margin-top: -20px;
    }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!