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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <div id="expandy-bit" class="z-depth-2">
  <a href="" class="expandy-control">
    <span>Click me to Expand/Close! </span> 
    <span class="fa fa-chevron-right expand-arrow"></span></a>
  
  <div id="content">
    <div class="actions">
      <button class="btn add-stuff"><span class="fa fa-plus"></span> Add Stuff</button>
      <button class="btn remove-stuff"><span class="fa fa-minus"></span> Remove Stuff</button>
    </div>
    
    <div class="stuff">
        <!-- This is where the new elements will be added -->
    </div>
    
  </div>
</div>
              
            
!

CSS

              
                body {
  margin-top: 50px;
}

#expandy-bit {
  width: 350px;
  margin: auto;
  padding: 1rem;
}

.expandy-control {
  display: flex;
  justify-content: space-between;
  padding: 1rem 2rem;
}

.expand-arrow {
  text-align: right;
}

.stuff {
  padding-top: 2rem;
}

.actions {
  display: flex;
  justify-content: space-around;
}

#content {
  height: 0px;
  overflow: hidden;
  transition: height 300ms;
}

.thing {
  padding: 2rem;
  border: 1px solid lightgrey;
  margin-bottom: 1rem;
}


              
            
!

JS

              
                const content = document.getElementById('content')
const expander = document.getElementsByClassName('expandy-control')[0]
const expandArrow = document.getElementsByClassName('expand-arrow')[0]
const addBtn = document.getElementsByClassName('add-stuff')[0]
const removeBtn = document.getElementsByClassName('remove-stuff')[0]
const stuffContainer = document.getElementsByClassName('stuff')[0]

//state of the expandable bit
let expandedState = false
//holds the dynamically added elements
const stuff = []
//store the height of the stuff container
let stuffContainerHeight = 66


expander.addEventListener('click', evt => {
  evt.preventDefault()

  expandedState = !expandedState;
  
  if(expandedState) {
    content.style.height = stuffContainerHeight + 'px'
    expandArrow.classList.replace('fa-chevron-right', 'fa-chevron-down')
    //change the height to 'auto' after the transition is complete
    setTimeout(() => content.style.height = 'auto', 301)
  } else {
    //change height back to specific before collapsing so transition will work
    content.style.height = stuffContainerHeight + 'px'
    expandArrow.classList.replace('fa-chevron-down', 'fa-chevron-right')
    //set height back to 0 after sleight delay to allow for the above specific height change to be implemented
    setTimeout(() => content.style.height = '0', 10)
  }
})

//dynamically add a new thing
addBtn.addEventListener('click', evt => {
  const newStuff = document.createElement('div')
  newStuff.className = 'thing'
  newStuff.appendChild(document.createTextNode("New Stuff"))
  stuffContainer.appendChild(newStuff)
  
  //add the new element to the stuff array so we can remove it easily
  stuff.push(newStuff)
  //store the new container height
  stuffContainerHeight = stuffContainer.offsetHeight
})


removeBtn.addEventListener('click', () => {
  //remove the last thing
  if(stuff.length > 0) {
    stuff.pop().remove()
  }
  
  // store the new height
  stuffContainerHeight = stuffContainer.offsetHeight
})

              
            
!
999px

Console