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>
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.
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.
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.
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.
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
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With