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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <div id="bg">
    <img src="https://unsplash.it/1280/720/?random" alt="">
</div>
<div id="quote-container" class="container">
    <div class="row">
        <div class="col">
            <blockquote>
                <p></p>
                <cite></cite>
            </blockquote>
        </div>
    </div>
    <div class="row">
        <div class="col text-center" id="controls">
            
        </div>
    </div>
</div>
              
            
!

CSS

              
                @mixin centered {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translateY(-50%) translateX(-50%);
}

body {
    margin: 0;
    padding: 0;
    font-family: 'Tangerine', cursive;
    font-size: 3em;
    line-height: 1em;
}

blockquote { margin: 0; }

p { margin-top: 0; }

cite {
    float: right;
}

button {
    cursor: pointer;
}

#bg {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    text-align: center;
  
    img {
        @include centered;
        min-height: 100%;
        min-width: 100%;
    }
}

#quote-container {
  @include centered;
  background-color: rgba(255, 255, 255, 0.8);
  padding: 1em;

  button#twitter-button {
    margin-left: 10px;

    a {
        color: #fff;
        text-decoration: none;
    }
  }
}

              
            
!

JS

              
                'use strict'

const twitter_tweet = 'https://twitter.com/intent/tweet'

const get_quote = () => {

    make_quote_request().then(
    (resolve) => {
        show_quote(resolve)
    },
    (reject) => {
        console.log(reject)
    })

}

const show_quote = (data) => {

    let quote_container = document.getElementById('quote-container'),
        quote_main = quote_container.getElementsByTagName('p')[0],
        quote_author = quote_container.getElementsByTagName('cite')[0]
    quote_main.innerHTML = data.quoteText
    quote_author.innerHTML = data.quoteAuthor

    if(!document.getElementById('reload-button')) {
        show_reload_button()
    }

    if(!document.getElementById('twitter-button')) {
        show_twitter_button(data.quoteText, data.quoteAuthor)
    } else {
        update_twitter_button(data.quoteText, data.quoteAuthor)
    }

}

const show_reload_button = () => {

    let button = document.createElement('button')
    button.innerHTML = 'New random quote'
    button.setAttribute('id', 'reload-button')
    button.className = 'btn btn-success btn-sm'
    button.addEventListener('click', () => get_quote() )

    document.getElementById('controls').appendChild(button)

}

const show_twitter_button = (text, author) => {

    let button = document.createElement('button'),
        link = twitter_tweet + '?text=' + text + ' ' + author

    button.innerHTML = '<a target="_blank" href="' + link + '"><i class="fa fa-twitter" aria-hidden="true"></i> Tweet</a>'
    button.setAttribute('id', 'twitter-button')
    button.className = 'btn btn-primary btn-sm'

    document.getElementById('controls').appendChild(button)

}

const update_twitter_button = (text, author) => {

    let button = document.getElementById('twitter-button'),
        button_link = button.getElementsByTagName('a')[0],
        link = twitter_tweet + '?text=' + text + ' ' + author
        
    button_link.setAttribute('href', link)
    
}

const make_quote_request = () => {

    return new Promise ( (resolve, reject) => {
        let req = new XMLHttpRequest()
        req.open('GET', 'https://cors-anywhere.herokuapp.com/https://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en', true)
        req.responseType = 'json'

        req.onload = () => {
            if (req.status >= 200 && req.status < 400) {
                resolve(req.response)
            } else {
                reject ('Server error')
            }
        }

        req.onerror = () => reject('error')

        req.send()
    })

}

window.addEventListener('DOMContentLoaded', () => {
    get_quote()
})

              
            
!
999px

Console