A step-by-step guide on replicating the Google login form animation

Help needed here! I'm attempting to recreate the user login page for Google Chrome, and I've hit a roadblock (seriously, I don't even know where to start) with the input field of that form.

I have tried inspecting Chrome's page, but it's quite challenging to understand how to replicate the effect it has.

Let me illustrate with a bit of code.

Here is my form with input fields (as shown below). When I click on one of the fields, I want the placeholder to appear as it does in Chrome.

I really can't figure out how to achieve that kind of effect... Before clicking: not selected After clicking: result

<input type="text" name="user-name" placeholder="Username"></input>
<input type="password" name="password" placeholder="Password"></input>
input{
border:solid 1px #222;
}
input:focus{
border:solid 2px #ddb760;
}

Answer №1

To achieve this effect, you can use CSS properties like `transform` and the pseudo-class `:placeholder-shown`. It's important to note that you need to include a placeholder attribute with a space value in your input elements, like so: placeholder=" ".

.custom-input {
  position: relative;
  margin-bottom: 10px;
  box-sizing: border-box;
  display: inline-block;
}
.custom-input label {
  background: white;
  padding: 3px;
  font-size: 12px;
  transition: transform 150ms cubic-bezier(0.4, 0, 0.2, 1), opacity 150ms cubic-bezier(0.4, 0, 0.2, 1);
  transform-origin: bottom left;
  color: #ddd;
  font-family: arial;
  position: absolute;
  top: 7px;
  left: 7px;
  z-index: 1;
  cursor: text;
}

.custom-input input, .custom-input textarea {
  border: 1px solid #ddd;
  display: block;
  padding: 10px;
  border-radius: 3px;
  width: 100%;
  box-sizing: border-box;
}

.custom-input.fill {
  display: block;
  width: 100%;
}

.custom-input input:focus, .custom-input textarea:focus {
  outline: 0;
  border-color: #1873e8;
}

.custom-input input:focus+label, .custom-input input:not(:placeholder-shown)+label,
.custom-input textarea:focus+label, .custom-input textarea:not(:placeholder-shown)+label{
  transform: translateX(-3px) translateY(-15px);
  font-size: 10px;
  color: #1a73e8;
}
<div class="custom-input">
  <input type="text" id="user-name" name="user-name" placeholder=" ">
  <label for="user-name">Username</label>
</div>
<div class="custom-input">
  <input type="password" id="password" name="password" placeholder=" ">
  <label for="password">Password</label>
</div>

<div class="custom-input fill">
  <input type="text" id="user-name2" name="user-name2" placeholder=" ">
  <label for="user-name2">Username</label>
</div>
<div class="custom-input fill">
  <input type="password" id="password2" name="password2" placeholder=" ">
  <label for="password2">Password</label>
</div>

<div class="custom-input fill">
  <textarea id="ta" name="ta" placeholder=" "></textarea>
  <label for="ta">Textarea</label>
</div>

Answer №2

When it comes to making the design look like a placeholder, Google has successfully achieved this by using a div with text that moves on hover and appears on top of the input field.

Here is a simple way to implement this:

var text = document.getElementById("text");
var label = document.getElementById("label-text");

text.addEventListener("focusout", onfocusout);

function onfocusout(e) {
if (text.value.length > 0) text.classList.add('not-empty');
else text.classList.remove('not-empty');
}

// allow focus of the input even on click of div
label.onclick = function(e) {
text.focus();
}
.input-group {
  position: relative;
}

#text {
  padding: 10px;
  
  border: 1px solid black;  
  outline: none;
}

#label-text {
  padding: 0 5px;

  position: absolute;
  top: 10px;
  left: 5px;
  
  background-color: #fff;
  color: #999;
  
  transition: .2s;
}

#text:focus {
  border-color: blue;
}

#text:focus + #label-text {  
  color: blue;
}

#text:focus + #label-text, #text.not-empty + #label-text {
  top: -10px;
  left: 5px;
}
<br><br>

<div class="input-group">
  <input id="text" type="text" onfocusout="" />
  <div id="label-text">Username</div>
</div>

Answer №3

Maybe this solution could be helpful:

HTML:

<input type="text" name="user-name" placeholder="Username" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Username'">
<input type="password" name="password" placeholder="Password" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Password'">

CSS:

input {
  border:solid 1px #ddb760;
}

input:focus {
  border:solid 1px blue;
}

Fiddle: https://jsfiddle.net/x9khyv71/

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

Offcanvas navigation in Bootstrap 5

I found this code snippet in the official Bootstrap 5 demos, but I'm struggling to figure out how to transition the off-canvas menu from left-to-right. The documentation and demo codes are quite different. As I work on a landing page in Bootstrap 5, ...

Google Maps API displaying empty spots instead of markers

I am facing an issue with my webpage where the Markers are not showing up, despite troubleshooting for many hours. The parsing php file has been confirmed to be working. Below is the code: <script src="https://maps.googleapis.com/maps/api/js">< ...

