Modify an element from a different HTML class

I am currently learning HTML, PHP, CSS, Javascript and MySQL as part of a project. I am facing an issue while attempting to change the property of a class using hover. Here is an example:

html {
  height: 100%;
}

body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background-color: blue;
  background-image: url("../images/background.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

.rain {
  position: absolute;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
}

.rain.back-row {
  display: none;
  z-index: 1;
  bottom: 60px;
  opacity: 0.5;
}

body.back-row-toggle .rain.back-row {
  display: block;
}

drop {
  position: absolute;
  bottom: 100%;
  width: 15px;
  height: 120px;
  pointer-events: none;
  animation: drop 0.5s linear infinite;
}

@keyframes drop {
  0% {
    transform: translateY(0vh);
  }
  75% {
    transform: translateY(90vh);
  }
  100% {
    transform: translateY(90vh);
  }
}

.stem {
  width: 1px;
  height: 60%;
  margin-left: 7px;
  background: linear-gradient(to bottom, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0.25));
  animation: stem 0.5s linear infinite;
}

@keyframes stem {
  0% {
    opacity: 1;
  }
  65% {
    opacity: 1;
  }
  75% {
    opacity: 0;
  }
  100% {
    opacity: 0;
  }
}

.splat {
  width: 15px;
  height: 10px;
  border-top: 2px dotted rgba(255, 255, 255, 0.5);
  border-radius: 50%;
  opacity: 1;
  transform: scale(0);
  animation: splat 0.5s linear infinite;
  display: none;
}

body.splat-toggle .splat {
  display: block;
}

@keyframes splat {
  0% {
    opacity: 1;
    transform: scale(0);
  }
  80% {
    opacity: 1;
    transform: scale(0);
  }
  90% {
    opacity: 0.5;
    transform: scale(1);
  }
  100% {
    opacity: 0;
    transform: scale(1.5);
  }
}

.loginp {
  position: relative;
  top: 45%;
  margin: 0 auto;
  width: 6.25%;
  margin: 0 auto;
}

.login,
.pwr {
  position: relative;
  z-index: 3;
  height: 25px;
  color: white;
  border: none;
  border-radius: 5px;
  background-color: rgba(255, 255, 255, 0.1);
  transition: 1s;
}


label.ll,
label.lp {
  top: 23px;
  left: 5px;
  position: relative;
  color: #9eb2c8;
  z-index: 2;
  transition: 1s;
}

input.login:focus~label.ll {
  color: red;
}
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Login Screen</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body onload="makeItRain()" class="back-row-toggle splat-toggle">
  <div class="rain front-row"></div>
  <div class="rain back-row"></div>

  <div class="loginp">
    <label class="ll">Login</label>
    <input class="login" type="text" name="login" placeholder="" />
    <label class="lp">Password</label>
    <input class="pwr" type="password" name="password" placeholder="" />
    <div>

<script> 

var makeItRain = function() {

  $('.rain').empty();

  var increment = 0;
  var drops = "";
  var backDrops = "";

  while (increment < 100) {
    var randoHundo = (Math.floor(Math.random() * (98 - 1 + 1) + 1));

    var randoFiver = (Math.floor(Math.random() * (5 - 2 + 1) + 2));

    increment += randoFiver;

    drops += '<div class="drop" style="left: ' + increment + '%; bottom: ' + (randoFiver + randoFiver - 1 + 100) + '%; animation-delay: 0.' + randoHundo + 's; animation-duration: 0.5' + randoHundo + 's;"><div class="stem" style="animation-delay: 0.' + randoHundo + 's; animation-duration: 0.5' + randoHundo + 's;"></div><div class="splat" style="animation-delay: 0.' + randoHundo + 's; animation-duration: 0.5' + randoHundo + 's;"></div></div>';
    backDrops += '<div class="drop" style="right: ' + increment + '%; bottom: ' + (randoFiver + randoFiver - 1 + 100) + '%; animation-delay: 0.' + randoHundo + 's; animation-duration: 0.5' + randoHundo + 's;"><div class="stem" style="animation-delay: 0.' + randoHundo + 's; animation-duration: 0.5' + randoHundo + 's;"></div><div class="splat" style="animation-delay: 0.' + randoHundo + 's; animation-duration: 0.5' + randoHundo + 's;"></div></div>';
  }

  $('.rain.front-row').append(drops);
  $('.rain.back-row').append(backDrops);
}

</script>
</body>
</html>

The CSS code snippet for the hover effect is:

input.login:focus ~ label.ll {
  color: red;
}

My objective is to have something happen with the <label class="ll"> when a user hovers over the <input class="login">, but I am encountering difficulties implementing this feature. Where could I be going wrong?

Answer №1

There are two issues to address:

  1. The usage of :focus is incorrect in this context. It is meant to trigger when an input element is clicked on and active, while you are actually looking for the behavior that occurs when the mouse hovers over an element (i.e. :hover).

  2. Your attempt to target the label.ll element using the general sibling combinator (~) is flawed because it can only match elements that come after the first one. In your HTML structure, .ll appears before .login.

Consider moving the hover state declaration to a parent element instead:

