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

              
                <style id="css"></style>
<div id="content">
  <div id="code">
    <pre><code class="language-css"></code></pre>
  </div>
  <div id="emailWrapper">
    <form id="emailClient">
      <div id="emailClient__newMessage">New Message</div>
      <div id="emailClient__to"><input type="text" name="to" value="" placeholder="your.email@example.com" required focus></div>
      <div id="emailClient__from">Avi Goldman &lt;me@avigoldman.com&gt;</div>
      <div id="emailClient__subject">Interactive email</div>
      <div id="emailClient__html">
        <div id="email">
        </div>
      </div>
      <div id="emailClient__buttons">
        <label id="emailClient__viewCode" for="interactive-hook">Toggle Support</label>
          <button id="emailClient__send">Send Demo</button>
        </div>
    </form>
  </div>
 </div>
              
            
!

CSS

              
                body {
  background: #1D1F20;
}

#content {
  display: flex;  
}

#emailWrapper {
  width: 45%;
  float: left;
  position: relative;
  height: 100vh;
}

#code {
  width: 52%;
  margin-left: 3%;
  margin-top: 6vh;
  height: 88vh;
  background: rgba(0, 0, 200,.1);
  // word-wrap: normal;
  // overflow-wrap: normal;
  
  pre, code {
    height: 100%;
    margin: 0;
  }
  
  code {
    box-sizing: border-box;
    padding: 10px 20px;
    font-size: 14px;
    overflow: scroll;
    display: block;
    color: #fff;
    
    .selector { color: #ddca7e; }
    
    .key { color: #9a8297; }
    
    .value { color: #d0782a; }
    
    .comment { color: #d0782a; }
  }
}

#email input {
  display: block;
}

#email .wrapper:before {
  transform: translate(-100%, 0%);
  top: -10px;
}

#email * {
  position: relative;
  margin-bottom: 10px;
  display: inline-block;
  transition: all .5s;

  &[id]:before {
    content: '#' attr(id);
  }

  &[class]:not([class='wrapper']):before {
    content: '.' attr(class);
  }

  &[id][class]:not([class='wrapper']):before {
    content: '#' attr(id) '.' attr(class) '';
  }

  &:before {
    font-size: 0.8em;
    white-space: nowrap;
    color: #666;
    position: absolute;
    top: 50%;
    left: -6px;
    background: #efefef;
    padding: 1px 2px;
    border: 1px solid #ddd;
    transform: translate(-100%, -50%);
    box-shadow: 0px 2px 4px 0px rgba(0,0,0,.2);
  }
}

#email *:not(.wrapper) {
    &:after {
    content: "";
    display: block;
    border-radius: 50%;
    visibility: hidden;
    border: 3px solid #37aadc;
    box-shadow: 0px 0px 18px rgba(55, 170, 220, 0.8), 0px 0px 3px rgb(231, 235, 236);
    transform: translate(-50%, -50%);
    position: absolute;
    top: 50%;
    left: 50%;
  }

  &:after {
    animation: look-at-me .75s ease-out 0s;
  }
}

@keyframes look-at-me {
    0% {
      min-width: 5px;
      width: 100%;
      padding-top: 100%;
      visibility: visible;
      opacity: 0;
    }
  
    10% {
      opacity: 1;
    }
  
    100% {
      min-width: 30px;
      width: 130%;
      padding-top: 130%;
      opacity: 0;
    }  
}

