Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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

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.

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 BEM Accordion

.sidebar#js-sidebar-1(data-block="sidebar")
  .sidebar__item.js-accordion--open
    .sidebar__title.js-accordion--open
      h2 Responsive Accordion
    .sidebar__content.js-accordion--open
      p Here is my attempt at creating a object oriented-ish accordion using BEM CSS guidlines. This accordion is fully responsive and completely barebones, making it easy to style however you want.
      .inner#js-sidebar-2(data-block="inner")
        .inner__item
          .inner__title
            h2 Implementation
          .inner__content
            p Use #[strong @mixin accordion()] to apply the base styles necessary to make the accordion work. Then initialize the accordion with js.
            p By default, the CSS mixin is super barebones, but you can pass the value #[strong 'base'] to the mixin to apply some base styles. You can always add your own presets to the mixin.
            p Also make sure to add #[strong data-block] to your accordion with the name used for your block. This data-attribute is used as a naming hook for js.
        .inner__item
          .inner__title
            h2 Things to Note
          .inner__content
            p Because the accordion works by adjusting the __content element's height, #[strong no vertical padding] can be applied to the __content element. To help prevent any issues, I've added !important styles to prevent vertical padding. If you need control of spacing in the __content element, I recommend having all content wrapped in another div.
            p The style #[strong .js-accordion--open] class is used to designate the open state. This class gets applied to 3 elements in the accordion block: __item, __title, & __content.
  .sidebar__item
    .sidebar__title
      h2 Height Auto Transition
    .sidebar__content
      p Accordion transitions are handled by CSS3 transitions. To make the accordion responsive, I've implemented a way to basically pull off a height: 0; to height: auto; transition.
      p This transition is pulled off by expanding __content to the element's scrollHeight and then setting the height to auto using transitionend. The reverse animation is achieved using a 4ms delay between reassignign the content height and animating the height back to 0.
  .sidebar__item
    .sidebar__title
      h2 Data Attribute vs JS-Class
    .sidebar__content
      p I bounced back and forth between applying the .js-accordion--open class and using a data-state. I stuck with a simple class since jsperf indicates data-attributes still take a performance hit.
      p Ultimately, neither trigger case adheres directly to the BEM guidelines. I wanted to be able simply use modifiers (ex: .accordion__content--open), but filtering through a sea of element--modifier combinations or using i-bem was something I didn't want to tackle.
 
br
h1 Barebone
 
.barebone#js-barebones(data-block="barebone")
  .barebone__item
    .barebone__title
      h2 Barebones Title 1
    .barebone__content
      p Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et dui sit amet massa vehicula semper id eu neque. Nulla venenatis consectetur mauris. Integer eu ornare eros. In malesuada viverra urna sed vehicula.
  .barebone__item
    .barebone__title
      h2 Barebones Title 2
    .barebone__content
      p Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam id lorem vestibulum, ornare ipsum vel, varius magna. Integer venenatis nibh urna, et hendrerit felis venenatis in.
  .barebone__item
    .barebone__title
      h2 Barebones Title 3
    .barebone__content
      p Curabitur accumsan augue eget purus lacinia, at fringilla velit vestibulum. Aenean iaculis enim elit. Vestibulum convallis, massa et vestibulum feugiat, leo ipsum commodo libero, vel lacinia diam libero vel purus.
              
            
!

CSS

              
                @mixin accordion($preset: 'none') {
  &__title{
    cursor: pointer;
    user-select: none;
  }

  &__content {
    height: 0;
    padding-top: 0 !important;
    padding-bottom: 0 !important;
    overflow: hidden;
    transition: height linear .1s;

    &.js-accordion--open{
      height: auto;
    }
  }
  
  
  //base preset
  @if $preset == 'base'{
    font-family: Arial, sans-serif;
    font-size: 16px;
    background-color: white;
    
    &__item{
      border: 1px solid #ddd;
      border-top: 0;
      
      &:first-child{
        border-top: 1px solid #ddd;
      }
    }
    
    &__title{
      position:relative;
      padding:15px 20px;
      transition: all ease-out .25s;
      font-weight: bold;
      padding-right:60px;
      
      &::after, &:after{
        position:absolute;
        content: '+';
        right: 14px;
        top: 50%;
        transform: translate(0%, -50%);
        width:22px;
        height:22px;
        text-align:center;
        background-color:white;
        color:#666;
        border-radius:100%;
        line-height:23px;
      }
      
      &.js-accordion--open{
        background-color: #eee;
        
        &::after, &:after{
          content: '–';
          line-height:20px;
        }
      }
    }
    
    &__content{
      padding-left:20px;
      padding-right:20px;
      font-size:14px;
      
      padding-top: 0;
      padding-bottom: 0;
      
      transition: height ease-out .25s;
    }
  }
}







//example
%example-centering{
  max-width:464px;
  width:86%;
  margin:20px auto;
}

.barebone{
  @include accordion();
  @extend %example-centering;
}

.sidebar{
  @include accordion('base');
  @extend %example-centering;
}

.inner{
  @include accordion('base');
  margin-bottom:20px;
}

.sidebar, .inner{
  p{
    margin-bottom: 20px;
    color: #777;
    line-height: 20px;
    
    &:first-child{
      margin-top:20px;
    }
    
    strong{
      font-weight:bold;
      color:#444;
    }
  }
}








//Unrelated Styles
body {
  background-color:#2bba9d;
}

h1 {
  margin-top:20px;
  font-family: Arial, sans-serif;
  color: white;
  text-transform: uppercase;
  font-size: 35px;
  font-weight: bold;
  text-align: center;
}
              
            
!

JS

              
                $.fn.accordion = function(options) {
  var settings = $.extend({
    autoCollapse: false
  }, options );
  
  var
    $accordion   = $(this),
    blockName    = $accordion.attr('data-block'),
    $items       = $('.' + blockName + '__item', $accordion);
  
  $accordion.delegate('.' + blockName + '__title', 'click', triggerAccordion);
  
  function triggerAccordion(){
    var
      $that         = $(this),
      $parent       = $that.parent(),
      $content      = $parent.children('.' + blockName + '__content'),
      isOpen        = $that.hasClass('js-accordion--open'),
      autoCollapse  = true,
      contentHeight = $content.prop('scrollHeight');
    
    if(isOpen){
      $that.removeClass('js-accordion--open');
      $parent.removeClass('js-accordion--open');
      $content.css('height', contentHeight);
      setTimeout(function(){
        $content.removeClass('js-accordion--open').css('height', '');
      }, 4);
    }else{
      if(settings.autoCollapse){
        //auto collapse open accordions
      }
      $that.addClass('js-accordion--open');
      $parent.addClass('js-accordion--open');
      $content.addClass('js-accordion--open').css('height', contentHeight).one('webkitTransitionEnd', event, function(){
        if(event.propertyName === 'height'){
          $(this).css('height', '');
        }
      });
    }
  }
};

$(document).ready(function(){
  $('#js-sidebar-1').accordion();
  $('#js-sidebar-2').accordion();
  $('#js-barebones').accordion();
});
              
            
!
999px

Console