Open In App

Design a Random Quote Generator in Tailwind CSS

Last Updated : 04 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

This project demonstrates the creation of a simple Random Quote Generator web application using HTML, Tailwind CSS for styling, and JavaScript for functionality. The application generates random quotes and their authors upon clicking the “Generate Quote” button, offering an interactive and visually appealing experience.

azzzzzzz

Prerequisites / Technologies Used:

Approach / Functionalities:

  • Display a random quote and its author when the “Generate Quote” button is clicked.
  • Utilize an array of quotes and authors.
  • Apply Tailwind CSS for styling.

Steps to Create & Configure the Project:

  • Create a new HTML file and include the necessary Tailwind CSS CDN link in <head> section.
  • Define the structure of the quote container and button in HTML body.
  • Create a JavaScript array quotes containing objects with the quote texts and authors.
  • Write a function generateQuote to randomly select a quote from array and display it.
  • Add an event listener to button to call the generateQuote function on click.

Example: Build a Random Quote Generator web app using HTML, CSS with Tailwind, and JavaScript. Clicking the button displays randomly selected quotes and authors, combining functionality with modern styling.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Random Quote Generator</title>
    <link href=
          rel="stylesheet">
</head>
 
<body class="bg-gray-100 flex justify-center
             items-center h-screen">
    <div class="quote-container p-8 rounded-lg
                shadow-lg border-2 border-green-500
                text-center">
        <p class="quote-text text-lg text-gray-700 mb-4"
           id="quoteText">
              Click the button below to
              generate a random quote!
          </p>
        <p class="author-text text-sm
                  text-gray-600" \
           id="authorText">
          </p>
        <button
            class="generate-button bg-green-500
                   text-white px-4 py-2 rounded-md
                   mt-4 hover:bg-green-600 focus:outline-none"
            onclick="generateQuote()">
              Generate Quote
          </button>
    </div>
    <script>
        const quotes = [
            {
                text: "The greatest glory in living lies not in never falling, but in rising every time we fall.",
                author: "Nelson Mandela"
            },
            {
                text: "The way to get started is to quit talking and begin doing.",
                author: "Walt Disney"
            },
            {
                text: "Your time is limited, don't waste it living someone else's life.",
                author: "Steve Jobs"
            },
            {
                text: "If life were predictable it would cease to be life, and be without flavor.",
                author: "Eleanor Roosevelt"
            },
            {
                text: "Life is what happens when you're busy making other plans.",
                author: "John Lennon"
            },
            {
                text: "The future belongs to those who believe in the beauty of their dreams.",
                author: "Eleanor Roosevelt"
            },
            {
                text: "Spread love everywhere you go. Let no one ever come to you without leaving happier.",
                author: "Mother Teresa"
            },
            {
                text: "In the end, it's not the years in your life that count. It's the life in your years.",
                author: "Abraham Lincoln"
            },
            {
                text: "Life is either a daring adventure or nothing at all.",
                author: "Helen Keller"
            },
            {
                text: "Many of life's failures are people who did not realize how close they were to success when they gave up.",
                author: "Thomas A. Edison"
            }
        ];
        function generateQuote() {
            const randomIndex = Math.floor(Math.random() * quotes.length);
            const quote = quotes[randomIndex];
            document.getElementById("quoteText").textContent = `"${quote.text}"`;
            document.getElementById("authorText").textContent = `- ${quote.author}`;
        }
    </script>
</body>
 
</html>


Output :

am1



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads