Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<h1>Toggling accordions using 5 different methods</h1>


<section>
  <details>
    <summary class=toggler_1>Pure HTML, zero JS, zero CSS [recommended 2022]</summary>
    <div class=tog_content>
      <p>The <code>summary</code> element acts as a button, and the element that follows is the hidden content.</p>
      <figure>
        <figcaption>HTML</figcaption>
        <pre><code class=language-markup spellcheck=false contenteditable>&lt;details&gt;
    &lt;summary&gt;Pure HTML accordian&lt;/summary&gt;
    &lt;div&gt;
        &lt;!-- Drop down content --&gt;
    &lt;/div&gt;
&lt;/details&gt;</code></pre>
      </figure>
    </div>
</section>


<section>
  <!-- Ref: https://www.w3.org/WAI/GL/wiki/Using_aria-expanded_to_indicate_the_state_of_a_collapsible_element -->

  <button class=toggler_1 aria-expanded=false onclick="this.setAttribute('aria-expanded',this.getAttribute('aria-expanded')==='false')">Toggle <strong>next sibling</strong> using <strong>inline</strong> JS [simplest]</button>

  
  <div class=toggled_1>
    <div class=tog_content>
      
      <h2 class=tog_title>Accessibily toggle a buttons <strong>next sibling</strong> using <strong>inline</strong> JS (on the button only).</h2>
      
      <figure>
        <figcaption>HTML <small>(with inline JavaScript)</small></figcaption>
        <pre><code class=language-markup spellcheck=false contenteditable>&lt;button class="toggler"
    onclick="this.setAttribute('aria-expanded', this.getAttribute('aria-expanded') === 'false');"
    aria-expanded="false"&gt;Toggle button&lt;/button&gt;
&lt;div class="toggled"&gt;Toggled content&lt;/div&gt;</code></pre>
      </figure>
      
      <figure>
        <figcaption>CSS</figcaption>
        <pre><code class=language-css spellcheck=false contenteditable>.toggler[aria-expanded="false"] + .toggled {
    overflow: hidden;
    visibility: hidden;
    opacity: 0;
    max-height: 0;
    transition:
        visibility 0s ease .6s,
        opacity .6s ease-in 0s,
        max-height .6s ease-out 0s;
}
.toggler[aria-expanded="true"] + .toggled {
    overflow: hidden;
    visibility: visible;
    opacity: 1;
    /* Ensure max-height is sufficient at min-width */
    max-height: 20rem;
    transition:
        visibility 0s ease 0s,
        opacity .6s ease-in 0s, 
        max-height .6s ease-out 0s;
}</code></pre>
      </figure>
      
    </div>
  </div>
</section>

 

 


<section>
  <label class=tog_container>

    <span class=toggler_2>Toggle <strong>next sibling</strong> with <strong>zero</strong> JavaScript</span>

    <input class=visually-hidden type=checkbox>

    <span class=toggled_2>
      <span class=tog_content>

        <em class=tog_title>Toggle <strong>next sibling</strong>. HTML &amp; CSS only, no JavaScript</em>

        <figure>
          <figcaption>HTML</figcaption>
            <pre><code class=language-markup spellcheck=false contenteditable>&lt;label&gt;
    &lt;span class="toggler"&gt;Toggle button&lt;/span&gt;
    &lt;input class="visually-hidden" type="checkbox"&gt;
    &lt;span class="toggled"&gt;Toggled content&lt;/span&gt;
&lt;/label&gt;</code></pre>
        </figure>

        <figure>
          <figcaption>CSS</figcaption>
            <pre><code class=language-css spellcheck=false contenteditable>.toggler, .toggled {display: block}
.toggled {
    overflow: hidden;
    visibility: hidden;
    opacity: 0;
    max-height: 0;
    transition: 
        visibility 0s ease .6s,
        opacity .6s ease-in 0s,
        max-height .6s ease-out 0s;
}
:checked + .toggled {
    visibility: visible;
    opacity: 1;
    /* Ensure max-height is sufficient at min-width */
    max-height: 50rem;
    transition-delay: 0s, 0s, 0s; /* unit "s" is required */
}</code></pre>
        </figure>
      
      </span>
    </span>

  </label>
