Expandable/Collapsible Content | WebReference

Expandable/Collapsible Content

Expandable/Collapsible Content

Overview

Sometimes content itself, not presentation or navigation, brings a subtle usability challenge. When page text is unavoidably long and complex, the mass of material can lead to confusion through a loss of context. Dense legal or technical documents are prone to this problem. One way to manage such text is to expose just what's needed, hiding sections beneath expandable headings:

+ traditional lorem ipsum dummy text

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+ sources from de finibus bonorum et malorum

+ 1.10.32

Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?

+ 1.10.33

At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

You've likely seen this before; it's similar to tree-view navigation, and is well suited to strongly structured material. But there are dangers. I've seen a dozen ways to do this in the last few years, and all of them suffered from one or more problems:

The following approach solves all those problems. It's simple to code, always accessible, cross-browser compatible, and preserves the current display state under Internet Explorer. For a taste of persistence, open one or more sections in the example shown above. Then do a page refresh, or click Back and return. If you're using IE 5 or later, the display state stays the same.

HTML

The concept and code for this technique are both pretty simple: each heading is a toggle for the display of the next block. All blocks start out hidden, due to a style definition, and a Javascript function toggles the display state.

Suppose you had two sections of content, with additional levels nested inside the second section. (This was the case on the first example.) An outline of the structure would look like this:

top-level heading
   hidden content
top-level heading
   hidden content
      second-level heading
         hidden content
      second-level heading
         hidden content

Let's create this structure, using minimal content, in order to expose the mechanics. Assuming you use h1, h2 etc. as heading tags, the HTML for each heading+section pair would look like this:

<h1><a href="javascript:void(0)" class="dsphead"
   onclick="dsp(this)">
   <span class="dspchar">+</span> heading</a></h1>
   <div class="dspcont">section</div>

We'll get to the styles and script function in a moment. For now, keep in mind that any block tag can be used to hold the headings and sections: p, div, ul, etc. Just don't put extra elements between a heading and its section—they have to be immediately adjacent. For the two-level structure we need, the HTML winds up looking like this:

<h1><a href="javascript:void(0)" class="dsphead"
   onclick="dsp(this)">
   <span class="dspchar">+</span> heading</a></h1>
   <div class="dspcont">section</div>
<h1><a href="javascript:void(0)" class="dsphead"
   onclick="dsp(this)">
   <span class="dspchar">+</span> heading</a></h1>
   <div class="dspcont">
      <h2><a href="javascript:void(0)" class="dsphead"
         onclick="dsp(this)">
         <span class="dsphead">+</span> heading</a></h2>
         <div class="dspcont">section</div>
      <h2><a href="javascript:void(0)" class="dsphead"
         onclick="dsp(this)">
         <span class="dspchar">+</span> heading</a></h2>
         <div class="dspcont">section</div>
   </div>

Created: March 27, 2003
Revised: June 26, 2003

URL: http://webreference.com/programming/css_content/1