可折叠面板关闭时,Div高度不为零

时间:2017-02-07 08:27:51

标签: html css

我有以下面板,当点击隐藏复选框的标签时显示它的内容,并在再次点击时隐藏它们。



.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>
&#13;
&#13;
&#13;

然而,当关闭时,面板高度不为零;它是20px。

折叠后如何重构面板高度降至零?

2 个答案:

答案 0 :(得分:5)

font-size:0;设置为.collapse-panel,因为它包含inline-block个元素,内联框会从其块父元素继承可继承的属性,并创建空格/边距。 font-size:0;将移除这些空间。

&#13;
&#13;
.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;
}
&#13;
<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>
&#13;
&#13;
&#13;

<强> Source

答案 1 :(得分:0)

你可以像这样制作面板的高度:如果我的问题是正确的。

.collapse-panel > input ~ div.collapse-panel-content {
    height: 0;
}

&#13;
&#13;
.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 ~ div.collapse-panel-content {
    height: 0;
}
.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;
}
&#13;
<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>
&#13;
&#13;
&#13;