</section>



<section>
  
  <div>
<button aria-expanded=false aria-controls=toggled_5
  onclick="(function(b){var s=b.getAttribute('aria-expanded')==='true',c=document.getElementById(b.getAttribute('aria-controls'));if(!c)return;c.setAttribute('style','max-height:'+c.scrollHeight+'px');b.setAttribute('aria-expanded',!s);c.setAttribute('aria-hidden',s);})(this);"
  class=toggler_3>
  Toggle a <strong>specified element</strong> using <strong>inline</strong> JS
</button>
  </div>

  <div class=toggled_5 id=toggled_5 aria-hidden=true>
    <div class=tog_content>

      <h2 class=tog_title>Accessibily toggle a buttons <strong>specified</strong> content panel using <strong>inline</strong> JS (on the button only). Button and content panel do not need to be siblings.</h2>

      <figure>
        <figcaption>HTML <small>(with inline JavaScript)</small></figcaption>
        <pre><code class=language-markup spellcheck=false contenteditable>&lt;button class="toggler"
    onclick="(function(b){var s=b.getAttribute('aria-expanded')==='true',c=document.getElementById(b.getAttribute('aria-controls'));if(!c)return;c.setAttribute('style','max-height:'+c.scrollHeight+'px');b.setAttribute('aria-expanded',!s);c.setAttribute('aria-hidden',s);})(this);"
    aria-controls="toggled"
    aria-expanded="false"&gt;Toggle button&lt;/button&gt;
&lt;div aria-hidden="true" id="toggled" class="toggled"&gt;Toggled content&lt;/div&gt;
</code></pre>
      </figure>
      
      <p>The <code>max-height</code> of the content panel is accurately calculated by the buttons inline JavaScript.</p>

      <figure>
        <figcaption>CSS</figcaption>
        <pre><code class=language-css spellcheck=false contenteditable>.toggled[aria-hidden] {
    overflow: hidden;
    visibility: hidden; /* Removes content from keyboard focus chain */
    opacity: 0;
    max-height: 0;
    transition: 
        visibility 0s linear .6s,
        opacity .6s ease-in,
        max-height .6s ease-out;
}
.toggled[aria-hidden="true"] {
    max-height: 0 !important; /* Overrides inline style */
}
.toggled[aria-hidden="false"] {
    visibility: visible;
    opacity: 1;
    max-height: 50em; /* Set more accurately inline by JavaScript */
    transition-delay: 0s, 0s, 0s;
    transition-timing-function: linear, linear, ease-out;
}
</code></pre>
      </figure>
      
      <p>See the full pen for details: <a target=_blank title="[new window]" href="https://codepen.io/2kool2/pen/aGOmxZ">Accordion expand/collapse using inline JavaScript</a></p>
      
      <p>Which also contains optional extensions to:</p>
      <ul>
        <li>Open from URL</li>
        <li>Open from anchor</li>
        <li>Scroll button to top</li>
        <li>Embed accordions</li>
        <li>Open a single accordion.</li>
      </ul>

    </div>
  </div>

</section>


 

<section>
  
  <div>
    <button class=toggler_3 data-toggler="#toggled_3">
      Toggle a <strong>specified element</strong> using JS
    </button>
  </div>

  <div class="toggled_3 xtoggled-long" id=toggled_3>
    <div class=tog_content>
      
      <h2 class=tog_title>Accessibily toggle a <strong>specified element</strong>, and account for variable height, using JavaScript</h2>
      
      <figure>
        <figcaption>HTML</figcaption>
        <pre><code class=language-markup spellcheck=false contenteditable>&lt;button data-toggler="#toggled"&gt;Toggle button&lt;/button&gt;
