Opening a Details Element from the URL

Chris Coyier Chris Coyier on

Say you’ve got a page with a bunch of <details> elements on it.

Your goal is to be able to send someone to that page with one particular details element open.

I was doing just this recently, and my first thought was to do it server-side. If the URL was like website.com/#faq-1 I’d see if faq-1 matches an ID of the details element and I’d put the open attribute on it like <details id="faq-1" open>. But no, you don’t get to have the #hash as part of the URL server side (for whatever reason 🤷‍♀️).

Then I started writing JavaScript to do it, where you definitely can access the hash (window.location.hash). I’d just querySelector for the hash and if I found a matching details element, I’d open it up.

Then I was reminded you don’t need to do this at all. What you need is (drumroll)… HTML.

The trick is hash-linking to an element inside the <details>. So like:

<details>
  <summary>What was Rosebud in Citizen Kane?</summary>
  <div id="faq-1">A sled.</div>
</details>
Code language: HTML, XML (xml)

Now, if you hash-link to #faq-1, the browser will know that it has to open that <details> in order for it to be seen, so it does. You don’t normally need a <div> wrapper or anything inside the details element, but we’re doing it here as it’s obviously handy.

Here’s a demo of a page that is set up in this way:

It’s probably more interesting to just visit this hash-link URL and see it open right up and work.

This came up for me while working on this documentation page where I wanted to be able to link to specific things that were otherwise “buried” in details elements.

As a bit of an enhancement, you might want to consider CSS like this:

:target {
  background: yellow;
  scroll-margin-block-start: 4rem;
}Code language: CSS (css)

That will apply some styling to the element that matches the hash in the URL, as well as push it away from the top edge of the browser window a little bit. In this case, it helps make sure the FAQ question is actually visible, not just the answer.

Looking for a complete course on getting into web development?

Frontend Masters logo

We have a complete intro course to web development by renowned developer Brian Holt from Microsoft. You'll learn how to be a successful coder knowing everything from practical HTML and CSS to modern JavaScript to Git and basic back-end development.

7-Day Free Trial

One response to “Opening a Details Element from the URL”

  1. pd says:

    This highlights to me how semantically dubious the details and summary tags are. It would seem a lot more logic for them to have followed the way label and form elements are paired together via the label for attribute.

    summary for="example"
    details id="example"

    There you have the basic concept known as … wait for it … a tab. I know, I know, HTML standards people are terrified of creating tags for simplistic UX concepts that have consumed countless hours of JS/HTML/CSS developer time. But we are basically there, except we are not. So very close when factoring in the existing grouping of details tags like radio buttons by the same attribute value (forget which attribute it is).

    I really do not understand why HTML has failed to keep up with basic tags to create basic UX concepts in trivial declarative manner without faffing about with any JS.

    We get to y little half-arsed atoms of new elements from the standards bods, but they’re always developed as if by someone who has never written HTML before because they never look into the existing ‘natural’ history of HTML to ensure they are not rewriting the wheel and building new elements that, at least partially, do the same thing established HTML elements had decades ago.

    HTML is not evolving, it’s stagnant relative to any other code that produces a UX.

    The list of what cannot be declared in HTML but can trivially be done in other languages must be quite long. One of these days hopefully someone will document it.

Leave a Reply

Your email address will not be published. Required fields are marked *

$839,000

Frontend Masters donates to open source projects through thanks.dev and Open Collective, as well as donates to non-profits like The Last Mile, Annie Canons, and Vets Who Code.