A tricky CSS selector question, don't know if it's even possible.
Lets say this is the HTML layout:
<div></div>
<div></div>
<div></div>
<div style="display:none"></div>
<div style="display:none"></div>
I want to select the last div
, which is displayed (ie. not display:none
) which would be the third div
in the given example.
Mind you, the number of div
s on the real page can differ (even the display:none
ones).
The real answer to this question is, you can't do it. Alternatives to CSS-only answers are not correct answers to this question, but if JS solutions are acceptable to you, then you should pick one of the JS or jQuery answers here. However, as I said above, the true, correct answer is that you cannot do this in CSS reliably unless you're willing to accept the
:not
operator with the[style*=display:none]
and other such negated selectors, which only works on inline styles, and is an overall poor solution.This worked for me.
I think it's not possible to select by a css value (display)
edit:
in my opinion, it would make sense to use a bit of jquery here:
in other way, you can do it with javascript , in Jquery you can use something like:
*reedited
It is not possible with CSS, however you could do this with jQuery.
JSFIDDLE DEMO
jQuery:
HTML:
CSS:
jQuery (previous solution):
There is a pure css solution (CSS3) if your div is hidden using "style" attribute
Note the space between "display:" and "none", if you can't say exactly if there is a space in the attribute or not, you can do the selector like this:
jQuery is king, but CSS3 solutions are god