Ways to verify the input text for content without activating the submit action using only JavaScript

I'm working on creating a unique input effect where the label moves up 100px when the input is focused, and then remains at the top when the user enters a value. If the input is empty and the user clicks outside, the label will return to its original position. I need help achieving this floating label effect. Thank you!

<html>
<head>

<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
</head>

<style>

.form{width:600px; margin:auto; }

.input-conatiner{ width:400px; margin:60px auto ; text-align:center; height:35px ;width:320px;  position:relative; }
.input-conatiner input{position:absolute; background:none; top:0; left:0; height:100%; width:100%;  }
.input-conatiner label{position:absolute; top:0; left:0; z-index:9; transition:300ms ;}
.input-conatiner:hover input + label {color:red; transform:translatey(-150%);}

</style>



<body>

<form>

<div class="input-conatiner">
<input type="text"/>
<label>Hello there</label>

</div>

<div class="input-conatiner">

<input type="text"/>
<label>Hello there</label>

</div>


<div class="input-conatiner">
<input type="text"/>
<label>Hello there</label>

</div>

</form>

</body>

</html>

Answer №1

When a user enters a value and then clicks outside the input field, the label will stay at the top. However, if the input is empty and the user clicks outside, the label will return to its original position.

So essentially, you only want the label to move back down if the field remains empty? If it's filled, it should stay above?

The closest way to achieve this with just CSS is to make the input field required and utilize the :valid pseudo-class:

<html>
<head>

<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
</head>

<style>

.form{width:600px; margin:auto; }

.input-conatiner{ width:400px; margin:60px auto ; text-align:center; height:35px ;width:320px;  position:relative; }
.input-conatiner input{position:absolute; background:none; top:0; left:0; height:100%; width:100%;  }
.input-conatiner label{position:absolute; top:0; left:0; z-index:9; transition:300ms ;}
.input-conatiner:hover input + label,
.input-conatiner input:valid + label {color:red; transform:translatey(-150%); }
</style>
<body>
<form>
<div class="input-conatiner">
<input type="text" required/>
<label>HI there</label>
</div>
</form>
</body>
</html>

If more functionality is needed, JavaScript may be required.

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

"Implement a smooth transition effect lasting 1000 milliseconds when a class is added via

I'm attempting to use the fadeIn 1000 effect when a class is added through a Click function. I have already attempted this: http://jsfiddle.net/rf7mudL5/ HTML <span class="date-outer">Text will Red on fadeIn</span> <button class="mo ...

Placing the right column above the left column for better visibility

I am facing a challenge with the layout of my two columns. When viewing on desktop: On mobile: The current design has the column on the left, "Browse Programs", appearing below the left column. I am looking for a solution to have it appear above the left ...

What is the best way to incorporate multiple CSS files into a single HTML file in Django3?

My template folder contains an HTML file called index.html, along with two CSS files named computer.css and mobile.css stored in the static folder. I want to know how I can incorporate both CSS files into the single index.html file. ...

developing a multimedia player using HTML5

I'm attempting to create a dynamic video "collage" that showcases videos with different aspect ratios in a flexible grid layout. Each row should have videos of the same height, while filling up the horizontal space within a container. With known widt ...

Tips for updating one-time binding data in AngularJS

I am currently facing an issue with a div that displays details such as mobile number, name etc. in the format: {{::mobilenumber}}, {{::name}} Within this div, there is a button that when clicked should populate the same values in a new form However, des ...

Unable to access content from a dictionary in Django templates

Currently, I am attempting to retrieve the value of the class method that returns a dictionary. The function is structured as follows: class GetData: def __init__(self, api_key, ip, interface): self.api_key = api_key self.asa_ip = ip ...

What is the best way to establish personalized CSS styles specific to each subdomain?

I am in the process of establishing a multi-tenant application with a single instance and single database setup. The backend infrastructure is built using Ruby on Rails, while the frontend is handled by a separate AngularJS app integrated with a Rails fram ...

Navigate to the initial visible element on the specified webpage via the page hash link

In my handlebars HTML template, I have a partial view that includes different pieces of content for desktop and mobile. I use different CSS classes to hide and show these elements accordingly. <div class='hideOnMobile showOnDesktop'> < ...

How can I address the variations in CSS appearance between Firefox and Google Chrome?

I'm currently working on a table with two cells in each row just for fun. For the first row, I want both cells to display the same picture. To achieve this, I wrote the following CSS: tr:nth-child(1){ background-image:url("cat.jpg"); background-size: ...

Passing the final element of a POST array to a different page

I am having trouble retrieving the correct array values from the form. In my code, I am invoking the form option value 3 times, which is defined in the database. Here is an example of what is currently in the database: https://i.sstatic.net/G4JQx.png Wh ...

Is it possible to use CSS transitions to animate an absolute positioned element?

I am having trouble getting a CSS transition to work for animating an element's position change. I have tried using the all properties in the transition declaration like in this example: http://jsfiddle.net/yFy5n/3/ However, my goal is to only apply ...

Ensure that every HTML link consistently triggers the Complete Action With prompt on Android devices

After extensive searching, I have yet to find a solution to my issue. I have been developing a web application that allows users to play video files, primarily in the mp4 format. Depending on the mobile browser being used, when clicking the link, the vide ...

Is there a way to position my image and text side by side?

My current objective revolves around implementing a specific design, as illustrated in this image. The issue I'm encountering in my code pertains to the utilization of the bootstrap grid system for ease of layout. However, when I incorporate both tex ...

Executing 'npm start' to launch an HTML file along with a CSS stylesheet - A step-by-step guide

As a newcomer to the world of coding, I've found that asking questions on platforms like Stack Overflow enables me to connect with fellow developers. I usually code using VS Code and rely on the "live server" extension to quickly test my code. Howeve ...

Empty spaces in an "option" html gadget on a website created with Structr 4.1.2 Community edition

Currently using Structr version 4.1.2 on a managed Sandbox (Community edition) of Structr. I am currently investigating the functionality of Structr and have encountered an issue related to how an "html" select element functions on a page built in Structr ...

Border utilities in Bootstrap 4 provide easy ways to add borders

I have been searching for a solution, but I can't seem to find it. Bootstrap 4 offers utility classes for adding and removing borders, but is there a way to define the border width or style like solid or dashed? <span class="border"></span&g ...

Maintain the background div while scrolling horizontally

I am facing an issue with my wide bar chart where I use iScroll to enable horizontal scrolling. Behind the bar chart, there are horizontal lines that should move along in the background as the user scrolls horizontally. The challenge is to keep the lines v ...

Adding a padding-left to a horizontal list within Flexbox enhances the design and structure

How can the left-padding added by Flexbox on a horizontal list be fixed even with justify-content: space-between? <div class="list-container"> <ul> <li>One</li> <li>Two</li> <li>Three</li> ...

Adding a Video as a Background Button Using HTML and CSS

My attempt to utilize background-image in CSS for the button background was unsuccessful as it only supports images, not videos. Is there a way to set a video as the background of a button? My goal is to create a button with a continuously looped muted v ...

Output the result of a MySQLI query using PHP's echo function

I am attempting to output my PHP while loop within an html echo. Essentially, I want to echo within another echo. Is this achievable? Below is the code snippet: <?php session_start(); require 'config.php'; if (@$_SESSION['username&ap ...