.login-wrapper:hover label {
  color: red;
}
<div class="login-wrapper">
  <label for="login">Login</label> <input id="login" />
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Deploying an Azure Blob Trigger in TypeScript does not initiate the trigger

After successfully testing my Azure function locally, I deployed it only to find that it fails to trigger when a file is uploaded to the video-temp container. { "bindings": [ { "name": "myBlob", "type&qu ...

The toggle in the dialog box refuses to submit the information

I am facing an issue with a dialog box that I am displaying using the jQuery .dialog() function. The dialog box contains a button that I want to use to post back to another page, but for some reason, it is not working as expected. I have tried setting the ...

Choose selections from a single item and utilize jQuery to alter each one

Currently, I am working on creating a quick buy feature for products that come with various variants within my product list. Each product has a specific set of available options which are selected from a dropdown menu using jQuery, passed to an input field ...

Trouble with Implementing Nested h3 Styles in SCSS

I'm having trouble adjusting the margins of an h3 tag using scss in Angular. The scss code seems to be working, but when I nest the h3 tag inside a div tag, the margin adjustments don't take effect. Could someone help me figure out what's g ...

The setInterval() function is triggering, yet the Ajax request to update the content of a div

Within the onDocumentReady(), $(function () {...}) function, I have included the line: window.setInterval("readPluginCache();", 3000); The functionality of the readPluginCache() method involves making an ajax call to retrieve and format replacement html f ...

correcting mistakes in the design of the website

After inserting Google Adsense on my website, the content of the site moved down. Is there a way to have the content appear alongside the Google Adsense rather than below it? You can view my site here: .wrapper { width: 990px; margin: 0 auto; ...

Tips on avoiding the default action during a keypress for specific events before eventually restoring the default action

I've been experimenting with a project that involves using the space bar to trigger an event. It's actually quite complex, but I simplified it for this example. The concept is that when the space bar is held down, it highlights a certain div, an ...

Trouble with Material-UI's useMediaQuery not identifying the accurate breakpoint right away

While utilizing the MUI useMediaQuery hook in my React app, I encountered a bug that resulted in errors being thrown due to the initial failure of the hook to recognize the correct breakpoint. Eventually, the page re-renders and displays the correct value. ...

Steps for setting up node-openalpr on a Windows 10 system

While attempting to install npm i node-openalpr, an error is occurring: When using request for node-pre-gyp https download, I encounter a node-pre-gyp warning and error message. The package.json for node-openalpr is not node-pre-gyp ready, as certain pr ...

How about this: "Is it possible to push a button, move the mouse, and then release

Is there a way to replicate the following user interaction in an element using WebdriverIO? Click with the left button and hold it down Move the mouse while keeping the button pressed Release the button I am looking to simulate a 'swipe' actio ...

Efficiently Loading AJAX URLs using jQuery in Firefox

setInterval(function(){ if(current_url == ''){ window.location.hash = '#!/home'; current_url = window.location.hash.href; } else if(current_url !== window.location){ change_page(window.location.hash.split('#!/&apo ...

Pure CSS navigation - radio buttons to display or hide different pages

I'm attempting to create a website navigation bar using radio buttons, where the pages are displayed based on the selection. So far, I've managed to achieve this using <a href='#div'></a> and a :target, by placing the .page ...

Set the default color of material-ui v1 Button to be white

The default color of RaisedButton in material-ui used to be white unless specified as primary or secondary, but it has since been updated to grey by default. I am interested in finding the most efficient method to change all those buttons back to white by ...

Overlay Image on Card, Overflowing into Card Body

I currently have a webpage featuring cards. Each card includes a thumbnail at the top with an overlay. The main content of the card needs to be positioned outside of the overlay, but it ends up overflowing onto it. Take a look at the demo: https://codepe ...

werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The server is unable to process the request sent by the browser or proxy. This error occurred in a Flask web application

Can someone guide me on troubleshooting this issue in Flask? I am still learning. Server running at (Press CTRL+C to exit) 127.0.0.1 - - [26/Jul/2020 11:19:45] "GET /predict HTTP/1.1" 500 - Traceback (most recent call last): raise exceptions. ...

What is the process for adding a box shadow beneath my header?

I am currently attempting to add a box shadow under my header, similar to the design featured in the project mockup at https://github.com/RaghavMangrola/the-brighton-times. After trying to implement the following CSS property and value: .header { ...

Can you elaborate on the contrast between React Server Components (RSC) and Server Side Rendering (SSR)?

I'm curious about the differences between RSC in React 18 and SSR in NextJS. Can anyone explain? ...

One way to dynamically track if any radio buttons in a group have been selected is by utilizing JQuery

Even though there are many related resources for this question, I still need a flawless solution. I have dynamically generated five groups of radio buttons. Each group contains up to five radio buttons. I have separately validated "none checked in" and "a ...

What is the best way to determine when the context value has been established?

Currently encountering an issue while trying to display a header. To achieve this, I begin by making an API call in InnerList.js and setting a list in context using the data from the API call. Next, in Context.js, I assign the list to a specific data set ...

Oops! The type in React.jsx is not valid - it should be a string for built-in components. You may want to consider converting your class component to a

The error message I am encountering is as follows: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it&ap ...