Reputation: 15836
I need to build a responsive accordion using pure css solution , the scenario is illustrated in images , the only different thing from default accordions , it that user can keep both tabs open ,and they should split screen into 2 halves.
can this be achieved using pure flex solution such that when any div content is hidden , the div should shrink automatically ?
Upvotes: 2
Views: 3579
Reputation: 123377
Codepen: http://codepen.io/anon/pen/LVmNwQ
This example relies on two hidden checkbox
to show and close the accordion, so it can work using only CSS. Shrink and expand labels depend on the checkbox :checked
status
HTML
<section>
<input type="checkbox" id="dcb1" />
<h2><label for="dcb1">Div #1 Control bar</label></h2>
<div>Text of div #1</div>
<input type="checkbox" id="dcb2" />
<h2><label for="dcb2">Div #2 Control bar</label></h2>
<div>Text of div #2</div>
</section>
CSS
* {
box-sizing: border-box;
}
section {
height: 100vh;
display: flex;
flex-direction: column;
}
input[type="checkbox"] {
position: absolute;
z-index: -1;
}
h2 {
margin : 0;
padding : 10px 20px;
width : 100%;
height : auto;
overflow : hidden;
color : #fff;
background : #0085b2;
cursor : pointer;
}
div {
background: #25c8ff;
height: 0;
overflow: hidden;
}
input:checked + h2 + div {
height: auto;
padding: 20px;
flex-grow : 1;
}
label:after {
float: right;
}
input + h2 label:after { content: "expand" }
input:checked + h2 label:after { content: "shrink" }
Upvotes: 2