0

I want to put a green color to the element i that has the "valid" class next to it, using only css.

<i>Hello</i>
<input type="text" class="valid">

.valid ~ i
{
 color: #00c851 !important;
}

http://jsfiddle.net/e4tubpkv/

yavg
  • 2,761
  • 7
  • 45
  • 115
  • That may be what you want, but if the elements are placed in that order in the DOM then, to quote Mick Jagger: "*you can't always get what you want.*" There is, in CSS, no previous-sibling selector, you can, however, reorder the elements and place them - visually - in that order using CSS Grid or Flexbox. – David Thomas May 14 '19 at 14:04
  • @DavidThomas I did not know who he was, but I realize then that it is impossible? – yavg May 14 '19 at 14:05
  • Basically, you can change the order in the DOM but "play" with css to present it in reverse. (Using `float` or with `flexbox`) – Mosh Feu May 14 '19 at 14:07

1 Answers1

1

You can not do that by CSS. There is no "previous sibling" selector.

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62