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

              
                <div id="text">
<!-- content generated with JS -->  
</div>
              
            
!

CSS

              
                $background: #efefef;
$primary_font: 'Fredoka One', sans-serif;
$first_color: rgba(0, 0, 255, 0.5);
$second_color: rgba(255, 0, 0, 0.5);

html, body {
  background-color: $background;
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
}

#text {
  font-family: $primary_font;
  font-size: 6em;
  line-height: 1em;
  text-align: center;
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  &:hover {
    cursor: default;
  }
  
  .wrapper {
    display: inline-block;
    top: -900px;
    position: relative;
    height: 150px; /* default */
    width: 90px; /* default */
    transition: ease 0.3s all;
    
    span {
      position: absolute;
      top:0;
      right:0;

      transition: ease 0.3s all;
      
      &.letter-2 {
        color: $first_color;
      }

      &.letter-1 {
        color: $second_color;
        z-index: 1;
        
        &:hover {
           top: -3px;
           right: -3px;
           
           ~ .letter-2 {
             top: 3px;
             right: 3px;
           }
        }
      }
      
      &.space {
        padding: 0;
        min-width: 30px;
        display: inline-block;
      }
    }
  }
}
              
            
!

JS

              
                // type anything here
const text = 'hover me';

// this function turns a string into an array
const createLetterArray = (string) => {
  return string.split('');
}

// this function creates letter layers wrapped in span tags
const createLetterLayers = (array) => {
  return array.map((letter) => {
      let layer = '';
      //specify # of layers per letter
      for (let i = 1; i <= 2; i++) {
        // if letter is a space
        if(letter == ' '){
          layer += '<span class="space"></span>';
        }else{
          layer += '<span class="letter-'+i+'">'+letter+'</span>';
        }
      }
      return layer;
  });
}

// this function wraps each letter in a parent container
const createLetterContainers = (array) => {
  return array.map((item) => {
    let container = '';
    container += '<div class="wrapper">'+item+'</div>';
    return container;
  });
}

// use a promise to output text layers into DOM first
const outputLayers = new Promise(function(resolve, reject) {
      document.getElementById('text').innerHTML = createLetterContainers(createLetterLayers(createLetterArray(text))).join('');
      resolve();
});

// then adjust width and height of each letter
const spans = Array.prototype.slice.call(document.getElementsByTagName('span'));
outputLayers.then(() => {
    return spans.map((span) => {
      setTimeout(() => {
        span.parentElement.style.width = span.offsetWidth+'px';
        span.parentElement.style.height = span.offsetHeight+'px';
      }, 250);
    });  
}).then(() => {
    // then slide letters into view one at a time
    let time = 250;
    return spans.map((span) => {
      time += 75;
      setTimeout(() => {
        span.parentElement.style.top = '0px';
      }, time);
    });
});
              
            
!
999px

Console