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

              
                <html>

<head>
  <!-- HTML HEAD CONTENT -->
</head>

<body>
  <header></header>
  <main>
    <!-- 
      You can adjust the behaviour of the accordion here with a true or false value on data-individual-openable 

      data-speed is the amount of miliseconds one entry takes to open and close 
    -->
    <div class="c-accordion" data-individual-openable="false" data-speed="400">
      <div class="c-accordion__entries">

        <div class="c-accordion__entry is-expanded" aria-expanded="true" role="tablist">
          <div id="c-accordion__entry-title-1" class="c-accordion__entry-title" aria-selected="true" aria-controls="c-accordion__entry-body-1" role="tab">
            <a href="" class="c-accordion__entry-header-link js-accordion__entry-header-link">
                 Title
                 <div class="c-accordion__entry-header-icon-wrapper">
                   <svg class="c-accordion__entry-header-icon" xmlns="http://www.w3.org/2000/svg" width="19.999" height="12.979" viewBox="0 0 19.999 12.979">
                    <path d="M12,14.979l-10-10L4.979,2,12,9.021,19.02,2,22,4.979Z" transform="translate(-2 -2)" fill="#7c7c7c"></path>
                  </svg>
                </div>
              </a>
          </div>
          <div id="c-accordion__entry-body-1" class="c-accordion__entry-body" aria-hidden="false" role="tabpanel" aria-labelledby="c-accordion__entry-title-1" style="display: block;">
            <div class="c-accordion__entry-contents c-wysiwyg">Text</div>
          </div>
        </div>
        <div class="c-accordion__entry" aria-expanded="false" role="tablist">
          <div id="c-accordion__entry-title-2" class="c-accordion__entry-title" aria-selected="false" aria-controls="c-accordion__entry-body-2" role="tab">
            <a href="" class="c-accordion__entry-header-link js-accordion__entry-header-link">
                 Title
                 <div class="c-accordion__entry-header-icon-wrapper">
                   <svg class="c-accordion__entry-header-icon" xmlns="http://www.w3.org/2000/svg" width="19.999" height="12.979" viewBox="0 0 19.999 12.979">
                    <path d="M12,14.979l-10-10L4.979,2,12,9.021,19.02,2,22,4.979Z" transform="translate(-2 -2)" fill="#7c7c7c"></path>
                  </svg>
                </div>
              </a>
          </div>
          <div id="c-accordion__entry-body-2" class="c-accordion__entry-body" aria-hidden="true" role="tabpanel" aria-labelledby="c-accordion__entry-title-2">
            <div class="c-accordion__entry-contents c-wysiwyg">Text</div>
          </div>
        </div>
        <div class="c-accordion__entry" aria-expanded="false" role="tablist">
          <div id="c-accordion__entry-title-3" class="c-accordion__entry-title" aria-selected="false" aria-controls="c-accordion__entry-body-3" role="tab">
            <a href="" class="c-accordion__entry-header-link js-accordion__entry-header-link">
                 Title
                 <div class="c-accordion__entry-header-icon-wrapper">
                   <svg class="c-accordion__entry-header-icon" xmlns="http://www.w3.org/2000/svg" width="19.999" height="12.979" viewBox="0 0 19.999 12.979">
                    <path d="M12,14.979l-10-10L4.979,2,12,9.021,19.02,2,22,4.979Z" transform="translate(-2 -2)" fill="#7c7c7c"></path>
                  </svg>
                </div>
              </a>
          </div>
          <div id="c-accordion__entry-body-3" class="c-accordion__entry-body" aria-hidden="true" role="tabpanel" aria-labelledby="c-accordion__entry-title-3">
            <div class="c-accordion__entry-contents c-wysiwyg">Text</div>
          </div>
        </div>
        <div class="c-accordion__entry" aria-expanded="false" role="tablist">
          <div id="c-accordion__entry-title-4" class="c-accordion__entry-title" aria-selected="false" aria-controls="c-accordion__entry-body-4" role="tab">
            <a href="" class="c-accordion__entry-header-link js-accordion__entry-header-link">
                 Title
                 <div class="c-accordion__entry-header-icon-wrapper">
                   <svg class="c-accordion__entry-header-icon" xmlns="http://www.w3.org/2000/svg" width="19.999" height="12.979" viewBox="0 0 19.999 12.979">
                    <path d="M12,14.979l-10-10L4.979,2,12,9.021,19.02,2,22,4.979Z" transform="translate(-2 -2)" fill="#7c7c7c"></path>
                  </svg>
                </div>
              </a>
          </div>
          <div id="c-accordion__entry-body-4" class="c-accordion__entry-body" aria-hidden="true" role="tabpanel" aria-labelledby="c-accordion__entry-title-4">
            <div class="c-accordion__entry-contents c-wysiwyg">Text</div>
          </div>
        </div>

        <!--
          One accordion entry can be initially opened 
          with the following adjustments:

          1) Add class "is-expanded" to .c-accordion__entry
          2) Change aria-expanded value to true on .c-accordion__entry
          3) Change aria-selected value to true on .c-accordion__entry-title
          4) Change aria-hidden value to false on .c-accordion__entry-body
          5) Add style="display:block;" to .c-accordion__entry-body

          Following data needs to be incremented with a unique ID for each accordion entry:

          1) The ID for each .c-accordion__entry-title
          2) The same ID has to be set in the aria-labelledby attribute for the corresponding .c-accordion__entry-body
          3) The ID for each .c-accordion__entry-body
          4) The same ID has to be set in the aria-controls attribute for the corresponding .c-accordion__entry-title
        -->
      </div>
    </div>
  </main>
  <footer></footer>
