I am 14 years old i understand html and css but not understanding java. Write a code for…
- User
I am 14 years old i understand html and css but not understanding java. Write a code for a site with dark mode
- Assistant
Sure! I’d be happy to help you create a simple website with a dark mode toggle using HTML, CSS, and a little bit of JavaScript. Even if you're not familiar with JavaScript yet, I'll explain everything step-by-step so you can understand how it works.
What is Dark Mode?
Dark mode is a feature that lets users switch the color scheme of a website from light (with dark text) to dark (with light text). It's easier on the eyes, especially in low-light environments.
How We'll Do It
- HTML: Structure the webpage and add a toggle switch (a button) to switch between light and dark modes.
- CSS: Define styles for both light and dark modes.
- JavaScript: Add functionality to the toggle switch so that when you click it, the website switches between light and dark modes.
Complete Code Example
Here's a complete example. You can create a file named
index.htmland paste the following code into it. Then, open it in your web browser to see it in action.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Dark Mode Example</title> <!-- Link to the CSS styles --> <style> /* CSS Variables for Light and Dark Themes */ :root { --background-color: #ffffff; --text-color: #000000; --link-color: #1a0dab; --button-background: #e0e0e0; --button-text: #000000; } /* Dark Theme Variables */ body.dark-mode { --background-color: #121212; --text-color: #ffffff; --link-color: #8ab4f8; --button-background: #424242; --button-text: #ffffff; } /* Apply the variables */ body { background-color: var(--background-color); color: var(--text-color); font-family: Arial, sans-serif; transition: background-color 0.3s, color 0.3s; padding: 20px; } a { color: var(--link-color); } /* Style for the toggle button */ .toggle-button { background-color: var(--button-background); color: var(--button-text); border: none; padding: 10px 20px; cursor: pointer; border-radius: 5px; font-size: 16px; transition: background-color 0.3s, color 0.3s; } .toggle-button:hover { opacity: 0.8; } </style> </head> <body> <h1>Welcome to My Website</h1> <p>This is a simple webpage with a dark mode toggle.</p> <a href="#">This is a link</a> <br><br> <!-- Toggle Button --> <button class="toggle-button" onclick="toggleDarkMode()">Toggle Dark Mode</button> <!-- JavaScript to handle the toggle --> <script> function toggleDarkMode() { // Get the body element const body = document.body; // Toggle the 'dark-mode' class on the body body.classList.toggle('dark-mode'); // Optional: Save the user's preference in localStorage if(body.classList.contains('dark-mode')){ localStorage.setItem('theme', 'dark'); } else { localStorage.setItem('theme', 'light'); } } // Optional: On page load, check if the user has a saved preference window.onload = function() { const savedTheme = localStorage.getItem('theme'); if(savedTheme === 'dark'){ document.body.classList.add('dark-mode'); } } </script> </body> </html>How It Works
-
HTML Structure:
- We have a basic HTML structure with a heading, a paragraph, a link, and a button.
- The button has an
onclickattribute that calls thetoggleDarkMode()function when clicked.
-
CSS Styling:
- We use CSS Variables (custom properties) to define colors for different parts of the webpage.
- The
:rootselector defines the default (light) theme. - When the
<body>has the classdark-mode, it overrides the variables with dark colors. - We also add some transitions to make the color change smooth.
-
JavaScript Functionality:
- The
toggleDarkMode()function toggles thedark-modeclass on the<body>. - When the class is added, the CSS variables switch to dark mode colors.
- Optional: We use
localStorageto remember the user's preference even if they reload the page or come back later.
- The
Trying It Out
- Save the Code: Create a new file called
index.htmland paste the above code into it. - Open in Browser: Double-click the
index.htmlfile to open it in your web browser. - Click the Button: Click the "Toggle Dark Mode" button to switch between light and dark themes.
Understanding the Code
- CSS Variables: They make it easy to switch themes by just changing the values of the variables.
.toggle-button: This is the style for the button that toggles the theme.- JavaScript:
document.body.classList.toggle('dark-mode'): This line adds thedark-modeclass if it's not present or removes it if it is.localStorage: This is used to save the user's choice so that the website remembers their preference.
Learn More
Since you're learning web development, here are a few topics you might want to explore next:
- JavaScript Basics: Understanding variables, functions, and events.
- CSS Flexbox and Grid: For advanced layout designs.
- Responsive Design: Making your website look good on all devices.
- Dark Mode Libraries: There are libraries like Darkmode.js that can add more features to your dark mode.
Feel free to ask if you have any questions or need further assistance. Happy coding!