Welcome to Coding Torque. In this blog, we are going to create a Random Quote Generator using HTML, CSS & JavaScript. You should create this project if you are a beginner and learning JavaScript. In this app, I have simply used an API to fetch quotes randomly and displayed them using JavaScript DOM functions.
Before we start, here are some more JavaScript projects for you to practice:
1. Filter Cards using JavaScript
2. Typing Effect using JavaScript
3. Email Validator using JavaScript
4. Tip Calculator using JavaScript
5. QR Code Generator using JavaScript
I would recommend you don't just copy and paste the code, just look at the code and type by understanding it.
Let's go step by step:
Step 1: HTML Code
Starter Template
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Font Awesome Icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA=="
crossorigin="anonymous" />
<!-- CSS -->
<link rel="stylesheet" href="style.css">
<title>Random Quotes Generator using JavaScript with Source Code - @code.scientist x @codingtorque</title>
</head>
<body>
<-- Further code here -->
<script src="script.js"></script>
</body>
</html>
Paste the below code in your <body> tag
<div class="container">
<h2>Random Quote</h2>
<p id="quote">"A thing well said will be wit in all languages."
</p>
<p id="author">- John Dryden</p>
<button onclick="generateQuote()" class="generateBtn"><i class="fas fa-sync"></i></button>
</div>
Output Till Now
Step 2: CSS Code
Create a file style.css and paste the code below.
@import url("https://fonts.googleapis.com/css2?family=Poppins&family=Potta+One&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
background: #171717;
color: white;
display: flex;
align-items: center;
justify-content: center;
}
.container {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
background-color: #262626;
text-align: center;
padding: 30px;
width: 30rem;
margin: 10rem;
border-radius: 10px;
}
#quote {
height: 6rem;
margin-top: 20px;
}
.generateBtn {
height: 50px;
width: 50px;
margin-top: 10px;
border-radius: 50%;
border: none;
background: deepskyblue;
color: white;
cursor: pointer;
font-size: 15px;
}
Output Till Now
Step 3: JavaScript Code
Create a file script.js and paste the code below.
const generateQuote = () => {
let url = "https://type.fit/api/quotes";
fetch(url).then(function (response) {
return response.json()
}).then(function (data) {
let randNum = Math.floor((Math.random() * 1500) + 1);
let randomQuote = data[randNum];
document.getElementById("quote").innerHTML = `"${randomQuote.text}"`;
document.getElementById("author").innerHTML = `- ${randomQuote.author ? randomQuote.author : ""}`;
});
}
If you want me to code any project or post any specific post, feel free to DM me at IG @code.scientist or @codingtorque
If you have any doubts or any project ideas feel free to Contact Us
Hope you find this post helpful💖
Follow us on Instagram for more projects like this👨💻