Is it possible to activate the jQuery .click() function for a button with specific text?

Here's a dilemma I'm facing: $('.add_to_cart span:contains("Choose a Size")').click(function() { console.log("it has been clicked") }); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></s ...

Looking for a way to extract Regular Expressions from an IgGrid cell in Infragistics?

Is it possible to apply a regular expression to a igTextEditor within an igGrid Updating? I attempted to utilize the validate option, but it was unsuccessful. $("#schedulerTable").igGrid({ columns: $scope.schedulerColumns, widt ...

boosting the maximum number of requests allowed

What can be done to increase the request limit if the user continues to hit rate limits? This is my current rate limiter setup: const Limiter = rateLimit({ windowMs: 10000, max: 5, standardHeaders: true, legacyHeaders: false, keyGenerator: funct ...

What is preventing Protractor from detecting Angular on a site that has been automatically initialized with Angular?

Whenever I try to utilize browser.get() in my code, I encounter the following error: Error: Angular could not be found on the page http://localhost:5000/#/login debug=timing&saveLogs=true&displayAll=true : angular never provided resumeBootstrap A ...

Retrieving file icons from a directory using Node.js fs

I am interested in creating a file system explorer application that displays files with icons. I am using node-webkit and the files can be executables, directories, or regular files. In the file list, I want to show the file's icon and name. Is it po ...

Display or conceal image based on date

I'm interested in showing a unique zodiac sign image on my website according to the date of the year. For instance, from March 21st to April 21st, I want to display the "Aries" image; from April 22nd to May 21st, I want to show the "Taurus" image, and ...

I am looking to implement a required alert for a radio button that mimics HTML5. How can

When using the input type text, adding the required attribute prevents the form from submitting and prompts the browser to focus on the required field with an alert instructing to fill it out. On the other hand, when applying the required attribute to t ...

What is the process for reverting to an older version of Webpack?

In my ASP.NET core Web project, I've successfully installed Webpack using NPM, and the current version is 2.4.1. However, I actually need to have 2.1.0-beta.25 installed. I attempted to install the desired version using the following command: npm ins ...

After my jQuery functions are executed, my CSS rules are loaded

Struggling with the communication between CSS and jQuery, I find myself torn. In CSS, my rules are often very specific, like this: div#container > div#contentLeft { //Code here } However, when I add jQuery to spice up my site, the CSS rules seem to ...

Are you in need of an IDE for Javascript, CSS, HTML, or Java

Alright, time to make a decision. I'm torn between Aptana, Eclipse, and Netbeans. Ideally, I want a versatile program that can handle all the different languages I plan on experimenting with - HTML, CSS, Javascript, and Java. I already have Visual Stu ...

Learn the process of uploading a file using FormData alongside multer in node js. Experience the issue of receiving undefined in req.file and { } in req.body

Is there a way to successfully upload a file using FormData along with multer in Node.js? I seem to be encountering issues as req.file shows undefined and req.body displays {}. Check out the HTML code snippet below: <input type="file" name=&q ...

Why isn't the CSS class being applied to the checkbox element?

I created a basic demo where the HTML markup was the same, but one extra rule was applied in one case and not in the other. Here is the Markup: <div class="rc40w1 oform oform-initialized"> <div class="rc40w5 hide"> < ...

Form an item using an array

Is there a way to efficiently convert this array into a map? Here is how the array looks: var array = [{ "id" : 123 }, { "id" : 456 }, { "id" : 789 }]; The desired output should be: var result = { "123": { id: 123 } , "456": { id: 456 } , ...

Iterate through a intricate array of JavaScript objects to extract their values

Looking for ways to extract total calorie and nutrition information from a large document displaying the nutritional data of a simulated recipe. Please review the codesandbox json file first. The objective is to capture the total calories and nutritive c ...

Choosing an id of my preference to set as the value in the ng-options select list and ensuring proper binding with ng-model

When using Angular, I am trying to create a select list where the value corresponds to the actual id of an object. I want to bind this correctly with the ng-model directive. Here is my attempt: <select ng-model="selectedPersonId" ng-o ...

I keep receiving a warning from React-Router stating that it is not possible to modify the <Router routes> component

In my application, I am utilizing React-Router along with a customized history object. The setup looks something like this: import { createHistory } from 'history'; import { Router, Route, IndexRedirect, useRouterHistory } from 'react-rout ...

Check the vuelidate required function in Vye.js for ensuring that all necessary fields

I am currently utilizing the vuelidate plugin for form validation. import { required, maxLength } from 'vuelidate/lib/validators'; In my Vue component, I have a method called isFinishedFill(): methods: { isFinishedFill() { return !!this. ...

ERESOLVE encountered difficulty resolving the error during the execution of npm install

I've been struggling with resolving dependency conflicts while installing and updating npm packages. The error message in the console is provided below. I have attempted to install legacy dependencies and re-install some modules, but nothing seems to ...