Quote Generator
Here's a complete quote generator using HTML, CSS, and JavaScript:
HTML (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quote Generator</title>
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
</head>
<body>
<div class="container">
<div class="quote-box">
<div class="quote-text">
<i class="fas fa-quote-left"></i>
<span id="quote">Click the button to generate a quote</span>
</div>
<div class="quote-author">
- <span id="author">Unknown</span>
</div>
<div class="buttons">
<button id="new-quote" class="btn">New Quote</button>
<a href="#" id="tweet-quote" class="btn" target="_blank">
<i class="fab fa-twitter"></i> Tweet
</a>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS (styles.css)
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Arial', sans-serif;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f5f5f5;
transition: background-color 0.5s ease;
}
.container {
width: 90%;
max-width: 600px;
padding: 20px;
}
.quote-box {
background-color: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
}
.quote-text {
font-size: 1.5rem;
margin-bottom: 20px;
text-align: center;
}
.quote-text i {
font-size: 1.2rem;
margin-right: 10px;
opacity: 0.7;
}
.quote-author {
text-align: right;
font-style: italic;
margin-bottom: 30px;
font-size: 1.1rem;
color: #555;
}
.buttons {
display: flex;
justify-content: space-between;
}
.btn {
padding: 10px 20px;
border: none;
border-radius: 5px;
background-color: #3498db;
color: white;
cursor: pointer;
font-size: 1rem;
transition: background-color 0.3s ease;
}
.btn:hover {
background-color: #2980b9;
}
#tweet-quote {
text-decoration: none;
display: flex;
align-items: center;
gap: 5px;
}
@media (max-width: 500px) {
.quote-text {
font-size: 1.2rem;
}
.quote-author {
font-size: 1rem;
}
.btn {
padding: 8px 15px;
font-size: 0.9rem;
}
}
JavaScript (script.js)
// Array of quotes
const quotes = [
{
quote: "The only way to do great work is to love what you do.",
author: "Steve Jobs"
},
{
quote: "Life is what happens when you're busy making other plans.",
author: "John Lennon"
},
{
quote: "The future belongs to those who believe in the beauty of their dreams.",
author: "Eleanor Roosevelt"
},
{
quote: "In the middle of every difficulty lies opportunity.",
author: "Albert Einstein"
},
{
quote: "Success is not final, failure is not fatal: It is the courage to continue that counts.",
author: "Winston Churchill"
},
{
quote: "The only limit to our realization of tomorrow is our doubts of today.",
author: "Franklin D. Roosevelt"
},
{
quote: "Do not watch the clock. Do what it does. Keep going.",
author: "Sam Levenson"
},
{
quote: "You miss 100% of the shots you don't take.",
author: "Wayne Gretzky"
},
{
quote: "The best way to predict the future is to invent it.",
author: "Alan Kay"
},
{
quote: "Believe you can and you're halfway there.",
author: "Theodore Roosevelt"
}
];
// DOM elements
const quoteElement = document.getElementById('quote');
const authorElement = document.getElementById('author');
const newQuoteBtn = document.getElementById('new-quote');
const tweetBtn = document.getElementById('tweet-quote');
// Function to get random quote
function getRandomQuote() {
const randomIndex = Math.floor(Math.random() * quotes.length);
return quotes[randomIndex];
}
// Function to display new quote
function displayNewQuote() {
const { quote, author } = getRandomQuote();
quoteElement.textContent = quote;
authorElement.textContent = author;
// Update tweet button
tweetBtn.href = `https://twitter.com/intent/tweet?text=${encodeURIComponent(`"${quote}" - ${author}`)}`;
}
// Event listeners
newQuoteBtn.addEventListener('click', displayNewQuote);
// Display first quote on load
displayNewQuote();
How to Use
- Create three files:
index.html, styles.css, and script.js
- Copy the respective code into each file
- Open the
index.html file in a web browser
Features
- Random quote generation with a click of a button
- Clean, responsive design that works on mobile and desktop
- Twitter sharing functionality
- Smooth transitions and hover effects
- Font Awesome icons for visual appeal
You can easily add more quotes by extending the quotes array in the JavaScript file. The design is responsive and will adapt to different screen sizes.