The CSS effects are failing to render properly within the React application

In my React application, I created a form with two fields: one for the username and one for the password. Each field has a label placed above it. The goal is to have the label shift up slightly, decrease in size, and change color when the user clicks on the text field. While I have successfully implemented this design using HTML and CSS mockups, the functionality does not work properly once integrated into the React application. The issue seems to be that the label's color changes automatically without requiring the user to click on the text field. https://i.sstatic.net/i9YoS.png

h1 {
  color: white;
  text-shadow: 1px 1px 2px black, 0 0 25px blue, 0 0 5px darkblue;
  font-size: 60px;
  text-align: center;
}


.box {
   position: absolute;
   top: 50%;
   left: 50%;
   transform: translate(-50%, -50%);
   width: 500px;
   padding: 40px;
   background: rgba(0,0,0,.7);
   box-sizing: border-box;
   box-shadow: 0 15px 25px rgba(0,0,0, .5);
   border-radius: 10px;
}

.box h2
  {
   margin: 0 0 0px;
   padding: 0;
   color: white;
   text-align: center;
  }

 .box .inputBox{
    position: relative;
  }

 .box .inputBox input{
    width: 100%;
    padding: 10px 0;
    font-size: 16px;
    color: white;
    margin-bottom: 30px;
    border: none;
    border-bottom: 1px solid white;
    outline: none;
    background: transparent;
 }

  .box .inputBox label{
     position: absolute;
     top: 0;
     left: 0;
     padding: 10px 0;
     font-size: 16px;
     color: white;
     pointer-events: none;
     transition: .5s;
    }
.box .inputBox input:focus ~ label, //this is the code for the special effects
.box .inputBox input:valid ~ label
  {
    top: -18px;
    left: 0;
    color: #5100c9;
    font-size: 12px;
  }

.box input[type="submit"]{
    background: transparent;
    border: none;
    outline: none;
    color: white;
    background: #5100c9;
    padding: 10px 20px;
    cursor: pointer;
    border-radius: 5px;
  }

   .box input[type="submit"]:hover{
       font-weight: bold;
     }

Answer №1

After creating a codepen experiment, I discovered that a particular line in the css was causing an issue:

.box .inputBox input:focus ~ label,
.box .inputBox input:valid ~ label

The problem lies with the second selector in that line (

.box .inputBox input:valid ~ label
), which always evaluates to true because

The :valid selector only applies to form elements that meet specific criteria, such as input elements with min and max attributes, email fields with a valid email format, or number fields with numeric values, etc.

This leads to the label constantly moving up and changing color, regardless of whether it has focus or not.

To resolve this issue, I simply removed

,
.box .inputBox input:valid ~ label
from your css, as demonstrated in the codepen.

Your next task will be to prevent the label from returning to its original position when the input field loses focus. While removing that line may have seemed like the solution you were aiming for, it is not the correct answer. You can find further guidance on achieving this objective by visiting this link .

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

Spotting repeated terms within an HTML document

I have a table with results generated using v-html (meaning the text inside the table does not appear until the page is rendered). I want to compare two rows and highlight any duplicate words they may contain. While looking for examples, I came across a p ...

I'm having a strange issue where my code runs perfectly on JSFiddle, but it refuses to work on localhost

I am facing an issue with running my code on my LAMP server localhost, even though it works fine on JSFiddle. Typically, when this happens, it's usually a small mistake that I overlook. However, having multiple sets of eyes look at the problem is alwa ...

Issue with the Release build failure involving the Linker and ___gxx_personality_v0"

In my pursuit of running my application offline on my phone using react-native v0.40, I attempted creating a Release build. Unfortunately, this resulted in errors causing the app to fail starting on my phone (although it runs successfully on the simulator) ...

Utilizing CSS within JavaScript

