cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <button class="btn js-btn">Give me a random Chuck Norris joke</button>
<main class="main js-main"></main>
<p class="loading js-loading">Loading…</p>
            
          
!
            
              * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}

.btn {
  margin: 1rem;
  width: calc(100vw - 2rem);
  height: 3rem;
  font: inherit;
  background-color: #AD1457;
  color: #fff;
  border: 0;
  border-radius: 4px;
}

.main {
  padding: 0 3rem;
  width: calc(100vw - 2rem);
  margin: 0 1rem;
  height: calc(100vh - 6rem);
  border-radius: 4px;
  box-shadow: inset 0 0 0 1px #AD1457;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 2rem;
  font-style: italic;
  font-weight: 300;
  color: #444;
  text-align: center;
}

.loading {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  display: none;
  justify-content: center;
  align-items: center;
  color: #fff;
  background: rgba(0,0,0,.9);
  font-size: 2rem;
  font-style: italic;
  font-weight: 300;
}

.loading--active {
  display: flex;
}
            
          
!
            
              // DOM references
var btn = document.querySelector('.js-btn');
var main = document.querySelector('.js-main');
var loading = document.querySelector('.js-loading');

function getRandomJoke() {

  // show loading screen
  loading.classList.add('loading--active');

  var xhr = new XMLHttpRequest();
  xhr.open('GET', 'https://api.icndb.com/jokes/random/1');
  xhr.responseType = 'json';

  xhr.onload = function onload() {
    // print a joke
    main.innerHTML = xhr.response.value[0].joke;

    // hide loading screen
    loading.classList.remove('loading--active');
  };

  xhr.onerror = function onerror() {
    // print an error
    main.textContent = 'Error :-(';

    // hide loading screen
    loading.classList.remove('loading--active');
  };

  xhr.send();
}

btn.addEventListener('click', getRandomJoke);
            
          
!
999px
Loading ..................

Console