cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

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.

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


<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 inline JS [simplest]</button>

  
  <div class=toggled_1>
    <div class=tog_content>
      
      <h2 class=tog_title>Accessibily toggle a buttons <strong>next sibling</strong> using inline 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 zero 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 class=toggler_3 data-toggler="#toggled_3">
      Toggle a <strong>specified element</strong> using JS [recommended]
    </button>
  </div>

  <div class="toggled_3 toggled-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 ease .6s,
        opacity .6s ease-in 0s,
        max-height .6s ease-out 0s;
}
.toggled[aria-hidden="false"] {
    visibility: visible;
    opacity: 1;
    /* max-height set by JS */
    transition-delay: 0s, 0s, 0s; /* unit "s" is required */
}</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>

<p class=smaller><a target=_blank title="[new window]" href="https://codepen.io/2kool2/pens/popular/?grid_type=list#">Pens by Mike Foskett</a> &mdash; <a target=_blank title="[new window]" href="https://websemantics.uk/">webSemantics</a></p>
            
          
!
            
              /* 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;
}
.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;
}
 


 
 
/* 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 JS */
 
.toggled_3[aria-hidden] {
  overflow: hidden;
  visibility: hidden;
  opacity: 0.35;
  max-height: 0;
  transition: 
    visibility 0s ease .6s,
    opacity .6s ease-out 0s,
    max-height .6s ease-out 0s;
}
.toggled_3[aria-hidden="false"] {
  visibility: visible;
  opacity: 1;
  /* max-height set by JS */
  transition-delay: 0s, 0s, 0s; /* unit "s" is required */
}


/* 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 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));
    } 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
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console