Can someone assist me with applying CSS to my Javascript code? I'm not very skilled in this area, so I'd appreciate insight from those who are knowledgeable. My JS is used for the menu hover effect, and here is the code: $(function(){ $(&ap ...

A new object type is being introduced that allows for a dynamic number of keys and values. It also supports values that can be either a

I'm working on a project using NextJS and TypeScript. I have a function named submitFunc that needs to accept three arguments - type (string), base endpoint (string), and an object with dynamic keys and values, where the values can be either strings o ...

What could be preventing the jQuery from showing the JSON response?

I am having an issue with a jQuery script that is supposed to pull a quote from an API in JSON format and display it on the page. However, for some reason, I am unable to retrieve data from the API. Can you help me figure out what is wrong here? Thank yo ...

The function 'appendChild' is not recognized on the type 'unknown'.ts(2339)

I'm encountering an issue while trying to integrate the Utterances component into my articles. Upon attempting to build the site, I receive the following error message: "Property 'appendChild' does not exist on type 'unknown' ...

What is the best way to compare a LatLng object with another one that is stored in an array?

Just starting out with JavaScript and decided to work on building apps using Google Maps. However, I've come across a puzzling issue. Take a look at the code snippet below. <html> <head> <script src = "http://maps.googleapis.com/maps/ ...

Modifying the color of the calendar icon within the date picker of MUI

Is there a way to customize the color of the calendar icon that automatically appears inside the DatePicker component from the MUI library? I attempted to change the color using props like shown below <DatePicker label="Controlled picker" ...

What is the best approach to include a new element in InputProps endAdornment while still maintaining the openPicker functionality?

I'm looking to add a new item in InputProps endAdornment while still maintaining the functionality of the iconEvent that controls the popup for setting the date (openPicker). <DatePicker label="Date of baptism" openTo="year" ...

How can text be concealed within an input field?

My degrees converter code is functioning well, but I have an issue. When I input a number for one temperature type, the conversion for the other two types is displayed correctly. However, if I then enter a number for another temperature type, the previous ...

Ways to turn off hover highlighting

Is there a way to disable the highlighting effect of the <select> element in HTML? When you hover over items in the dropdown list, a blue color strip moves with your mouse. I need to find a way to get rid of this effect. Here is an example of the c ...

The CSS code does not seem to be rendering properly within the plugin when attempting to generate a PDF file

I attempted to use a rendering plugin in order to create a PDF file. However, when I tried to add a CSS file for my template, it did not work as expected. I am a bit confused about how to properly import a CSS file into my GSP page. Here is the template c ...

Tips for creating a clickable phone number on a WordPress classified website

Despite trying numerous solutions to make the phone number clickable, it still didn't work as expected <?php global $redux_demo;?> <?php $phoneon= $redux_demo['phoneon']; ?> <?php if($phoneon == 1){?> <?php $po ...

What is the reason behind adding an extra block in overlapping P tags?

I've encountered a puzzling situation in my code. I have two P tags with borders applied to them, leading me to believe that there should be two light blue blocks displayed. However, upon running the code, I am seeing three blocks instead of the expec ...

Determining the position of the selected option in an Angular 2 Select menu

Is there a way to retrieve the position of the selected option in a similar manner to the index of a table? Example for a table: <tr *ngFor="let car of cars; let i = index" (click)="showTabs(i)"> <td>{{i+1}}</td> </tr> I am lo ...

Having trouble retrieving a particular element using xpath in selenium with Python

Currently, I am working on extracting wind direction information using selenium and I have found that using xpath is the most straightforward approach to retrieve this data. There is a table containing all the necessary details, and the structure of the el ...

Retrieve container for storing documents in JavaServer Pages

Previously, I created JSP and HTML code to upload a file from the hard disk into a database using <input type="file" name="upfile"> However, a dialog box with an "Open" button is displayed. What I am looking for is a "Save" button that will allow u ...

Mastering the art of throwing and managing custom errors from the server to the client side within Next.js

I'm in the process of developing a Next.js application and I am faced with the challenge of transmitting customized error messages from the server to the client side while utilizing Next JS new server-side actions. Although my server-side code is func ...

The React state remains stagnant and does not receive any updates

Although there have been numerous questions on this topic before, each one seems to be unique and I haven't found a close match to my specific issue. In my scenario, I have a grid containing draggable ItemComponents. When an item is selected, additio ...