alexchenco alexchenco - 3 months ago 17
CSS Question

How to hide a last element that has a div after it with CSS?

In other words:

<div>
<a>
<a>
<a> // how to hide this?
<div>
<div>

<div>
<a>
<a> // but not this?
<div>
<a> // and not this?
<div>


In other words, how to hide the last anchor tag when there's a div after it? (Or how to hide the preceding element if there's a final
div?)

Answer

if I understood your markup correctly, you can use div:first-of-type > a:last-of-type

a {
  display: block
}
div:first-of-type > a:last-of-type {
  color: red
}
<div>
  <a>a</a>
  <a>a</a>
  <a>a</a> <!-- how to hide this? -->
  <div>
  </div>
</div>

<div>
  <a>b</a>
  <a>b</a><!-- but not this? -->
  <div></div>
  <a>b</a> <!--or this?-->
</div>