[英]Collapsible lists using HTML and CSS
I have a collapsible list implemented using HTML and CSS.我有一个使用 HTML 和 CSS 实现的可折叠列表。 The list works properly, but I need a little modification.
该列表工作正常,但我需要稍作修改。
Whenever I click an item in the list it expands.每当我单击列表中的一个项目时,它就会展开。 But as I click on another item in the same list, the previously expanded element gets collapsed while the clicked one gets expanded.
但是当我点击同一个列表中的另一个项目时,之前展开的元素会折叠起来,而被点击的元素会被展开。
Please help me to apply the behavior that makes it possible to expand multiple list items at the same time.请帮助我应用可以同时展开多个列表项的行为。
I want it to be done in HTML and CSS only.我希望它只在 HTML 和 CSS 中完成。
Here is the implementation I currently have.这是我目前拥有的实现。 CSS styles:
CSS样式:
.row { vertical-align: top; height: auto !important; }
list { display: none; }
.show { display: none; }
.hide:target + .show { display: inline; }
.hide:target { display: none; }
.hide:target ~ .list { display:inline; }
@media print { .hide, .show { display: none; } }
And the HTML markup:和 HTML 标记:
<div class="row">
<a href="#hide1" class="hide" id="hide1">Expand</a>
<a href="#show1" class="show" id="show1">Collapse</a>
<div class="list">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
</div>
<details> <summary>See More</summary> This text will be hidden if your browser supports it. </details>
One can find more information about <details>
HTML element, including other examples, on MDN.可以在 MDN 上找到有关
<details>
HTML 元素的更多信息,包括其他示例。
Pure HTML & CSS纯 HTML 和 CSS
A checkbox and it's :checked
state sounds like a perfect match for your case:一个复选框和它的
:checked
状态听起来很适合你的情况:
[id^="togList"], /* HIDE CHECKBOX */ [id^="togList"] ~ .list, /* HIDE LIST */ [id^="togList"] + label span + span, /* HIDE "Collapse" */ [id^="togList"]:checked + label span{ /* HIDE "Expand" (IF CHECKED) */ display:none; } [id^="togList"]:checked + label span + span{ display:inline-block; /* SHOW "Collapse" (IF CHECKED) */ } [id^="togList"]:checked ~ .list{ display:block; /* SHOW LIST (IF CHECKED) */ }
<div class="row"> <input id="togList1" type="checkbox"> <label for="togList1"> <span>Expand</span> <span>Collapse</span> </label> <div class="list"> <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> </div> </div>
Based on the answers above, just created a single HTML with CSS and JS embedded for this kind of task.根据上面的答案,刚刚为此类任务创建了一个嵌入了 CSS 和 JS 的 HTML。 GitHub repo , the link will stay valid as I have no plan to remove it.
GitHub repo ,该链接将保持有效,因为我不打算删除它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.