Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collapsible lists using HTML and CSS

Tags:

html

css

I have a collapsible list implemented using HTML and 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.

Here is the implementation I currently have. CSS styles:

.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:

<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> 
like image 332
user3881003 Avatar asked Jul 27 '14 04:07

user3881003


People also ask

How do you make a collapsible list in HTML?

Close the head portion of the page, with the ending tag for the head of the page (</head>). Create the body of the HTML code. Type the tag for starting the body (<body>). Create the list content, including some HTML styling information for the users browser to use, for them to expand and collapse the list.

How do you collapse HTML in CSS?

Just add data-toggle="collapse" and a data-target to element to automatically assign control of a collapsible element. The data-target attribute accepts a CSS selector to apply the collapse to. Be sure to add the class collapse to the collapsible element. If you'd like it to default open, add the additional class in.


1 Answers

If you use a modern browser, you can just use HTML5 like this:

<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.

like image 140
Ananth Avatar answered Sep 22 '22 11:09

Ananth