Ever wanted to create your own fun, unique stories with just a few words? This Interactive Story Generator lets users fill in the blanks to generate random, engaging stories! 📖✨
With just HTML, CSS, and JavaScript, we’ll build a simple yet fun web app where users:
✔️ Enter a name, place, object, and action.
✔️ Click “Generate Story” to create a randomized adventure.
✔️ Get a different story each time they try!
By the end of this tutorial, you’ll have a fully functional storytelling app that sparks creativity and fun! 🎭
🚀 Features of Our Story Generator
✅ Dynamic Storytelling – Users input details, and the app generates a unique story.
✅ Randomized Storylines – The script chooses a random story template, ensuring variety.
✅ User-Friendly UI – Simple form & vibrant design for an engaging experience.
✅ Responsive Design – Works on mobile, tablets, and desktops.
Video Tutorial:
🏗️ 1. HTML: Structuring the Story Generator
We begin by creating a form for user input and a button to generate stories dynamically.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Interactive Story Generator 📖</title> <link rel="stylesheet" href="style.css" /> </head> <body> <div class="container"> <h1>Interactive Story Generator</h1> <div class="wrapper"> <div> <label for="name">Enter a Name:</label> <input type="text" id="name" placeholder="Type a name..." required /> </div> <div> <label for="place">Enter a Place:</label> <input type="text" id="place" placeholder="Type a place..." required /> </div> <div> <label for="object">Enter an Object:</label> <input type="text" id="object" placeholder="Type an object..." required /> </div> <div> <label for="action">Enter an Action:</label> <input type="text" id="action" placeholder="Type an action..." required /> </div> </div> <button id="generateStory">Generate Story</button> <div id="story" class="story"></div> </div> <script src="script.js"></script> </body> </html>
✨ Key Elements:
✔️ #name, #place, #object, #action
– User input fields.
✔️ #generateStory
button – Triggers story generation.
✔️ #story
– Displays the generated story dynamically.
🎨 2. CSS: Styling the Story Generator
To make it attractive and engaging, we use a vibrant red background, modern fonts, and a card-like container.
body { font-family: "Poppins", sans-serif; box-sizing: border-box; background-color: #fa3546; margin: 20px; } .container { position: absolute; width: min(400px, 90vw); transform: translate(-50%, -50%); left: 50%; top: 50%; background: white; padding: 30px; border-radius: 10px; box-shadow: 0 30px 30px rgba(0, 0, 0, 0.2); } .wrapper { display: grid; grid-template-columns: 1fr 1fr; gap: 15px; } input { width: 90%; padding: 10px; margin-bottom: 15px; border: 1.5px solid #000000; border-radius: 5px; } label { font-weight: bold; display: block; margin-bottom: 10px; } button { width: 95%; background-color: #fa3546; color: white; cursor: pointer; border: none; padding: 15px 0; border-radius: 10px; } button:hover { background-color: #45a049; } .story { margin-top: 20px; padding: 15px; background: #fff3cd; border-radius: 10px; display: none; }
🎨 UI Features:
✔️ Vibrant Background – A bold red theme for excitement.
✔️ Rounded Corners & Shadows – Gives a modern, polished look.
✔️ Hover Effects – The button changes color on hover.
✔️ Hidden Story Box – The story only appears after clicking the button.
⚡ 3. JavaScript: Generating Random Stories
Now, let’s add the magic to our app! The JavaScript:
✔️ Captures user input from the form.
✔️ Randomly picks a story template from an array.
✔️ Inserts user input into the story.
✔️ Displays the generated story.
document.getElementById("generateStory").addEventListener("click", function () { let name = document.getElementById("name").value.trim(); let place = document.getElementById("place").value.trim(); let object = document.getElementById("object").value.trim(); let action = document.getElementById("action").value.trim(); let storyDiv = document.getElementById("story"); if (name === "" || place === "" || object === "" || action === "") { alert("Please fill in all fields!"); return; } let stories = [ `One day, ${name} went to ${place} and found a mysterious ${object}. Without thinking, ${name} decided to ${action}, and something magical happened!`, `In the heart of ${place}, ${name} discovered a hidden ${object}. As curiosity took over, ${name} started to ${action}, and the adventure began!`, `${name} was walking through ${place} when suddenly a ${object} appeared. Without hesitation, ${name} chose to ${action}, leading to an unforgettable experience!`, `While exploring ${place}, ${name} saw a sparkling ${object}. As soon as they touched it, they were transported to a new world where they had to ${action} to find a way back home!`, `One night at ${place}, ${name} found an ancient ${object}. The moment they tried to ${action}, the object began to glow, revealing a hidden secret!`, `At ${place}, ${name} was given a strange ${object} by an old wise person. They were told that if they ${action} at the right moment, something incredible would happen!`, `${name} was cleaning their attic when they stumbled upon an old ${object}. Curious, they decided to ${action}, and suddenly, they were taken on a journey through time!`, ]; let randomStory = stories[Math.floor(Math.random() * stories.length)]; storyDiv.innerText = randomStory; storyDiv.style.display = "block"; });
🔍 How It Works:
✔️ Stores multiple story templates in an array.
✔️ Randomly selects a story using Math.random()
.
✔️ Replaces placeholders with user input.
✔️ Displays the generated story inside #story
.
🎯 Final Thoughts
This Interactive Story Generator is a fun and creative project that teaches:
✔️ Working with Forms – Capturing user input.
✔️ Randomization – Selecting random story templates.
✔️ DOM Manipulation – Dynamically inserting user data.
✔️ CSS Styling – Creating a modern, polished UI.