&lt;div id="toggled" class="toggled"&gt;Toggled content&lt;/div&gt;
</code></pre>
      </figure>

        <figure>
          <figcaption>CSS</figcaption>
            <pre><code class=language-css spellcheck=false contenteditable>.toggled[aria-hidden] {
    overflow: hidden;
    visibility: hidden;
    opacity: 0;
    max-height: 0;
    transition: 
        visibility 0s linear .6s,
        opacity .6s ease-in,
        max-height .6s ease-out;
}
.toggled[aria-hidden="true"] {
    max-height: 0 !important; /* Overrides inline style */
}
.toggled[aria-hidden="false"] {
    visibility: visible;
    opacity: 1;
    max-height: 50em; /* Set more accurately inline by JavaScript */
    transition-delay: 0s, 0s, 0s; /* unit "s" is required */
    transition-timing-function: linear, linear, ease-out;
}</code></pre>
        </figure>

        <figure>
          <figcaption>JavaScript</figcaption>
            <pre><code class=language-javascript spellcheck=false contenteditable>const toggler = (() =&gt; {
  
  const dataAttr = "data-toggler";

  // Get height of an element object
  // Assumes it is hidden by max-height: 0 in the CSS
  const getHeight = (obj) =&gt; {
    obj.setAttribute("style", "max-height: auto");
    const height = obj.scrollHeight + "px";
    obj.removeAttribute("style");
    return height;
  };
  
  // Toggles the show/hide state of button and block using ARIA attributes
  const toggleState = (togglerBtn, isHidden) =&gt; {
  
    const toggledAttr = togglerBtn.getAttribute(dataAttr);
    if (!toggledAttr) {return;}
    
    const toggled = document.querySelector(toggledAttr);
    if (!toggled) {return;}

    if (isHidden) { // Show
      toggled.setAttribute("style", "max-height: " + getHeight(toggled));
    } else { // Hide
      toggled.removeAttribute("style");
    }

    togglerBtn.setAttribute("aria-expanded", isHidden);
    toggled.setAttribute("aria-hidden", !isHidden);
  };
  
  const isOpen = (togglerBtn) =&gt; {
    return togglerBtn.getAttribute("aria-expanded") === "true";
  };
  
  const togglerClicked = (e) =&gt; {
    const togglerBtn = e.target;
    toggleState(togglerBtn, !isOpen(togglerBtn));
  };
  
  const initialise = (() =&gt; {

    const togglerButtons = document.querySelectorAll("["+ dataAttr + "]");

    // Run through each toggle button which has a data-toggler3 set
    for (const togglerBtn of togglerButtons) {
      
      // Set ARIA states
      toggleState(togglerBtn, isOpen(togglerBtn));

      // Toggle ARIA state on a button click
      togglerBtn.addEventListener("click", togglerClicked);
    }
  })();
  
})();</code></pre>
        </figure>

    </div>
  </div>

</section>


 

<section>
  
  <div>
    <button class=toggler_3 data-toggler="#toggled_4">
      Toggle a <strong>specified element</strong> using JS (short content)
    </button>
  </div>

  <div class="toggled_3 toggled-short" id=toggled_4>
    <div class=tog_content>
      
      <figure class=tog_fig-solo>
        <figcaption>CSS</figcaption>
        <pre><code class=language-css spellcheck=false contenteditable>.toggled[aria-hidden].toggled-short {
  transition-duration: 0s, .4s, .4s;
}</code></pre>
      </figure>

    </div>
  </div>

</section>


