Creating a quote generator
The user wants a random quote generator using an API with animated transitions in HTML, JS, and CSS. I think they’re looking for sample code in one HTML file or possibly separated files. I’ll fetch random quotes from an API like https://api.quotable.io/random or https://type.fit/api/quotes, ensuring to handle CORS issues. I’ll include animations like fade in/out and button hovers. I'll provide a complete example with markup, styling, and functionality so the user can easily copy and run it.Planning quote generator features
I'm thinking about adding an animated background color change with each new quote. Since the user wants a random quote generator with animated transitions, it seems they might appreciate a visually appealing design. I'll explain the basics while being concise and mention important aspects like CORS, asynchronous fetching, and error handling.
For the HTML layout, I'll include a centered quote box with the quote text, author, and a "New Quote" button. I may also consider adding a loading feature. For animations, I'll use CSS classes for fading effects, considering both class-based transitions and keyframes to enhance the experience.Refining animated transitions
I'm considering a way to restart animations by simply changing the class. Instead of toggling fade-in/out, using CSS keyframes for a minimal approach seems better. I’ll define keyframes for a fade-in effect that moves the quote up slightly during the transition. To make it straightforward for educational purposes, I’ll use an animation property to handle this.
Additionally, I'll animate the button hover for a slight lift. I'll also incorporate a subtle overlay and ensure to fetch data from the API smoothly. This is a nice pattern to teach!Implementing the quote-fetching function
I’m implementing the quote-fetching function using the Quotable API. The code will disable the button while loading to prevent multiple clicks, and it’ll update the button text to “Loading...” during the fetch. If the fetch is successful, it displays the quote and the author; if it fails, an error message appears.
I'll also randomize an accent color for each quote by defining an array of colors and applying a random color to CSS variables. This way, the button background and other elements will update nicely!Crafting the HTML/CSS/JS structure
Alright, I'm thinking of creating a simple and effective random quote generator using HTML, JS, and CSS without going overboard—even though a little flair is nice. I’ll integrate the CSS into a <style> tag within the HTML file for simplicity. The user didn’t request lengthy explanations, so I’ll provide brief bullet points along with the code.
I’ll make it a single HTML file with everything included, and I can also mention Google Fonts for a stylish touch, since it's optional and aligns with the user's request. Let's design a gradient background, set up a container for the quotes, and add animations using CSS keyframes. I'll write out the final code! Below is a complete, self-contained example of a random quote generator that:
- Fetches quotes from an API (
https://api.quotable.io/random)
- Has animated quote transitions (fade + slide in)
- Uses only HTML, CSS, and vanilla JavaScript
You can save this as index.html and open it in your browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Random Quote Generator</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Optional: Google Font for nicer typography -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap"
rel="stylesheet"
>
<style>
:root {
--accent-color: #ff6b6b;
--bg-gradient: radial-gradient(circle at top left, #1b2735, #090a0f);
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: "Poppins", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: var(--bg-gradient);
color: #f5f5f5;
padding: 1rem;
}
.quote-wrapper {
max-width: 600px;
width: 100%;
}
.quote-card {
background: rgba(15, 23, 42, 0.92);
border-radius: 18px;
padding: 2.5rem 2.25rem 2rem;
box-shadow:
0 22px 60px rgba(0, 0, 0, 0.5),
0 0 0 1px rgba(148, 163, 184, 0.15);
position: relative;
overflow: hidden;
}
/* Decorative gradient glow */
.quote-card::before {
content: "";
position: absolute;
inset: -2px;
pointer-events: none;
background: conic-gradient(
from 180deg at 50% 50%,
rgba(248, 250, 252, 0.0),
rgba(248, 250, 252, 0.15),
rgba(248, 250, 252, 0.0) 40%
);
opacity: 0.6;
mix-blend-mode: soft-light;
}
.quote-header {
display: flex;
align-items: center;
gap: 0.5rem;
margin-bottom: 1.25rem;
font-size: 0.85rem;
letter-spacing: 0.17em;
text-transform: uppercase;
color: rgba(148, 163, 184, 0.9);
}
.quote-dot {
width: 9px;
height: 9px;
border-radius: 999px;
background: var(--accent-color);
box-shadow: 0 0 12px rgba(248, 113, 113, 0.7);
}
.quote-body {
position: relative;
padding-left: 1.5rem;
margin-bottom: 1.75rem;
}
.quote-mark {
position: absolute;
left: 0;
top: -0.3rem;
font-size: 2.8rem;
line-height: 1;
color: rgba(148, 163, 184, 0.5);
transform: translateY(-0.1rem);
}
#quote {
font-size: 1.25rem;
line-height: 1.7;
font-weight: 400;
color: #e5e7eb;
text-shadow: 0 1px 2px rgba(15, 23, 42, 0.75);
}
#author {
text-align: right;
font-size: 0.95rem;
margin-top: 0.9rem;
color: rgba(156, 163, 175, 0.95);
font-style: italic;
}
.quote-footer {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
}
.hint {
font-size: 0.8rem;
color: rgba(148, 163, 184, 0.9);
}
button#new-quote {
padding: 0.7rem 1.6rem;
border-radius: 999px;
border: none;
cursor: pointer;
font-size: 0.95rem;
font-weight: 600;
letter-spacing: 0.06em;
text-transform: uppercase;
background: var(--accent-color);
color: #0b1120;
box-shadow:
0 14px 24px rgba(248, 113, 113, 0.5),
0 0 0 1px rgba(248, 250, 252, 0.15);
display: inline-flex;
align-items: center;
gap: 0.4rem;
transition:
transform 0.16s ease,
box-shadow 0.16s ease,
filter 0.16s ease,
background 0.3s ease;
}
button#new-quote span.arrow {
display: inline-block;
transform: translateX(0);
transition: transform 0.16s ease;
}
button#new-quote:hover:not(:disabled) {
transform: translateY(-1px);
box-shadow:
0 18px 32px rgba(248, 113, 113, 0.6),
0 0 0 1px rgba(248, 250, 252, 0.22);
filter: brightness(1.06);
}
button#new-quote:hover:not(:disabled) span.arrow {
transform: translateX(3px);
}
button#new-quote:active:not(:disabled) {
transform: translateY(0);
box-shadow:
0 8px 16px rgba(15, 23, 42, 0.7),
0 0 0 1px rgba(248, 250, 252, 0.2);
filter: brightness(0.98);
}
button#new-quote:disabled {
cursor: wait;
opacity: 0.7;
box-shadow: none;
}
/* Quote enter animation */
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(12px);
filter: blur(3px);
}
to {
opacity: 1;
transform: translateY(0);
filter: blur(0);
}
}
.animate-quote {
animation: fadeInUp 0.45s ease-out;
}
/* Subtle page fade-in */
@keyframes pageFadeIn {
from {
opacity: 0;
transform: translateY(6px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.quote-wrapper {
animation: pageFadeIn 0.6s ease-out;
}
@media (max-width: 600px) {
.quote-card {
padding: 2rem 1.5rem 1.6rem;
}
#quote {
font-size: 1.1rem;
}
}
</style>
</head>
<body>
<div class="quote-wrapper">
<div class="quote-card">
<div class="quote-header">
<span class="quote-dot"></span>
<span>Random Quote</span>
</div>
<div class="quote-body">
<div class="quote-mark">“</div>
<p id="quote">Loading quote...</p>
<p id="author"></p>
</div>
<div class="quote-footer">
<div class="hint">Click the button to get a new quote</div>
<button id="new-quote">
New Quote
<span class="arrow">↻</span>
</button>
</div>
</div>
</div>
<script>
const API_URL = "https://api.quotable.io/random";
const quoteText = document.getElementById("quote");
const authorText = document.getElementById("author");
const newQuoteBtn = document.getElementById("new-quote");
const quoteCard = document.querySelector(".quote-card");
const accentColors = [
"#f97373",
"#4ade80",
"#38bdf8",
"#a855f7",
"#facc15",
"#fb7185",
"#22c55e",
"#f97316"
];
function setRandomAccentColor() {
const color = accentColors[Math.floor(Math.random() * accentColors.length)];
document.documentElement.style.setProperty("--accent-color", color);
}
function animateQuote() {
quoteCard.classList.remove("animate-quote");
// Force reflow to restart the animation
void quoteCard.offsetWidth;
quoteCard.classList.add("animate-quote");
}
async function fetchRandomQuote() {
newQuoteBtn.disabled = true;
const originalText = newQuoteBtn.textContent;
newQuoteBtn.textContent = "Loading...";
try {
const response = await fetch(API_URL);
if (!response.ok) {
throw new Error(`HTTP error ${response.status}`);
}
const data = await response.json();
quoteText.textContent = data.content || "No quote found.";
authorText.textContent = data.author ? `— ${data.author}` : "";
setRandomAccentColor();
animateQuote();
} catch (err) {
console.error(err);
quoteText.textContent = "Could not fetch a quote. Please try again.";
authorText.textContent = "";
} finally {
newQuoteBtn.disabled = false;
newQuoteBtn.textContent = originalText;
}
}
newQuoteBtn.addEventListener("click", fetchRandomQuote);
// Load an initial quote on page load
fetchRandomQuote();
</script>
</body>
</html>
You can customize:
- The API URL (
API_URL) to use a different quote service.
- The accent colors in
accentColors.
- The animation in the
@keyframes fadeInUp rule.