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

              
                - const rand = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min
- const notes = ['c', 'd', 'e', 'f', 'g', 'a', 'b', 'c2']

mixin note(note, i)
  - let h = rand(0, 360)
  - let sound = note === 'c2' ? 2 : note
  .note(style=`--h: ${--h}; --i: ${--i};` class=`note-${note}` data-key=`${sound}`)
    kbd #{note}

main
  .xylophone
    each note, index in notes
      - let sound = note === 'c2' ? 2 : note
      +note(note, index)
      audio(data-key=`${sound}` src=`https://s3-us-west-2.amazonaws.com/s.cdpn.io/1312918/${sound}.wav`)
  p Press letters on keyboard or click to play sounds
    br
    span (press 2 for c2)
    br
  p.license
    a 	&copy; Xylophone Sounds by 
    a(href="https://freesound.org/people/DANMITCH3LL/sounds") DANMITCH3LL is licensed under
     a(href="https://creativecommons.org/licenses/by/3.0/")  CC BY 3.0
              
            
!

CSS

              
                *, *:before, *:after {
  box-sizing: border-box;
}

:root {
  --desired-size: 40;
  --coefficient: 1vmin;
  --size: calc(var(--desired-size) * var(--coefficient));
}

body {
  background-image: linear-gradient(120deg, #fdfbfb 0%, #ebedee 100%);
  height: 100vh;
  display: grid;
  place-items: center;
  margin: 0;
  font-family: sans-serif;
}

main {
  margin: 0;
}

.xylophone {
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  height: calc(1.2 * var(--size));
  width: calc(2.5 * var(--size));
  margin: 0 auto;
}

.xylophone > .note:before,
.xylophone > .note:after,
.xylophone:before,
.xylophone:after{
  position: absolute;
  content: '';
}

.xylophone:before,
.xylophone:after {
  height: 30px;
  z-index: -1;
  width: 100%;
  left: 0;
  background-image: linear-gradient(to right, #434343 0%, black 100%);
}

.xylophone:before {
  top: 10%;
  transform: rotate(4deg);
}

.xylophone:after {
  bottom: 10%;
  transform: rotate(-4deg);
}

.xylophone .note {
  --primary-color: hsla(var(--h, 0), 100%, 50%, 1);
  --offset: calc(var(--i, 0) * 4%); 
  position: relative;
  height: calc(110% - var(--offset));
  width: 10%;
  background: var(--primary-color);
  border-radius: 5px;
  transform: translateY(-15px);
  box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23);
  transition: all 1s ease;
}

.xylophone > .note:before,
.xylophone > .note:after {
  left: 50%;
  transform: translateX(-50%);
  height: 15px;
  width: 15px;
  border-radius: 50%;
  background-image: linear-gradient(120deg, #fdfbfb 0%, #ebedee 100%);
  box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23);
}

.xylophone > .note:before {
  top: 10px;
}

.xylophone > .note:after {
  bottom: 10px;
}

.xylophone kbd {
    height: 100%;
    width: 100%;
    color: #FFF;
    font-size: 2em;
    display: flex;
    align-items: center;
    justify-content: center;
    text-shadow: 2px 2px #000;
    transition: border 0.2s ease;
}

.xylophone kbd:hover,
.xylophone kbd:focus {
  border: 3px solid #ffc600;
  cursor: pointer;
}

.xylophone > .note.playing {
  border-color: #ffc600;
  box-shadow: 10px -5px 2rem #FFC600;
}

main > p {
  text-align: center;
  font-size: 5vmin;
  width: 100vw;
}

main p.license {
  font-size: 2vmin;
  bottom: 10px;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  font-weight: grey;
  opacity: 0.7;
}

main span {
  font-style: italic;
  font-weight: light;
  font-size: 0.75em;
}

main a {
  text-decoration: none;
}
              
            
!

JS

              
                function removeStyles(e) {
  if (e.propertyName !== 'box-shadow') return;
  e.target.classList.remove('playing');
}

function playSound(e) {
  let dataKey = e.key;
  if (!dataKey) {
    dataKey = e.target.textContent === 'c2' ? 2 : e.target.textContent;
  }
  
  const audio = document.querySelector(`audio[data-key="${dataKey}"]`);
  const key = document.querySelector(`.note[data-key="${dataKey}"]`);
  if (!audio) return;

  key.classList.add('playing');
  audio.currentTime = 0;
  audio.play();
}

const keys = Array.from(document.querySelectorAll('.note'));
keys.forEach(key => key.addEventListener('transitionend', removeStyles));
window.addEventListener('keydown', playSound);
window.addEventListener('click', playSound);


// Xylophone Sounds Attribution - Sounds I used in this demo are not mine.
// Author: DANMITCH3LL
// Author URL: https://freesound.org/people/DANMITCH3LL/sounds
// License: Creative Commons Attribution 3.0 Unported
// License URL: http://creativecommons.org/licenses/by/3.0/

              
            
!
999px

Console