Mike Sav
Mike Sav

Reputation: 15311

Selecting adjacent and previous siblings in CSS

Say for instance I have the following HTML

<ul>
  <li><a href="a">A</a></li>
  <li><a href="b">B</a></li>
  <li class="myclass"><a href="c">C</a></li>
  <li><a href="d">D</a></li>
  <li><a href="e">E</a></li>
</ul>

Now I can detemine the next sibling to the li with the class="myclass" by using the following css .someclass + li {}. However if I wanted to apply a CSS rule to previous li, is it possible to do this using css rather than jQuery's $("#some-id").prevAll() or writing some JavaScript to find this?

I'm not that confident as I've never heard of this and my searching hasn't provided a solution.

Thanks

Upvotes: 3

Views: 359

Answers (2)

Jon
Jon

Reputation: 437664

There is no selector that does that even in the CSS3 specification; you will either have to be creative in assigning classes to the elements and using other selectors, or go with jQuery.

Upvotes: 0

Richard Tuin
Richard Tuin

Reputation: 4562

Nope, in CSS you can't 'traverse' the dom structure. You can only inherit style definitions recursively.

Upvotes: 4

Related Questions