Peter Peter - 4 years ago 84
CSS Question

Div height not zero when collapsible panel closed

I have the following panel that shows it's contents when the label for the hidden checkbox is clicked and hides them when clicked again.



.collapse-panel {
margin-bottom: 20px;
border-bottom: 2px solid darkblue;
}

.collapse-panel > label {
display: block;
position: relative;
top: -5px;
height: 2px;
}

.collapse-panel > input {
display: none;
}

.collapse-panel > label:before {
content: 'vvv';
float: left;
padding: 2px;
border: 1px solid darkblue;
border-radius: 5px;
font-family: "Lucida Console", Monaco, monospace;
font-size: 8pt;
font-weight: bold;
color: deepskyblue;
background-color: white;
cursor: pointer;
}

.collapse-panel div.collapse-panel-content {
display: inline-block;
height: 0;
margin: 0;
overflow: hidden;
white-space: nowrap;

transition-property: all;
transition-duration: 1.0s;
transition-timing-function: ease-in-out;
}

.collapse-panel > input:checked ~ div.collapse-panel-content {
height: 25px;
}

.collapse-panel > input:checked ~ label:before {
content: '^^^';
padding-top: 4px;
padding-bottom: 0;
font-size: 9pt;
}

div.collapse-panel-content > * {
display: inline-block;
float: right;
max-width: 30px;
margin: 5px;
}

<div class="collapse-panel">
<input type="checkbox" name="" id="collapse_panel_checkbox">
<div class="collapse-panel-content">
<div style="height: 20px; width: 20px; background-color: red;">
</div>
<div style="height: 20px; width: 20px; background-color: green;">
</div>
<div style="height: 20px; width: 20px; background-color: blue;">
</div>
</div>
<label for="collapse_panel_checkbox"></label>
</div>





However, when closed, the panel height is not zero; it is 20px.

How can I refactor to have the panel height drop to zero when collapsed?

Answer Source

Set font-size:0; to .collapse-panel, because it's containing inline-block elements, Inline boxes inherit inheritable properties from their block parent element, and creates space/margin. font-size:0; will remove those space.

.collapse-panel {
    margin-bottom: 20px;
    border-bottom: 2px solid darkblue;
    font-size:0;
}

.collapse-panel > label {
    display: block;
    position: relative;
    top: -5px;
    height: 2px;
}

.collapse-panel > input {
    display: none;
}

.collapse-panel > label:before {
    content: 'vvv';
    float: left;
    padding: 2px;
    border: 1px solid darkblue;
    border-radius: 5px;
    font-family: "Lucida Console", Monaco, monospace;
    font-size: 8pt;
    font-weight: bold;
    color: deepskyblue;
    background-color: white;
    cursor: pointer;
}

.collapse-panel div.collapse-panel-content {
    display: inline-block;
    height: 0;
    margin: 0;
    overflow: hidden;
    white-space: nowrap;

    transition-property: all;
    transition-duration: 1.0s;
    transition-timing-function: ease-in-out;
}

.collapse-panel > input:checked ~ div.collapse-panel-content {
    height: 25px;
}

.collapse-panel > input:checked ~ label:before {
    content: '^^^';
    padding-top: 4px;
    padding-bottom: 0;
    font-size: 9pt;
}

div.collapse-panel-content > * {
    display: inline-block;
    float: right;
    max-width: 30px;
    margin: 5px;
}
<div class="collapse-panel">
    <input type="checkbox" name="" id="collapse_panel_checkbox">
    <div class="collapse-panel-content">
      <div style="height: 20px; width: 20px; background-color: red;">
      </div>
      <div style="height: 20px; width: 20px; background-color: green;">
      </div>
      <div style="height: 20px; width: 20px; background-color: blue;">
      </div>
    </div>
    <label for="collapse_panel_checkbox"></label>
</div>

Source

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download