#emailClient {
  border: 1px solid #000;
  background: #fff;
  box-shadow: 0px 0px 6px 1px rgba(0,0,0,.3);
  position: absolute;
  bottom: 0;
  font-size: 13px;
  width: 95%;
  max-width: 600px;
  right: 30px;
  
  &__newMessage {
    background: #343434;
    color: white;
    padding: 10px 12px;
  }
  
  &__to, &__from, &__subject {
    padding: 10px 12px;
    
    &:before {
      content: "To";
      color: #777;
      width: 60px;
      display: inline-block;
    }
  }
  
  &__to input {
    height: 100%;
    width: calc(100% - 68px);
    font: inherit;
    display: inline-block;
    border: none;
    outline: 0;
    background: transparent;
  }
  
  &__from {
    &:before {
      content: "From";
    }
  }
  
  &__subject {
    &:before {
      content: "Subject";
    }
    
    border-bottom: 1px solid #CCC;
    padding: 12px 12px;
  }
  
  &__html {
    padding: 12px 12px;
    min-height: 200px;
    overflow: show;
  }
  
  &__buttons {
    padding: 6px;
    background: #f5f5f5;
    border-top: 1px solid rgba(0,0,0, 0.1);
    display: flex;
    justify-content: space-between;
  
    > * {
      background: #e3e3e3;
      padding: 8px 10px;
      border: 1px solid rgba(0,0,0,.1);
      font-size: 12px;
      font-weight: 600;
      font-family: Arial, san-serif;
      color: #474d53;
      border-radius: 2px;
      cursor: pointer;
      margin-right: 10px;
      outline: 0;
    }
  }
  
  & &__send {
    background: #FA6423;
    color: #eee;
  }
}


              
            
!

JS

              
                const divider = '------------------------------';
let turk = {
  queue: [],
  
  styleTarget: '#css',
  
  presentationTarget: '#code pre code',
  
  htmlTarget: '#email',
  
  els: {},
  
  wait(delay) {
    this.queue.push({ delay });
  }, 
  
  space(lines = 1) {
    this.queue.push({ type: 'space', contents: Array(lines+1).join('\n') });
  }, 
  
  progressBar(delay) {
    const spaces = 20;
    const otherChars = '/*  */'.length;
    
    this.addComment({
      contents: Array(spaces).join('='),
      speed: delay/(spaces+otherChars)  });
  },
  
  addHTML({ action = 'append', contents = '<br>', target = this.htmlTarget, delay = 750 }) {
    this.queue.push({
      type: 'html',
      action,
      contents,
      target,
      delay
    });
  },
  
  addCSS({ contents = '', delay, speed }) {
    this.queue.push({
      type: 'css',
      contents,
      delay,
      speed
    });
  },
  
  addComment({ contents = '', delay, speed }) {
    this.queue.push({
      type: 'comment',
      contents,
      delay,
      speed
    });
  },
  
  run() {
    this.els = {
      style: $(this.styleTarget),
      presentation: $(this.presentationTarget),
      css: $(`${this.styleTarget}, ${this.presentationTarget}`),
      html: $(this.htmlTarget)
    };
    
    this.next();
  },
  
  next() {
    if (this.queue.length === 0) {
      return console.log('done');
    }
    
    let typingPromise = Promise.resolve();
    const item = this.queue.shift(); 

    switch (item.type) {
      case 'space':
        typingPromise = write(this.els.css, item.contents);
        break;
        
      case 'comment':
        let comment = item.contents.split('\n').length > 1 ?
          `/* ${item.contents.replace(/\n/gm, '\n * ')}\n */\n\n` :
          `/* ${item.contents} */\n`; 

        typingPromise = write(this.els.css, comment, item.speed);
        break;

      case 'css':
        typingPromise = write(this.els.css, `${this.htmlTarget} ${item.contents}\n\n`, item.speed);
        break;

      case 'html':
        if (item.action === 'append') {
          $(item.contents).appendTo(item.target);
        }

        if (item.action === 'wrap') {
          $(item.target).wrapAll(item.contents);
        }

        if (item.action === 'prepend') {
          $(item.target).prepend(item.contents);
        }

        break;
    }

    typingPromise
      .then(() => wait(item.delay || 300))
      .then(() => { this.next(); });
  }
};


function wait(time = 0) {
  return new Promise((resolve, reject) => {
    setTimeout(() => { resolve() }, time);
  });
}

function scrollToBottom() {
  // turk.els.presentation.animate({ scrollTop: turk.els.presentation[0].scrollHeight });
}

function write($target, str, speed = 40) {
  if (str.length === 0) {
    return Promise.resolve();
  }
  
  return new Promise((resolve, reject) => {
    $target.append(str.charAt(0));
    scrollToBottom($target);
    
    setTimeout(() => {
       write($target, str.substr(1), speed).then(resolve);
    }, speed);
  })
}


