Search code examples
cssanchorhtml-lists

css, select previous anchor


<ul class="list">
    <li><a href="">Text 1</a></li>
    <li><a href="">Text 2</a></li>
    <li><a href="" class="selected">Text 3</a></li>
</ul>

how do I select the anchor for Text 2 without adding an extra class? (and no javascript)

<style type="text/css">
    ul.list li a.selected ******{background-color:#000}
</style>

output:

<li><a href="" style="background-color:#000;">Text 2</a></li>

Solution

  • It seems like you're looking for a previous sibling selector, which unfortunately doesn't exist. See this post for useful links on what is possible.