1

I'd like to select the last <p> but only if the next sibling is NOT an <ul>

<!-- Select this p element -->
<div>
    <p>...</p>
    <img>
</div>

<!-- Do NOT this p element -->
<div>
    <p>...</p>
    <ul>...</ul>
</div>

This is not working:

<style>
    div p:last-of-type, div p:not(+ul) {
    ...
}
</style>
sansSpoon
  • 2,115
  • 2
  • 24
  • 43

3 Answers3

0

There's no way to do this with pure CSS, if you really must select the next sibling NOT <ul>, you're going to have to resort of Javascript.

ferhado
  • 2,363
  • 2
  • 12
  • 35
0

As per my understanding there is no way to select such selector using only CSS. You should need some script to do it. Check below snippet which helps with simple jquery.

$(document).ready(function() {
  $('p').each(function() {
    if (!$(this).next().is('ul')) {
      $(this).addClass('color-red');
    }
  });
});
.color-red {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <p>Some text</p>
  <ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
  </ul>
</div>
<div>
  <p>Some text</p>
  <span>Some more text</span>
</div>
RaJesh RiJo
  • 4,302
  • 4
  • 25
  • 46
0
<div class="all">
<div>
    <p>1sst</p>
    <img>
</div>

<!-- Do NOT this p element -->
<div>
    <p>last p</p>
    <ul>...</ul>
</div>
</div>
<script>
$('.all div').children('p').last()
</script>
Xigo
  • 29
  • 9