So I know all this can be done in Javascript without much difficulty, but wondered if, in the end it could all be done with CSS. I tried looking at a number of entries on here, many say ‘no’– but they are also quite old (circa 2014) so I don’t know if the CSS standard has been updated significantly since then.
I’ve two classes .arrow and .slide. I’ve done the checkbox hack to be able to activate .slide on a click by #test:checked + .slide.
In part I am wondering if it is possible to chain more than one CSS Selector together, or does that not work ? I.e. something like #test:checked + .slide + .arrow2 + etc..
The specific ‘second’ action I wish to occur has presently been stuffed away in .arrow:active, because I wasn’t sure how else to demonstrate it.
Go over to the right to the arrows, do a long hold click and you will see the state change I want to happen. I mean if you click the arrows twice, .slide goes in and out just fine.
I’m just not sure how to achieve the same state change with .arrow, or if it is possible to pull both into one event without resorting to JS.
Outta ideas…
.arrow {
width: 19px;
height: 32px;
float: right;
padding-left: 20px;
background-image: url("http://www.allpointsbulletin.info/assets/img/ArrowDown.png");
transition: background-image 0.5s ease-in-out;
}
.arrow:active {
background-image: url("https://www.allpointsbulletin.info/assets/img/ArrowUp.png");
}
.slide {
clear:both;
width:10%;
height:0px;
overflow: hidden;
text-align: center;
transition: height 1.5s ease;
list-style-type: none;
}
.slide li {
padding : 30px;
}
#test {
position: absolute;
opacity: 0;
height: 0px;}
#test:checked + .slide {height: 500px;}
<label for="test"><h4>See more info<div class="arrow"></div></h4></label>
<input type="checkbox" id="test">
<ul class="slide">
<li><a href="#">Lorem Ipsum</a></li>
<li><a href="#">Lorem Ipsum</a></li>
<li><a href="#">Lorem Ipsum</a></li>
<li><a href="#">Lorem Ipsum</a></li>
</ul>
2
Answers
It is possible using CSS. You need to use
:has()
pseudo selector for that and also use a parent container. The changes:And you don’t need
.arrow:active
pseudo selector anymore.Note: This
:has()
pseudo selector has limited browser support.Place the checkbox before everything because the flow of an adjacent sibling combinator is left to right on the CSS selector syntax and HTML element directly ahead of the previous HTML element.
Details are commented in example