Tree views in CSS
This is not what is typically called a tree view: this is collapsible nested lists. The differences are important, and very fiddly to reconcile.<p>The key difference is that in a tree view, every node is focusable and has an action associated with activating it (which for parent nodes <i>might</i> be expanding&#x2F;collapsing, but need not be), whereas with collapsible nested lists, only parent nodes are focusable, and their action is reserved and limited to expanding and collapsing. (You could make leaf nodes focusable and give them actions—e.g. make each one a link—but you can’t give parent nodes any other action, which is a fundamental limitation.)<p>Here’s what tree views are: <a href="https:&#x2F;&#x2F;w3c.github.io&#x2F;aria-practices&#x2F;#TreeView" rel="nofollow">https:&#x2F;&#x2F;w3c.github.io&#x2F;aria-practices&#x2F;#TreeView</a>. This describes the functionality and interaction modes (including all the expected keyboard behaviours), with examples.<p>One important secondary difference: the article said that “the standard keyboard interaction is supported automatically”, and this is true for collapsible nested lists, but false for a tree view. In the former, each parent node is separately focusable, and you expect to use Tab&#x2F;Shift+Tab to navigate between parent nodes; but tree views are composite widgets that manage focus, only appearing once in the tab order and using other keys (like the arrow keys) to manipulate focus within.<p>Is it possible to implement a true tree view with &lt;details&gt;? Mostly, yes, but there are limitations and difficulties.<p>If you’re OK with parent nodes’ only action being to toggle expansion, it’s not particularly hard; do it largely as shown in this article, making each leaf node focusable, and add some (nicely optional) JavaScript to get the keyboard interactions right and give the right ARIA attributes. The ARIA Authoring Practices document already linked will guide you through approximately all of the considerations. (If you haven’t worked with this document before, please do read the “Read Me First” section first.) In fact, I strongly recommend this general approach: make something that <i>functions</i> with only the HTML, then <i>enhance</i> it with JavaScript.<p>If you want parent nodes to have actions, parent nodes’ actions will need to be moved out of the &lt;summary&gt;. You’ll end up with something like this:<p><pre><code>  &lt;ul class=&quot;tree&quot;&gt;
    &lt;li&gt;
      &lt;a href&gt;Giant planets&lt;&#x2F;a&gt;
      &lt;details open&gt;
        &lt;summary&gt;Expand&lt;&#x2F;summary&gt;
        &lt;ul&gt;
          &lt;li&gt;
            &lt;a href&gt;Gas giants&lt;&#x2F;a&gt;
            &lt;details&gt;
              &lt;summary&gt;Expand&lt;&#x2F;summary&gt;
              &lt;ul&gt;
                &lt;li&gt;&lt;a href&gt;Jupiter&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
                &lt;li&gt;&lt;a href&gt;Saturn&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
              &lt;&#x2F;ul&gt;
            &lt;&#x2F;details&gt;
          &lt;&#x2F;li&gt;
          &lt;li&gt;
            &lt;a href&gt;Ice giants&lt;&#x2F;a&gt;
            &lt;details&gt;
              &lt;summary&gt;Expand&lt;&#x2F;summary&gt;
              &lt;ul&gt;
                &lt;li&gt;&lt;a href&gt;Uranus&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
                &lt;li&gt;&lt;a href&gt;Neptune&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
              &lt;&#x2F;ul&gt;
            &lt;&#x2F;details&gt;
          &lt;&#x2F;li&gt;
        &lt;&#x2F;ul&gt;
      &lt;&#x2F;details&gt;
    &lt;&#x2F;li&gt;
  &lt;&#x2F;ul&gt;
</code></pre>
Focus and accessibility management will become a little more complicated, but it’s still workable. But styling will be at least somewhat painful because of the DOM structures and the order of things. You’ll probably want to use details::before for the marker. Grid and `details { display: contents }` <i>might</i> help with layout, though absolute positioning is probably the pragmatic approach (and test cross-browser before doing anything like `details { display: contents }`, the two features can have surprising interactions). Also in the absence of JavaScript, it’s not going to be possible to get the expected interaction order: you would <i>want</i> the focus order to be expand&#x2F;collapse button, then the node itself, then children, but those first two will necessarily be transposed. Still, it’ll be better than it not working at all.