White Maskers White Maskers - 29 days ago 11
CSS Question

How it's possible add style on hover if element is an immediate previous of that element?



a:hover + ul{
display:none;
}

<li>
<a>hover me</a>
<ul> hide me</ul>
</li>
<li>
<p>change my color</p>
<ul>hover me</ul>
</li>





I know that it's possible to add style to a particular element using '+' if it's the immediate next element(as shown as above).Is it possible to apply hover even if the element is immediate previous ?

Answer

That is not possible with css selectors but you could use kind of hack to change order of elements with Flexbox and use + selector.

div {
  display: flex;
  flex-direction: column;
}
strong:hover + p {
  color: blue;
}
p {
  order: -1;
}
<div>
  <strong>hover me</strong>
  <p>change my color</p>
</div>