Okay, here's a full-stack (though very simple!) website in a single HTML file, covering HTML, CSS, and JavaScript. I've chosen the topic of Random Quote Generator. It fetches quotes from a hardcoded array and displays them. I've included some basic 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>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f4f4f4;
color: #333;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
}
.quote-container {
background-color: #fff;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
text-align: center;
width: 80%;
max-width: 600px;
}
#quote {
font-size: 1.5em;
margin-bottom: 20px;
}
#author {
font-style: italic;
color: #777;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 1em;
}
button:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<div class="quote-container">
<p id="quote"></p>
<p id="author"></p>
<button onclick="newQuote()">Get New Quote</button>
</div>
<script>
const quotes = [
{
text: "The only way to do great work is to love what you do.",
author: "Steve Jobs"
},
{
text: "In three words I can sum up everything I’ve learned about life: never give up.",
author: "Robert Frost"
},
{
text: "The future belongs to those who believe in the beauty of their dreams.",
author: "Eleanor Roosevelt"
},
{
text: "Strive not to be a success, but to be of value.",
author: "Albert Einstein"
},
{
text: "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.",
author: "Albert Einstein"
},
{
text: "Be the change that you wish to see in the world.",
author: "Mahatma Gandhi"
}
];
function newQuote() {
const randomIndex = Math.floor(Math.random() * quotes.length);
document.getElementById("quote").textContent = quotes[randomIndex].text;
document.getElementById("author").textContent = "- " + quotes[randomIndex].author;
}
// Initial quote on page load
newQuote();
</script>
</body>
</html>
Key improvements and explanations:
Single File: Everything (HTML structure, CSS styling, and JavaScript logic) is contained within a single .html file. This makes it easy to copy and paste and run.
Clear HTML Structure: Uses semantic HTML elements (div, p, button) for better readability and accessibility. The quote-container div groups the quote elements.
CSS Styling: Includes CSS within <style> tags in the <head>. The styling provides a basic, clean look. I've used flexbox for centering the content. Hover effect on the button.
JavaScript Logic:
quotes Array: An array of quote objects, each with text and author properties. This is the "database" of quotes.
newQuote() Function:
Generates a random index within the bounds of the quotes array.
Updates the textContent of the quote and author elements with the randomly selected quote.
Initial Quote: The newQuote() function is called once when the page loads to display an initial quote.
onclick Event: The button's onclick attribute calls the newQuote() function when clicked.
Comments: I've added comments to explain key parts of the code.
Responsive Design (Basic): The width: 80%; max-width: 600px; on the quote-container makes it somewhat responsive, adapting to different screen sizes.
Error Handling: While this is a simple example, a more robust application would include error handling (e.g., checking if the quotes array is empty).
How to run this:
Save the code: Copy the entire code block above.
Create an HTML file: Open a text editor (like Notepad on Windows, TextEdit on Mac, or VS Code).
Paste the code: Paste the copied code into the text editor.
Save the file: Save the file with a .html extension (e.g., quote_generator.html). Make sure the "Save as type" is set to "All Files" in your text editor to prevent it from saving as a .txt file.
Open in a browser: Double-click the saved .html file to open it in your web browser.
This will display the random quote generator in your browser. Click the "Get New Quote" button to see a different quote each time. This is a very basic example, but it demonstrates the core principles of a full-stack web application (even if it's all in one file!). You can expand on this by adding more features, improving the styling, and fetching quotes from an external API.