英文:
Can't add more items in dropdown
问题
我做了一个下拉菜单,但无法添加超过三个项目。
尽管如果我移除 overflow:hidden 标签,我可以添加更多项目,但我使用这个标签来去除项目符号和使下拉菜单打开和关闭。
英文:
I made a dropdown but can't add more than three items.
Although if I remove overflow:hidden tag I can add more items but I used this tag to remove bullets and for the dropdown to open and close.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.slide {
clear: both;
width: 100%;
height: 0px;
list-style-type: none;
text-align: center;
overflow: hidden;
transition: height .4s ease;
}
.slide li {padding : 20px;}
#touch {position: absolute; opacity: 0; height: 0;}
#touch:checked + .slide {height: 200px;}
<!-- language: lang-html -->
<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>
<li><a href="#">Lorem Ipsum</a></li>
</ul>
<!-- end snippet -->
答案1
得分: 1
Sum the line-height
(默认约为1.2倍字体大小)和 padding
(顶部/底部x2)的值,用于你的 li
元素。
你会看到只有三个 li
可以适应 .slide { height: 200px }
。
=> 1.2 * 16px * 3 + 40px * 3 = 177.6px
。第四个 li
添加了顶部内边距 20px
,总共为 197.6px
,这正好足够容纳三个 li
和第四个 li
的一些像素。
-
每个
li
所需的空间:16px(字体大小) * 1.2(行高) + 约3px(内联间距) + 2 * 20px(顶部/底部内边距)= 约62px。(Firefox DevTools 报告li.height: 22px
,不包括内边距,这证明了这个计算)。 -
.slide
的总最小高度 = 5 * 62px = 310px。
没有动画要求的话,当点击 touch
时最好使用 height: auto
...
在你的 CSS 中添加 * { outline: 1px dashed }
,你会看到它发生变化。
片段 不包括 <input id="touch">
* { outline: 1px dashed }
.slide {
clear: both;
width: 100%;
height: 200px;
list-style-type: none;
text-align: center;
overflow: hidden;
transition: height .4s ease;
}
.slide li {
padding: 20px
}
.slide.test1 { height: 310px }
.slide.test2 { height: auto }
<h3>原始:200px</h3>
<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>
<li><a href="#">Lorem Ipsum</a></li>
</ul>
<br>
<h3>高度:310px</h3>
<ul class="slide test1">
<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>
<li><a href="#">Lorem Ipsum</a></li>
</ul>
<br>
<h3>高度:auto</h3>
<ul class="slide test2">
<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>
<li><a href="#">Lorem Ipsum</a></li>
</ul>
英文:
Sum the line-height
(default ~1.2 x font-size) and padding
(x2 for top/bottom) of your li
elements.
You will see that only three li
will fit in .slide { height: 200px }
=> 1.2 * 16px * 3 + 40px * 3 = 177.6px
. Adding top padding 20px
for the fourth li
, totalling 197.6
, which yields just enough room for three li
and a few pixels of the fourth.
-
Room needed per
li
: 16px(font-size) * 1.2(line-height) + ~3px (inline spacing) + 2 * 20px(top/bottom padding) = ~62px. (Firefox DevTools reportsli.height: 22px
without padding, which proves the calc). -
Total minimum height of a
.slide
= 5 * 62px = 310px.
Without animation requirements, it would be best to use height: auto
when touch
gets clicked...
Add * { outline: 1px dashed }
to you CSS and you will see it happen.
snippet without the <input id="touch">
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
* { outline: 1px dashed }
.slide {
clear: both;
width: 100%;
height: 200px;
list-style-type: none;
text-align: center;
overflow: hidden;
transition: height .4s ease;
}
.slide li {
padding: 20px
}
.slide.test1 { height: 310px }
.slide.test2 { height: auto }
<!-- language: lang-html -->
<h3>original: 200px</h3>
<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>
<li><a href="#">Lorem Ipsum</a></li>
</ul>
<br>
<h3>height: 310px</h3>
<ul class="slide test1">
<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>
<li><a href="#">Lorem Ipsum</a></li>
</ul>
<br>
<h3>height: auto</h3>
<ul class="slide test2">
<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>
<li><a href="#">Lorem Ipsum</a></li>
</ul>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论