Validation for both input[type="email"]:valid and input[type="email"]:invalid is malfunctioning

When a user selects the email field in my form, the label that describes the email field moves up and shrinks in size:

https://i.sstatic.net/x2whi.png

Here is the code I am using:

input:focus + label,
input:valid + label /*Is supposed to leave the label over the text even if the input isn't valid*/ 
input:invalid + label{
    color: aqua;
    font-size: 100%;
    transform: translateY(-20px);
} 

This code works as expected for input fields with type=text. However, with email input fields, the label moves back down after the user deselects the input:

https://i.sstatic.net/4jXk5.png

Can anyone suggest how I can resolve this issue? It's puzzling that it occurs with type "email" but not when it's "text"

<input type="text" for="Sname" name="Sname" id="Sname" maxlength="100" required>
<label for="Sname">Lorem</label><br>

<input type="text" id="Iname" name="Iname" maxlength="100" required>
<label for="Iname">ipsum</label>
                   
<label for="CB"> <input type="checkbox" id="CB" name="CB"> <span>Lorem dolor sit amet</span> </label>
                         
<input type="email" id="EmailInput" name="EmailInput" maxlength="150" disabled required>
<label for="EmailInput" id="EmailLabel">Mail</label>
                        

Answer №1

The reason behind the solution is somewhat unclear, but it appears to work as intended.

input[type="text"]:focus + label, 
input[type="text"]:valid + label,
input[type="email"]:focus + label, 
input[type="email"]:valid + label, 
input[type="email"]:invalid + label {
    color: aqua;
    font-size: 100%;
    transform: translateY(-20px);
} 

It seems that specifying input[type="text"] prevents confusion, especially when dealing with invalid inputs in a text field.

That's why I specifically targeted this selection.

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

I'm having trouble getting my text to center properly within a li container

I'm struggling to perfectly align the text in the middle of the li container. It seems to be centered horizontally, but vertically it's off. I've attempted adding a span to the text and adjusting the margin and padding, but so far I've ...

Placing a div element beneath some text

Here is my HTML code snippet: <div id="backgroundH"></div> <div id="header"> <h2> Premium Store </h2> </div> This is my CSS code snippet: #backgroundH { width: 100%; height: 50px; background-color: #d ...

Maintaining a one-line CSS menu layout, the middle element is designed with two inner elements that seamlessly stack without disturbing the appearance of the menu items

I'm facing a challenge with aligning the two elements in the "inline" class - I need them stacked so that the element containing "SECURITIES" is under "A.M.S". The issue arises when I try to add a line break after the A.M.S element, which also affects ...

There seems to be an issue with the functionality of Angular Material on iOS devices

I recently developed a website using Angular and Angular Material. While the site functions properly on Windows and Android across all browsers, I encountered an issue with iOS devices running Safari. Some elements on the page do not display correctly on i ...

Flexbox ancestor causing Bootstrap 5 Carousel width to double

A peculiar issue arises with the Bootstrap 5 Carousel placed inside a Flexbox where it unexpectedly expands in width (pushing other flex-items aside) when transitioning between images. This behavior persists even when the immediate container is set to disp ...

Ignoring Bootstrap Popover Placement Setting

Within my Bootstrap modal, there is a select element with options that each have detailed descriptions in Bootstrap popovers. Despite trying different values for the placement option in the popover initialization, the popover consistently appears in the mi ...

Exciting transition effect for font sizes in CSS

Issue: Hover over the X and wait for the div to collapse completely. Move away from the X Notice how the div jumps to the bottom and then back up. Inquiry: How can I prevent text wrapping while expanding the div during animation? (Currently using del ...

How can you animate a MUI v4 Grid element using CSS transitions?

I was exploring the potential of using breakpoints in the MUI v4 component to control the visibility of items in my Grid System. How can I create a smooth CSS transition for b, transitioning from 0px to a defined breakpoint size of 3 for xl? Using % works ...

How can I insert a item into an Array using JavaScript code?

My instructor set up an array in my JavaScript file that is off limits for me to modify. My task is to add new objects to it through code without directly manipulating the existing array. Here's a snapshot of what my array contains: const words = [{ ...

Slide content towards the left to uncover the option to delete

Is there a way to achieve this task? Here is what I'm currently working on: https://jsfiddle.net/Lg0wyt9u/967/ <li> <div class='type'><i class='fa fa-arrow-circle-right'>I</i></div> & ...

Ensure that each column within a flexbox list is set to a flex-direction of column and flex-wrap of wrap, with a width no wider than

This code snippet is almost functioning correctly, but there seems to be an issue with it taking up the entire width available: https://jsfiddle.net/d16ba2re/2/ CSS: ul { display: flex; flex-direction: column; flex-wrap: wrap; height: 100px; p ...

CSS: generating space between lines within a single paragraph

I need help styling a multiline paragraph with varying width. I want each line to have a solid background color, except for in between two lines where I want to display a background image. For example, visit I've attempted adjusting the line-height, ...

Preventing parent properties from overriding descendants is a key part of maintaining a hierarchical

I found a css file online that I'm using for a website I'm building, but I am only incorporating certain components from it. The issue is that the global styles in this css file are overriding all the global styles on my website. My solution was ...

Incorporating CSS into React.js applications

I am currently working on a project using MERN.io. In my project, I am utilizing the React.js component Dropdown. However, the Dropdown component does not come with its own style and CSS, resulting in no styling at all. ... import Dropdown from 'rea ...

Utilizing image backgrounds for hyperlinks in the Android browser

Encountering a minor issue with some <a> tags that have images as background, and the text inside the tags is indented using CSS. However, on the Android browser, part of the string used in HTML appears to cover the background image within the area o ...

What is the best method for implementing multiple select options in ReactJS?

Hey there! I'm new to React and I'm attempting to create a multiple select option using the Axios GET method. However, I've encountered an issue with adding multiple select options in this file. I've been trying to achieve this with che ...

`CSS Selectors in Internet Explorer 8`

I'm having trouble setting the style of the first div within a fieldset in IE 8. I've tried various methods like :first, first-child, etc but nothing seems to be working. Does anyone have suggestions on how to make it work in IE 8? Thank you! He ...

When a text is wrapped by an HTML div using absolute positioning, is there a way to prevent it from wrapping without relying on white space

I have a group of div elements positioned absolutely on the screen. If any div contains content that is too big for the screen, the browser wraps it into multiple lines to fit. However, I do not want this behavior. Instead, I want the overflowing content ...

Add the CSS code to the <head> section, even though the CSS styles are located within

According to the analysis from Webpagetest.org, it appears that The CSS for http://mypage.com/howitworks is located in the document body: The link node for http://mypage.com/design_header.css should be relocated to the document header. The design_header ...

What is the reason that custom styling for a single react component instance does not function properly?

In my current view, I have integrated a react component as shown below: <Jumbotron> Lots of vital information </Jumbotron> I am looking to give this particular instance a unique style, like using a different background image. However, addin ...