Creating a stylish website with a snowy background, a PC monitor displaying neon text, and functionality to handle user input via the keyboard involves combining HTML, Tailwind CSS, and JavaScript. Below is a comprehensive guide and complete code to achieve this.
Overview
- Snowy Background: We'll create a snowfall effect using CSS animations.
- PC Monitor Design: Using Tailwind CSS, we'll design a PC monitor that serves as a container for the neon text.
- Neon Text Display: The monitor's screen will display text entered by the user. We'll handle keyboard inputs using JavaScript, ensuring that each press of the
Enter key moves the text to a new line.
Complete Code
Here's the complete code that accomplishes the desired functionality. You can copy and paste this into an index.html file and open it in your browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Neon Monitor with Snow</title>
<!-- Tailwind CSS CDN -->
<script src="https://cdn.tailwindcss.com"></script>
<style>
/* Snowflake styles */
.snowflake {
position: absolute;
top: -10px;
color: #fff;
user-select: none;
z-index: 1000;
font-size: 1em;
pointer-events: none;
animation: fall linear infinite;
}
@keyframes fall {
to {
transform: translateY(100vh);
}
}
/* Neon text styles */
.neon-text {
color: #39ff14;
text-shadow:
0 0 5px #39ff14,
0 0 10px #39ff14,
0 0 20px #39ff14,
0 0 40px #39ff14,
0 0 80px #39ff14;
font-family: 'Courier New', Courier, monospace;
white-space: pre-wrap;
}
</style>
</head>
<body class="bg-black min-h-screen overflow-hidden relative">
<!-- Snowflakes Container -->
<div id="snowflakes"></div>
<!-- PC Monitor Container -->
<div class="flex items-center justify-center min-h-screen">
<div class="bg-gray-800 rounded-lg shadow-2xl w-3/4 max-w-4xl aspect-video relative">
<!-- Monitor Frame -->
<div class="absolute inset-0 border-4 border-gray-700 rounded-lg overflow-hidden flex items-center justify-center">
<!-- Screen Area -->
<div id="screen" class="w-full h-full bg-black p-6 overflow-auto">
<div id="display" class="neon-text"></div>
</div>
</div>
<!-- Monitor Stand (Optional) -->
<div class="absolute bottom-0 left-1/2 transform -translate-x-1/2 w-1/4 h-2 bg-gray-700 rounded-t-full"></div>
</div>
</div>
<!-- Hidden Input to Capture Keyboard Events -->
<input type="text" id="textInput" class="absolute opacity-0" autofocus />
<script>
// Snowflake Generation
const snowflakes = document.getElementById('snowflakes');
const numberOfSnowflakes = 100;
for(let i = 0; i < numberOfSnowflakes; i++) {
const snowflake = document.createElement('div');
snowflake.classList.add('snowflake');
snowflake.textContent = '❄';
snowflake.style.left = Math.random() * 100 + 'vw';
snowflake.style.animationDuration = (5 + Math.random() * 10) + 's';
snowflake.style.fontSize = (10 + Math.random() * 20) + 'px';
snowflakes.appendChild(snowflake);
}
// Neon Text Display Logic
const display = document.getElementById('display');
const textInput = document.getElementById('textInput');
// Ensure the input is always focused
textInput.focus();
let currentLine = '';
// Listen for key presses
document.addEventListener('keydown', (e) => {
// Prevent default behavior for certain keys
if (e.key === 'Enter') {
e.preventDefault();
appendLine(currentLine);
currentLine = '';
} else if (e.key === 'Backspace') {
e.preventDefault();
currentLine = currentLine.slice(0, -1);
updateDisplay();
} else if (e.key.length === 1) { // Printable characters
e.preventDefault();
currentLine += e.key;
updateDisplay();
}
});
function appendLine(text) {
display.textContent += text + '\n';
// Scroll to bottom
const screen = document.getElementById('screen');
screen.scrollTop = screen.scrollHeight;
}
function updateDisplay() {
display.textContent = display.textContent.slice(0, display.textContent.lastIndexOf('\n') + 1) + currentLine;
}
// Optional: Click to focus input
document.body.addEventListener('click', () => {
textInput.focus();
});
</script>
</body>
</html>
Explanation
1. HTML Structure
-
Head Section:
- Tailwind CSS CDN: We include Tailwind CSS via CDN for utility-first styling.
- Custom Styles:
- Snowflake Styles: Defines the appearance and animation of the snowflakes.
- Neon Text Styles: Defines the neon effect for the displayed text.
-
Body Section:
- Snowflakes Container (
#snowflakes): This div will hold all the snowflake elements.
- PC Monitor Container:
- A flex container centers the monitor on the screen.
- The monitor is styled with Tailwind classes to resemble a PC monitor.
- Inside the monitor, the
#screen div serves as the display area.
- The
#display div inside #screen will show the neon text.
- Hidden Input (
#textInput): An invisible input field is used to capture keyboard events. It's positioned absolutely and set to be invisible but always focused to ensure keyboard inputs are captured.
2. Snowfall Effect
The snowfall effect is achieved by creating multiple snowflake elements (❄) and animating them to fall from the top to the bottom of the viewport.
- JavaScript Logic:
- We generate 100 snowflakes by creating
div elements with the class snowflake.
- Each snowflake is positioned at a random horizontal position (
left) and given a random animation duration and font size to create a natural effect.
- The CSS
@keyframes fall animation moves each snowflake from the top to the bottom of the viewport.
3. PC Monitor Design
The PC monitor is styled using Tailwind CSS:
- Container: The monitor has a gray background, rounded corners, and a shadow to give it depth.
- Screen Area (
#screen): This is the display area where the neon text will appear. It's styled with a black background and padding.
- Neon Text (
#display): The text inside the screen has a neon glow achieved through multiple text-shadow layers.
4. Handling User Input and Displaying Neon Text
The JavaScript code handles user inputs from the keyboard and displays the text inside the monitor's screen.
Customization
Feel free to customize the appearance and behavior to better suit your preferences:
-
Snowflakes:
- Adjust the
numberOfSnowflakes variable to increase or decrease the snowfall intensity.
- Change the snowflake character to other Unicode snow symbols if desired.
-
Neon Text:
- Modify the
neon-text class in the CSS to change colors or shadow effects.
- Change the font family to better suit your design.
-
Monitor Design:
- Adjust the size, border, and other styling properties using Tailwind classes to better mimic different monitor styles.
Final Notes
This setup provides a visually appealing interface where users can type text, see it displayed in a neon style inside a PC monitor design, and enjoy a snowy background. The combination of Tailwind CSS for styling and JavaScript for interactivity ensures both aesthetics and functionality are addressed.