Quotes Generator Using JavaScript with Source Code


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Quotes Generator</title>
    <link rel="stylesheet" href="style.css" type="text/css" />
  </head>
  <body>
    <div class="container">
      <div class="quote"></div>
      <div class="author"></div>
    </div>
    <script src="./script.js" type="text/javascript"></script>
  </body>
</html>

@import url('https://fonts.googleapis.com/css2?family=PT+Sans&display=swap');
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
*::before,
*::after {
    box-sizing: border-box;
}
html,
body {
    font-size: 10px;
    font-family: 'PT Sans','lato', Arial, Helvetica, sans-serif;
}
body {
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}
.container {
    padding: 3.5rem;
    max-width: 70rem;
}
.quote {
    font-size: 3rem;
    margin-bottom: 3rem;
}
.author {
    font-size: 2rem;
    font-style: italic;
    letter-spacing: 1px;
}

const quoteElement = document.querySelector('.quote');
const authorElement = document.querySelector('.author');


const link = `https://api.quotable.io/random`;

function getQuote(){
    fetch(link)
        .then(response => {
            let data = response.json();
            return data;
        })
        .then(data => {
            quoteElement.innerText = `${data.content}`;
            authorElement.innerText = `${data.author}`;
        })
        .catch(error => {
            console.log(error);
        });
}

window.addEventListener('load', getQuote);

Leave a Reply