22

How to select the above element of an element in CSS using selectors

<li class="content">One</li>
<li class="content">Two</li>
<li class="content">Three</li>
<li class="no-content">First</li>
<li class="content">Four</li>
<li class="content">Five</li>
<li class="content">Six</li>

Here I want to use the class no-content, so that I can get its above element <li class="content">Three</li>, using CSS selector.

2
  • Unfortunately I don't think that's possible yet. Same question: stackoverflow.com/questions/1817792/… Edit: If JS is an option it's obviously quite easy: $('li.no-content').prev().addClass('prev') Commented Apr 4, 2012 at 6:58
  • 2
    Rather than “above”, the term for the <li> before <li class="no-content"> is “preceding sibling”. Commented Apr 4, 2012 at 7:20

2 Answers 2

34

This is not possible with pure CSS...

Sign up to request clarification or add additional context in comments.

3 Comments

2017 - this is possible, see this fiddle I made. jsfiddle.net/maxshuty/cj55y33p/3
@maxshuty your fiddle demonstrates the general sibling combinator, only finding siblings listed after the target! In fact, if you take out all the .top css (including the ~s you have used) the fiddle works the same! You have over complicated a wrong answer!
Using axe syntax, the selector .no-content ? .content would select <li class="content">Three</li> in the example above. See: github.com/RouninMedia/axe
-1

Are you looking the same:- http://jsfiddle.net/rohitazad/4Y9D5/3/

you can select to bottom div but not select to top .

5 Comments

Please check http://jsfiddle.net/4Y9D5/2/, Here I am just add a class to above element, actually it is not possible, but need like...
Hi you are select to bottom div as like this jsfiddle.net/rohitazad/4Y9D5/3
No, please check link that I have already given. I need like that, but without adding the class to preceding sibling, I want to select the preceding sibling using selector no-content...
now check to this you may define .test backgroundcolor as like this jsfiddle.net/rohitazad/4Y9D5/8
I can't write class test there, its only for you to understand what I need. Is it possible to select the test class node to be selected with the class no-content...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.