</body>

</html>
              
            
!

CSS

              
                .c-accordion__entry {
  border-bottom: 1px solid grey;
}

.c-accordion__entry-title {
  position: relative;
}

.c-accordion__entry-header-link {
  display: block;
  padding: 10px 45px 10px 0;
  color: #000;
  font-size: 1.75em;
  cursor: pointer;
}

.c-accordion__entry-body {
  display: none;
}

.c-accordion__entry-contents {
  padding: 5px 0; 
  font-size: 1.125em;
}

.c-accordion__entry-header-icon-wrapper {
  position: absolute;
  top: 50%;
  right: 10px;
  display: block;
  transform: translate(0,-50%);
  transition: all .4s;
}

.c-accordion__entry-header-icon {
  display: block;
}


// This needs to be so specific because then we can nest accordions
.c-accordion__entry.is-expanded > .c-accordion__entry-title > .c-accordion__entry-header-link > .c-accordion__entry-header-icon-wrapper {
  transform: translate(0,-50%) rotate(180deg);
}

// ===== ADDITIONAL STYLING FOR CODEPEN =====

main {
  max-width: 1000px;
  margin: 25px auto;
}
              
            
!

JS

              
                var MODULE = MODULE || {};

(function($, MODULE) {
  MODULE.init_accordion = function() {
    // Logic for accordion
    var accordion = (function() {
      var $accordion_elem = $(".c-accordion"),
        $accordion_headerlink = $accordion_elem.find(
          ".js-accordion__entry-header-link"
        ),
        $accordion_item = $accordion_elem.find(".c-accordion__entry");

      var settings = {
        speed: parseInt($accordion_elem.attr("data-speed")) || 400,
        individual_openable: $accordion_elem.attr("data-individual-openable") === "true"
      };

      return {
        init: function() {
          $accordion_headerlink.on("click", function(e) {
            e.preventDefault();
            accordion.toggle($(this));
          });

          if (
            !settings.individual_openable &&
            $(".c-accordion__entry.is-expanded").length > 1
          ) {
            $(".c-accordion__entry.is-expanded").removeClass(
              "is-expanded"
            );
          }
        },
        toggle: function(self) {
          if (
            !settings.individual_openable &&
            self[0] !=
              self
                .closest(".c-accordion")
                .find(
                  ".c-accordion__entry.is-expanded .c-accordion__entry-header-link"
                )[0]
          ) {
            self
              .closest(".c-accordion")
              .find(".c-accordion__entry")
              .removeClass("is-expanded")
              .find(".c-accordion__entry-body")
              .slideUp();
          }

          self
            .closest(".c-accordion__entry")
            .toggleClass("is-expanded")
            .attr("aria-expanded", function(i, attr) {
              return attr == "true" ? "false" : "true";
            });

          self.parent().attr("aria-selected", function(i, attr) {
            return attr == "true" ? "false" : "true";
          });

          self
            .parent()
            .next()
            .stop()
            .slideToggle(settings.speed)
            .attr("aria-hidden", function(i, attr) {
              return attr == "true" ? "false" : "true";
            });
        }
      };
    })();

    accordion.init();
  };

  /* READY FUNCTION
  ====================== */

  $(function() {
    MODULE.init_accordion();
  });
})(jQuery, MODULE);

              
            
!
999px

Console