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

              
                <main>
  <h1>Copy me!</h1>
  <div class="click-to-copy-container">
    <button 
      class="click-to-copy"
      title="Click to Copy Email Address"
    >
      hello@juangarcia.com.ar
    </button>
    <span>Copied!</span>
  </div>
  <a href="mailto:hello@juangarcia.com.ar" class="click-to-copy-fallback">
    hello@juangarcia.com.ar
  </a>
</main>
              
            
!

CSS

              
                /* Styling and layout purposes only */
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}

main {
  background: #F9F5FF;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  min-height: 100vh;
}

main h1 {
  font-weight: 600;
  font-size: 34px;
  letter-spacing: -0.4px;
  color: #07070A;
  margin-bottom: 1rem;
}

.click-to-copy-container {
  position: relative;
}

button.click-to-copy, a.click-to-copy-fallback {
  background: #21FA90;
  border: none;
  box-shadow: none;
  padding: 1rem;
  border-radius: .25rem;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.2px;
  color: #07070A;
  transition: all .2s ease-in-out;
  text-decoration: none;
}

button.click-to-copy:hover, a.click-to-copy-fallback: hover {
  opacity: .8;
}

button.click-to-copy + span {
  /* We make the span hidden by default. In this case, using display: none; isn't an issue from an accessibility standpoint, since we don't want screen readers to pick this up unless the user clicks on the button */
  display: none;
  /* From here on, just styling purposes 👇🏻 */
  margin-left: 1rem;
  background: #DADADA;
  padding: .5rem;
  border-radius: .2rem;
  position: absolute;
  top: .75rem;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .4px;
  font-weight: 600;
  color: #666;
}

/* Styles for the tooltip arrow */
button.click-to-copy + span:after {
	right: 100%;
	top: 50%;
	border: solid transparent;
	content: "";
	height: 0;
	width: 0;
	position: absolute;
	pointer-events: none;
	border-color: rgba(218, 218, 218, 0);
	border-right-color: #DADADA;
	border-width: 6px;
	margin-top: -6px;
}

/* When the click event is fired, and the .is-copied class is added to the button, show the tooltip */
button.click-to-copy.is-copied + span {
  display: initial;
}

/* Hide the fallback version on desktop resolutions */
a.click-to-copy-fallback {
  display: none;
}

/* Toggle fallback version for mobile, where the native email apps are way more used and mailto link become way more relevant. I'm using an iPad portrait resolution as the breakpoint, but feel free to play with it! */
@media screen and (max-width: 768px) {
  /* Hide the desktop button */
  button.click-to-copy {
    display: none;
  }
  
  /* Show its replacement */
  a.click-to-copy-fallback {
    display: block;
  }
}
              
            
!

JS

              
                // Select the button from the markup
const button = document.querySelector('.click-to-copy');


// Function that runs on click. It: 
// 1) Prevents the default behavior of the button (refresh the page);
// 2) Runs the copyToClipboard function;
// 3) Adds and removes some CSS classes, used for styling and notifying the user about the copy event
const clickToCopy = (e) => {
  e.preventDefault();
  copyToClipboard(e.currentTarget.textContent);
  e.target.classList.add('is-copied');
  setTimeout(() => { e.target.classList.remove('is-copied') }, 1200);
}

// Copy to clipboard function, taken from https://www.30secondsofcode.org/blog/s/copy-text-to-clipboard-with-javascript/
const copyToClipboard = (str) => {
  const el = document.createElement('textarea');
  el.value = str;
  el.setAttribute('readonly', '');
  el.style.position = 'absolute';
  el.style.left = '-9999px';
  document.body.appendChild(el);
  const selected =
    document.getSelection().rangeCount > 0 
      ? document.getSelection().getRangeAt(0)
      : false;
  el.select();
  document.execCommand('copy');
  document.body.removeChild(el);
  if (selected) {
    document.getSelection().removeAllRanges();
    document.getSelection().addRange(selected);
  }
}

// Fire the event on click
button.addEventListener('click', clickToCopy);
              
            
!
999px

Console