<main>
  <h1>Copy me!</h1>
  <div class="click-to-copy-container">
    <button 
      class="click-to-copy"
      title="Click to Copy Email Address"
    >
      [email protected]
    </button>
    <span>Copied!</span>
  </div>
  <a href="mailto:[email protected]" class="click-to-copy-fallback">
    [email protected]
  </a>
</main>
/* 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;
  }
}
// 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);
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.