<!--  Footer include -->
[[[https://codepen.io/2kool2/pen/mKeeGM]]]
              
            
!

CSS

              
                /* Generic styles */

*, *::after, *::before {
  box-sizing: inherit;
}
html {
  overflow-y: scroll;
  touch-action: manipulation;
}
body {
  font-family: sans-serif;
  padding: 0 2rem;
  max-width: 50rem;
  margin: 2rem auto;
  text-align: center;
  line-height: 1.5;
  backgound-color: hsl(0, 0%, 35%);
  background-position: center bottom;
  background-size: cover;
  background-attachment: fixed;
  /* Unsplash: Ronald Yang */
  background-image: url(https://images.unsplash.com/photo-1434144893279-2a9fc14e9337?w=1920);
}
h1 {
  font-weight: 100;
  color: #fff;
  text-shadow: 0 0 3px #000;
  background-color: hsla(0, 0%, 35%, 0.9);
  margin: 3rem 0;
  line-height: 1.2;
  padding: 0.25rem;
}
section {
  margin: 3rem 0;
}
ul {
  max-width: 12rem;
  margin: 1rem auto;
}
li {text-align: left}

.tog_title {
  display: block;
  font-size: 1rem;
  font-weight: 100;
  font-style: normal;
  margin: 0;
  text-shadow: 0 0 2px #000;
}
figure {
  position: relative;
  margin: 1rem 0 0;
}
figcaption {
  font-weight: 700;
  padding: 0;
  margin: 0;
  left: 1rem;
  font-size: 1rem;
  letter-spacing: 0.05em;
  text-shadow: 0 1px #000;
}
.smaller,
a {
  color:#fff;
  background-color: hsla(0, 0%, 35%, 0.8);
  padding:.5rem;
  text-shadow: 0 1px #000;
  letter-spacing: 0.05em;
}
.visually-hidden {
  /*https://developer.yahoo.com/blogs/ydn/clip-hidden-content-better-accessibility-53456.html*/
  position: absolute !important;
  clip: rect(1px, 1px, 1px, 1px);
  padding:0 !important;
  border:0 !important;
  height: 1px !important;
  width: 1px !important;
  overflow: hidden;
}
button * {pointer-events:none;}
button::-moz-focus-inner { 
	border: 0; 
	padding: 0; 
	margin-top:-2px; 
	margin-bottom: -2px; 
}
[class^="toggler"] {
  /* Just the button prettiness here */
  font-size: 1.125rem;
  line-height: 1.2;
  display: block;
  box-sizing: border-box;
  width: 100%;
  margin: 0;
  padding: 1rem;
  border: 0 solid;
  background-color: hsl(0, 0%, 80%);
  cursor: pointer;
  transition: all .3s ease-out;
  outline: 0 solid;
  box-shadow: 0 2px 4px rgba(0,0,0,.5);
}
[class^="toggler"]:hover,
[class^="toggler"]:focus {
  background-color: hsl(0, 0%, 90%);
  outline: 0 solid;
  box-shadow: 0 3px 6px rgba(0,0,0,.75);
}
[class^="toggler"]:active {
  box-shadow: inset 0 0 2px rgba(0,0,0,.75);
}
[class^="toggled"] {
  box-shadow: 0 3px 6px rgba(0,0,0,.75);
}
.tog_content {
  background-color: hsla(0, 0%, 35%, 0.8);
  color: #fff;
  display: block;
  padding: 1rem;
}
.tog_fig-solo {
  margin:0
}



 
/* Next Sibling inline JS */

.toggler_1[aria-expanded="false"] + .toggled_1 {
  overflow: hidden;
  visibility: hidden;
  opacity: 0.35;
  max-height: 0;
  transition:
    visibility 0s ease .6s,
    opacity .6s ease-in 0s,
    max-height .6s ease-out 0s;
}
.toggler_1[aria-expanded="true"] + .toggled_1 {
  overflow: hidden;
  visibility: visible;
  opacity: 1;
  /* Note: Manually set, check content visible at minimum viewport widths */
  max-height: 75rem;
  transition:
    visibility 0s ease 0s,
    opacity .6s ease-in 0s,
    max-height 1s ease-out 0s;
}
 


/* Specified element using inline JS*/

.toggled_5[aria-hidden] {
  overflow: hidden;
  visibility: hidden; /* Removes content from keyboard focus chain */
  opacity: 0;
  max-height: 0;
  transition: 
    visibility 0s linear .6s,
    opacity .6s ease-in,
    max-height .6s ease-out;
}
.toggled_5[aria-hidden="true"] {
  max-height: 0 !important; /* Overrides inline style */
}
.toggled_5[aria-hidden="false"] {
  visibility: visible;
  opacity: 1;
  max-height: 50em; /* Set more accurately inline by JavaScript */
  transition-delay: 0s, 0s, 0s;
  transition-timing-function: linear, linear, ease-out;
}


 
 
/* Next sibling without JS */

.tog_container,
.toggler_2,
.toggled_2 {
  display: block;
}
 
.toggled_2 {
  overflow: hidden;
  visibility: hidden;
  opacity: 0.35;
  max-height: 0;
  transition:
    visibility 0s ease .6s,
    opacity .6s ease-in 0s,
    max-height .6s ease-out 0s;
}
:checked + .toggled_2 {
  visibility: visible;
  opacity: 1;
  /* Ensure max-height is sufficient at min-width */
  max-height: 50rem;
  transition-delay: 0s, 0s, 0s; /* unit "s" is required */
}
 



/* Specified element using unobtrusive JS */
 
.toggled_3[aria-hidden] {
  overflow: hidden;
  visibility: hidden;
  opacity: 0;
  max-height: 0;
  transition: 
    visibility 0s linear .6s,
    opacity .6s ease-in,
    max-height .6s ease-out;
}
.toggled_3[aria-hidden="true"] {
    max-height: 0 !important; /* Overrides inline style */
}
.toggled_3[aria-hidden="false"] {
    visibility: visible;
    opacity: 1;
    max-height: 50em; /* Set more accurately inline by JavaScript */
    transition-delay: 0s, 0s, 0s; /* unit "s" is required */
    transition-timing-function: linear, linear, ease-out;
}


/* Specified element, short content addition */

[class^="toggled"][aria-hidden].toggled-short {
  transition-duration: 0s, .4s, .4s;
}


/* Specified element, extra long content addition */

.toggled-long[aria-hidden="true"] { /* Closing */
  transition-duration: 0s, .6s, .6s;
  transition-delay: 0.8s, 0s, 0s; /* unit "s" is required */
}
.toggled-long[aria-hidden="false"] { /* Opening */
  transition-duration: 0s, .8s, .8s;
  transition-delay: 0s, 0s, 0s; /* unit "s" is required */
}






/* Prism - is used for code highlighting from an external pen: https://codepen.io/2kool2/pen/MEbeEg */

              
            
!

JS

              
                // JS is needed to toggle an object which isn't a sibling.

const toggler = (() => {
  
  const dataAttr = "data-toggler";


	// Get height of an element object
  // Assumes it is hidden by max-height: 0 in the CSS
	const getHeight = (obj) => {
    obj.setAttribute("style", "max-height: auto");
		const height = obj.scrollHeight + "px";
    obj.removeAttribute("style");
		return height;
	};
  

  // Toggles the show/hide state of button and block using ARIA attributes
  const toggleState = (togglerBtn, isHidden) => {
  
    const toggledAttr = togglerBtn.getAttribute(dataAttr);
    if (!toggledAttr) {return;}
    
    const toggled = document.querySelector(toggledAttr);
    if (!toggled) {return;}

    if (isHidden) { // Show
      //toggled.setAttribute("style", "max-height: " + getHeight(toggled));
      toggled.setAttribute('style', 'max-height:' + toggled.scrollHeight + 'px');
    } else { // Hide
      //toggled.removeAttribute("style");
    }

    togglerBtn.setAttribute("aria-expanded", isHidden);
    toggled.setAttribute("aria-hidden", !isHidden);
  };
  

  const isOpen = (togglerBtn) => {
    return togglerBtn.getAttribute("aria-expanded") === "true";
  };
  

  const togglerClicked = (e) => {
    const togglerBtn = e.target;
    toggleState(togglerBtn, !isOpen(togglerBtn));
  };
  

  const initialise = (() => {

    const togglerButtons = document.querySelectorAll("["+ dataAttr + "]");

    // Run through each toggle button which has a data-toggler3 set
    for (const togglerBtn of togglerButtons) {
      
      // Set ARIA states
      toggleState(togglerBtn, isOpen(togglerBtn));

      // Toggle ARIA state on a button click
      togglerBtn.addEventListener("click", togglerClicked);
    }
  })();
  
})();





/* Prism - is used for code highlighting from an external pen: https://codepen.io/2kool2/pen/MEbeEg */

              
            
!
999px

Console