HomeCSSDynamic color changer

Dynamic color changer

Introduction:

Welcome to another exciting tutorial! In this guide, we’ll explore how to create a dynamic color changer using HTML, CSS, and JavaScript. This interactive web element allows users to input a color value, which dynamically updates the background color of a designated box. It’s a simple yet effective way to engage users and add a touch of interactivity to your web projects. Let’s dive in and see how it’s done!

Things You Will Learn:

  1. HTML Structure: We’ll start by setting up the HTML structure for our color changer. This includes a container div, an input box for users to enter color values, and a result box where the color changes will be displayed.
  2. CSS Styling: Learn how to style the HTML elements using CSS to create an appealing visual layout. We’ll design the container, input box, and result box to ensure a cohesive look and feel.
  3. JavaScript Logic: Dive into the JavaScript code to understand how the color-changing functionality works. We’ll capture user input, dynamically update the background color of the result box, and ensure that the color changes are reflected in real time.

Video Tutorial:

If you prefer to learn by watching a video tutorial over reading this lengthy blog post you can watch the video tutorial here down below. Also do not forget to subscribe to my YouTube channel where I post tips, tricks and tutorials related to web development regularly.

Project Folder Structure:

Now before we move on to actual coding we create a project folder structure. We name the project folder as – ”Dynamic Color Changer”. Within this folder we have 3 files. These files are:

  • index.html
  • style.css
  • script.js

HTML:

We begin with the HTML code. Copy the code below and paste it into your HTML document.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Dynamic Color Changer</title>
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <div id="result-box"></div>
      <input type="text" id="input-box" value="orange" />
    </div>
    <!-- Script -->
    <script src="script.js"></script>
  </body>
</html>

 

CSS:

Next, we style our game using CSS. For this copy, the code provided to you below and paste it into your stylesheet.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
body {
  background-color: #121317;
}
.container {
  background-color: #202229;
  width: 18.75em;
  position: absolute;
  transform: translate(-50%, -50%);
  left: 50%;
  top: 50%;
  padding: 3em;
  border-radius: 1em;
}
#result-box {
  height: 200px;
  width: 200px;

  box-shadow: 0 0 0 10px #202229, 0 0 0 15px #f5f5f5;
  border-radius: 1em;
  margin-bottom: 2em;
}
#input-box {
  width: 100%;
  padding: 1em;
  border: none;
  outline: none;
  border-radius: 0.5em;
  font-family: "Poppins", sans-serif;
}

 

JS:

Finally, we add functionality using Javascript. For this once again copy the code below and paste it into your script file.

let inputBox = document.getElementById("input-box");
let resultBox = document.getElementById("result-box");
let changeColor = () => {
  let input = inputBox.value;
  resultBox.style.backgroundColor = input;
};
inputBox.addEventListener("input", changeColor);
window.addEventListener("load", changeColor);

 

Conclusion:

You’ve just created a dynamic color changer using HTML, CSS, and JavaScript! This tutorial covered the essential steps to build an interactive web element that allows users to change the background color dynamically. Feel free to customize the color changer further or integrate it into your web projects to enhance user experience and interactivity.

RELATED ARTICLES

Bear CSS Art

Elephant CSS Art

LEAVE A REPLY

Please enter your comment!
Please enter your name here

9 + six =

Most Popular