gpt4 book ai didi

html - CSS 最小高度不根据内容调整大小

转载 作者:太空宇宙 更新时间:2023-11-04 07:34:30 24 4
gpt4 key购买 nike

我目前正在尝试在选择标签时显示内容。我遇到的问题是,将出现的内容的大小会根据被拉入的数据而有所不同,因此我无法设置在所有情况下都适用的特定高度。我认为解决此问题的最佳方法是使用 min-height 但高度似乎并没有根据内容大小的长度来下注。我该如何解决这个问题,即大小将取决于内容的多少。

#block {
background: yellow;
height: 0;
overflow: hidden;
-webkit-transition: height 300ms linear;
-moz-transition: height 300ms linear;
-o-transition: height 300ms linear;
transition: height 300ms linear;
}

label {
cursor: pointer;
}

#showContent {
display: none;
}

#showContent:checked+#block {
min-height: 5px;
}

#showContent:not(:checked) {
height: 0px;
}
<label for="showContent">Show content</label>
<input type="checkbox" id="showContent" />
<div id="block">
Show content ..... Bla bla bla
</div>

jsfiddle https://jsfiddle.net/sacora9/81m14v1s/14/

最佳答案

只需将高度设置为auto。否则,您设置了 min-height:5px,但高度仍设置为 0,因此计算出的高度将为 5px。所以解决方案是释放 height 并使其自由:

#block {
background: yellow;
height: 0;
overflow: hidden;
-webkit-transition: height 300ms linear;
-moz-transition: height 300ms linear;
-o-transition: height 300ms linear;
transition: height 300ms linear;
}

label {
cursor: pointer;
}

#showContent {
display: none;
}

#showContent:checked+#block {
height : auto; /* <----- Here, instead of min-height: 5px */
}

#showContent:not(:checked) {
height: 0px;
}
<label for="showContent">Show content</label>
<input type="checkbox" id="showContent" />
<div id="block">
Show content ..... Bla bla bla
</div>

编辑

我刚刚注意到动画部分。从 0 到 auto 的转换很棘手,但这是诀窍:

#block {
background: yellow;
overflow: hidden;

max-height: 0;
transition: max-height 0.3s cubic-bezier(0,1,0,1);
}

label {
cursor: pointer;
}

#showContent {
display: none;
}

#showContent:checked+#block {
max-height: 1000px;
transition: max-height 0.3s cubic-bezier(1,0,1,0);
}

#showContent:not(:checked) {
height: 0px;
}
<label for="showContent">Show content</label>
<input type="checkbox" id="showContent" />
<div id="block">
Show content ..... Bla bla bla
</div>

关于html - CSS 最小高度不根据内容调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49072705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com