Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a details element to OPEN by default or via CSS

Tags:

HTML5 adds two new elements that are useful for marking up a table of contents for an article: details and summary.

The details element defaults to closed (hides everything except the summary element) and when clicked, it expands to reveal its contents. When it does this, it adds an "open" attribute to the details element.

I would like the element to default to open without having to add the open attribute to the markup. Is it possible to do this via CSS or would scripting have to come into play?

Example:

<details> <summary>Table of Contents</summary>     <ul>         <li><a href="#" class="active">Introduction</a></li>         <li><a href="/2/">Body</a></li>         <li><a href="/3/">Conclusion</a></li>     </ul> </details> 
like image 447
Scott B Avatar asked Jan 11 '13 20:01

Scott B


People also ask

How do I open details in HTML?

The HTML <details> open attribute is used to indicate whether the details will be display on page load. It is a boolean attribute. If this attribute is present then the detail will be visible. Example: This example illustrates the use of open attribute in <details> element.

How do you style a detail tag?

The details tag (or “element”) can be used on its own, or combined with the also new summary tag. This HTML combination is called a disclosure widget, but actually acts like a simple accordion that the user can open if they wish to view more information.

When would you use the details element in your website and why?

Definition and Usage The <details> tag specifies additional details that the user can open and close on demand. The <details> tag is often used to create an interactive widget that the user can open and close. By default, the widget is closed.

What is the default element in HTML?

The HTML default Attribute is a Boolean attribute. This attribute is used to specify that the track will be enabled if the user's preferences do not indicate that another track would be more appropriate. Note: With a default attribute, there must not be more than one track element per media element.


2 Answers

You can add open attribute into details tag like this:

<details open>      <summary>Table of Contents</summary>      <ul>          <li><a href="#" class="active">Introduction</a></li>          <li><a href="/2/">Body</a></li>          <li><a href="/3/">Conclusion</a></li>      </ul>  </details>

The attribute will be expanded by default

like image 181
Ariona Rian Avatar answered Sep 28 '22 02:09

Ariona Rian


If you want a JS solution, you could give the details element an ID:

<details id="target-me">  <summary>Table of Contents</summary>   <ul>     <li><a href="#" class="active">Introduction</a></li>     <li><a href="/2/">Body</a></li>     <li><a href="/3/">Conclusion</a></li>   </ul> </details> 

And then use the following JS:

document.getElementById("target-me").open = true; 
like image 34
kosher Avatar answered Sep 28 '22 01:09

kosher