// building the queue

turk.addComment({ contents: `Hey there!
Lets learn about interactive email.


Interactive email
> An interactive email has the functionality for users
> to take an action within the email that triggers an
> event within the same email.` });

turk.addComment({ contents: `How does that work?
Email clients like gmail strip out JavaScript to protect
us from dangerous senders. But there are some pretty cool
CSS hacks out there to still make emails interactive.

We use some checkbox magic!

We need a client that supports 3 things
1. the \`<input />\` tag
2. the \`:checked\` selector
3. the \`+\` and \`~\` selectors to target siblings` });

turk.space();

turk.wait(300);

turk.addComment({ contents: `Lets get started
${divider}` });

turk.addComment({ contents: `We'll start with a label....` });
turk.space();
turk.addHTML({ contents: '<label for="button-hook" id="button">Click me!</label>' });

turk.addComment({ contents: `a checkbox....` });
turk.space();
turk.addHTML({ contents: '<input type="checkbox" id="button-hook" />' });

turk.addComment({ contents: `and content we want to show after we click the 'button'....` });
turk.space();
turk.addHTML({ contents: '<br><div id="magic-content">You clicked the button!</div>' }); 

turk.addComment({ contents: `Lets make our label look like a button.` });
turk.addCSS({ contents: `#button {
  background: coral;
  color: white;
  padding: 0.67em 1em;
  display: inline-block;
  cursor: pointer;
}` });


turk.addComment({ contents: `Hmm, lets make that easier to see.` });
turk.addCSS({ contents: `* {
  font-size: 16px;
}` });

turk.space();
turk.addComment({ contents: `Next we'll hide the #magic-content.` });
turk.addCSS({ contents: `#magic-content {
  display: none;
}`})


turk.addComment({ contents: `Now lets show the \`#magic-content\` when the checkbox is checked.` });
turk.addCSS({ contents: `#button-hook:checked ~ #magic-content {
  display: inline-block;
}`});
turk.space();

turk.addComment({ contents: `Give it a try!` });
turk.progressBar(8000);
turk.space(3);

turk.addComment({ contents: `Ok, but this doesn't work everywhere.
We need to create a fallback for those poor souls that won't get our
awesome super advanced email.` });


turk.addComment({ contents: `We'll wrap our interactive content in a div
with the id \`#interactive\`...` })
turk.addHTML({ contents: `<div class="wrapper" id="interactive" />`, action: 'wrap', target: `${turk.htmlTarget} *` });
turk.space();

turk.addComment({ contents: `add a checked checkbox right before \`#interactive\`...` });
turk.addHTML({ contents: `<input type="checkbox" id="interactive-hook" checked />`, action: 'prepend', target: turk.htmlTarget });
turk.space();

turk.addComment({ contents: `and we'll hide the interactive content by default.` });
turk.addCSS({ contents: `#interactive {
  display: none;
}`});

turk.addComment({ contents: `Next, we'll override that when its next to a checked checkbox...which it is!` });
turk.addCSS({ contents: `#interactive-hook:checked ~ #interactive {
    display: block;
}`});

turk.addComment({ contents: `Now if the client support the css we need it will work but
if it doesn't our interactive content won't show.` });


turk.addComment({ contents: `Lastly, we need some fallback content.` });
turk.addHTML({ contents: `<br><span id="fallback">Dude, get a better email client.</span>` });
turk.space();

turk.addComment({ contents: `Lets apply the same idea as before and flip the styles.
We'll show \`#fallback\` by default and hide it if its next to a checked checkbox.` });
turk.addCSS({ contents: `#interactive-hook:checked ~ #fallback {
  display: none;
}`});

turk.addComment({ contents: `Lets clean it all up...` });
turk.addCSS({ contents: `#button-hook, #interactive-hook {
  display: none !important;
}`});

turk.addComment({ contents: `Wooo! We successfully made an interactive email with a safe fallback` });

// turk.addHTML({ contents: `<div>Sign up for <a href="https://sparkpost.com">SparkPost</a> to start sending your awesome, interactive emails</div>` });

turk.run();

              
            
!
999px

Console