Theme change search bar

Viewed 432

I'm a beginner in html and I want to learn. Three days ago I started my html journey and I'm running into a problem...

So, I created a 'dark-mode' theme on my page and it's working.

My problem is: when I click the Theme button, I want to change the search bar color and text and I don't know how to do it.

Basically when I click the Theme button to change theme to dark mode I want the search bar to change background color to #121212 and text color to white.

And when I re-click the Theme button and come back to light mode, to undo the things, but I can't figure out how.

Thank you in advance.

    <!DOCTYPE html>
    <html>
    <head>
    <title>Test</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
    
    <style>
    body {
      background-color: white;
      color: #369497;
      transition: 0.5s;
    }
    
    .dark-mode {
      background-color: #121212;
      color: white;
      transition: 0.5s;
    }
    
    .button {
      display: inline-block;
      border-radius: 4px;
      background-color: #369497;
      border: none;
      color: white;
      text-align: center;
      font-size: 18px;
      padding: 12px;
      width: 120px;
      transition: all 0.5s;
      cursor: pointer;
      margin: 10px;
    }
    
    .button-pos {
      margin: 0;
      position: absolute;
      bottom: 0%;
      right: 0%;
      transform: translate(-20%, -15%);
    }
    
    .button span {
      cursor: pointer;
      display: inline-block;
      position: relative;
      transition: 0.5s;
    }
    
    .button span:after {
      content: '\00bb';
      position: absolute;
      opacity: 0;
      top: 0;
      right: -20px;
      transition: 0.5s;
    }
    
    .button:hover span {
      padding-right: 25px;
    }
    
    .button:hover span:after {
      opacity: 1;
      right: 0;
    }
    
    input[type=text] {
      width: 480px;
      box-sizing: border-box;
      border: 2px solid #369497;
      border-radius: 4px;
      font-size: 16px;
      background-color: white;
      padding: 12px 20px 12px 10px;
    }
    
    .searchbar-pos {
      margin: 0;
      position: absolute;
      top: 25%;
      left: 25%;
    }
    
    </style>
    </head>
    
    <body>
    <div class="bgimg w3-display-container w3-animate-opacity">
    <div class="w3-display-topmiddle">
    <img src="logo" alt="logo">
    </div>
    </div>
    
    <div class="bgimg w3-display-container w3-animate-opacity">
      <div class="w3-display-topleft w3-padding-large w3-xlarge">
        Logo
      </div>
    </div>
    
    <script>
    function myFunction() {
       var element = document.body;
       element.classList.toggle("dark-mode");
    }
    </script>
    
    <div class="w3-animate-opacity">
    <div class="button-pos">
    <button onclick="myFunction()" class="button" style="vertical-align:middle"><span>Theme </span></button>
    </div>
    </div>
    
    <div class="searchbar-pos">
    <form>
      <input type="text" name="search" placeholder="Search...">
    </form>
    </div>
    
    </body>
    </html>

1 Answers

You have to write a css rule for that, basically something that overrides the input styling when there's a parent element with dark-mode class applied (body in this case).

All you need is ti add this to your css:

.dark-mode input[type=text] {
  background-color: #121212;
  color: white;
}

Here's your code with that change:

<!DOCTYPE html>
<html>

<head>
  <title>Test</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">

  <style>
    body {
      background-color: white;
      color: #369497;
      transition: 0.5s;
    }
    
    .dark-mode {
      background-color: #121212;
      color: white;
      transition: 0.5s;
    }
    
    .button {
      display: inline-block;
      border-radius: 4px;
      background-color: #369497;
      border: none;
      color: white;
      text-align: center;
      font-size: 18px;
      padding: 12px;
      width: 120px;
      transition: all 0.5s;
      cursor: pointer;
      margin: 10px;
    }
    
    .button-pos {
      margin: 0;
      position: absolute;
      bottom: 0%;
      right: 0%;
      transform: translate(-20%, -15%);
    }
    
    .button span {
      cursor: pointer;
      display: inline-block;
      position: relative;
      transition: 0.5s;
    }
    
    .button span:after {
      content: '\00bb';
      position: absolute;
      opacity: 0;
      top: 0;
      right: -20px;
      transition: 0.5s;
    }
    
    .button:hover span {
      padding-right: 25px;
    }
    
    .button:hover span:after {
      opacity: 1;
      right: 0;
    }
    
    input[type=text] {
      width: 480px;
      box-sizing: border-box;
      border: 2px solid #369497;
      border-radius: 4px;
      font-size: 16px;
      background-color: white;
      padding: 12px 20px 12px 10px;
    }
    
    .dark-mode input[type=text] {
      background-color: #121212;
      color: white;
    }
    
    .searchbar-pos {
      margin: 0;
      position: absolute;
      top: 25%;
      left: 25%;
    }
  </style>
</head>

<body>
  <div class="bgimg w3-display-container w3-animate-opacity">
    <div class="w3-display-topmiddle">
      <img src="logo" alt="logo">
    </div>
  </div>

  <div class="bgimg w3-display-container w3-animate-opacity">
    <div class="w3-display-topleft w3-padding-large w3-xlarge">
      Logo
    </div>
  </div>

  <script>
    function myFunction() {
      var element = document.body;
      element.classList.toggle("dark-mode");
    }
  </script>

  <div class="w3-animate-opacity">
    <div class="button-pos">
      <button onclick="myFunction()" class="button" style="vertical-align:middle"><span>Theme </span></button>
    </div>
  </div>

  <div class="searchbar-pos">
    <form>
      <input type="text" name="search" placeholder="Search...">
    </form>
  </div>

</body>

</html>

Related