How to hide the first and last element of three identical elements
I have three divs with the same class name, I want to hide the first and last div.
My code:
<div class="clsname">div1</div>
<div class="clsname">div2</div>
<div class="clsname">div3</div>
Now I want to hide div1 and div3. Actually I want this output in the browser:
div2
How can i do this?
To clarify, I want something like this: (this will only hide div3)
div.clsname + div.clsname + div.clsname {display:none;}
+3
user4920811
source
to share
5 answers
Here is a solution that targets only three divs with a class and leaves the rest of the divs alone. (So it works either with or without other divs in between.)
div.clsname, div.clsname ~ div.clsname ~ div.clsname {display:none}
div.clsname ~ div.clsname {display:block}
<div>Other div, displayed normally</div>
<div class="clsname">div1</div>
<div>Other div, displayed normally</div>
<div>Other div, displayed normally</div>
<div class="clsname">div2</div>
<div>Other div, displayed normally</div>
<div>Other div, displayed normally</div>
<div>Other div, displayed normally</div>
<div class="clsname">div3</div>
<div>Other div, displayed normally</div>
<div>Other div, displayed normally</div>
<div>Other div, displayed normally</div>
<div>Other div, displayed normally</div>
Compatible with all modern browsers and even IE8.
+1
source to share