How can I design a form that resembles the sign-in form used by Google?

Currently, I am in the process of creating a contact form for a website that is inspired by the design of Google's material sign-in form.

I have successfully implemented an effect where clicking on the input field causes the label to change its position, size, and color. However, this effect only works with input fields that have the "required" attribute.

Unfortunately, I have not been able to achieve the same effect with default input fields.

The CSS code for this form can be found below:

.civildaily-form {
  display: block;
}

.civildaily-form .form-group {
  position: relative;
  margin-bottom: 2rem;
}
/* More CSS code */

Here is an example of how the form markup looks like:

<form class="civildaily-form">
  <div class="form-group">
    <input type="text" class="form-control" required>
    <label for="Name">Name</label>
  </div>
  
  /* Additional form inputs */

</form>

Answer №1

It appears that by adding left: 0; to your css for .civildaily-form label and removing conflicting css selectors for the :valid pseudo class on your input elements, you can resolve the issue.

CSS alone cannot reliably determine whether an input is empty across all major browsers (the :valid pseudo-class only applies to required fields, or a workaround using the :placeholder-shown pseudo-class with limited browser support). Using JavaScript is a more dependable method to detect empty inputs and apply appropriate label positioning styles (such as adding a .filled class in the CSS to work alongside JavaScript).

Below is a working code snippet:

const fields = document.querySelectorAll('.form-control');
for (const field of fields) {
  field.addEventListener('blur', (event) => {
    const sel = event.target;
    if (sel.value) {
      sel.classList.add('filled');
    } else {
      sel.classList.remove('filled');
    }  
  });
}
.civildaily-form {
  display: block;

/* Remaining CSS code provided here */
}
<form class="civildaily-form">
  <div class="form-group">
    <input class="form-control" id="name" type="text" placeholder="" required>
    <label for="name">Name</label>
  </div>
  <div class="form-group">
    <input class="form-control" id="email" type="email" required>
    <label for="email">Email</label>
  </div>
  <div class="form-group">
    <input class="form-control" id="phone" type="text">
    <label for="phone">Phone</label>
  </div>
  <div class="form-group">
    <input class="form-control" id="company" type="text">
    <label for="company">Company Name</label>
  </div>
</form>

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

Looking to remove an item from an array using Redux?

Is there an issue with my reducers? Here is how they are set up: export default (itemsList = [], action) => { if (action.type === 'ADD_ITEM') { return [...itemsList, action.payload] } return itemList } The deleting reduce ...

Tips for solving the problem of TSX error where 'ReactNode' cannot be assigned with 'void' type

I've been attempting to loop through the Mui component MenuItem using a forEach loop, but I'm encountering an error stating 'Type 'void' is not assignable to type 'ReactNode''. Here's the section of my code caus ...

A guide on verifying if two arrays of integers are permutations using JavaScript

Is there a way to determine if two sets of integers in JavaScript are permutations? For example, given the arrays: a = [1, 2, 3, 4, 5] and b = [2, 3, 5, 1, 4] I want a function that will return true if they are permutations of each other. ...

Trouble with implementing useEffect to assign value to TextField

I'm currently working on a project where I am using the useEffect hook to retrieve initial data from a database and set it as the initial value of a Material-UI TextField upon loading the page. Although this approach works most of the time, there are ...

StartsWith() function failing when used in conjunction with takeWhile()

I'm trying to iterate over an Immutable List and create a new list containing only the entries that start with a specific string. In this case, I want to find all states that begin with the letter 'D'. However, instead of returning a list wi ...

Save the language code (ISO 639) as a numerical value

Currently, I'm working with a MongoDB database and have made the decision to store certain information as Numbers instead of Strings for what I thought would be efficiency reasons. For instance, I am storing countries based on the ISO 3166-1 numeric s ...

Troubleshooting custom filtering with jQuery Datatables across different tables is presenting challenges

I am currently utilizing the Datatables 1.10 jQuery plug-in and I am interested in implementing the custom search feature to filter two tables simultaneously on a single page, as shown below: function applyFilterByErrorClass(propertiesTable, errorClassNam ...

Overlapping divs are observed when utilizing absolute positioning in CSS

I am facing a challenge with placing the div identified by our_team below the div with the ID of services. When I use absolute positioning, the former ends up overlapping the latter. The services div transitions to display information when hovered over du ...

Implement a callback function in React using hooks after changing the state

Back in the days of React before hooks, setting state and calling a function after it had been set was achieved using the following syntax: this.setState({}, () => {//Callback}) Now, with hooks, what is the equivalent way to achieve this? I attempted ...

What is the best way to display input data (with names and values) in a textarea field

I'm currently working on a project that requires the value of a textarea to be updated whenever one of the input values in the same form is changed. Here is the HTML code: <form id="form" action="" method=""> <textarea readonly class="overv ...

Steps to exit browser in WebDriver Sampler in JMeter and halt execution

I have been attempting to close the browser in my Selenium Jmeter last sampler thread, but I keep encountering the following error: INFO c.g.j.p.w.s.WebDriverSampler: WebDriver has been quit. 2024-02-01 22:53:24,989 ERROR c.g.j.p.w.s.WebDriverSampler: Sess ...

The maximum value of the slider corresponds to the total number of elements in the array

As I work on constructing a Material UI Slider, I have a specific requirement. I want the maximum value of my slider to dynamically adjust according to the number of items in an array of options. ['Answer1', 'Answer2', 'Answer3&ap ...

Angular Commandments: Understanding the Directives

Within my code, there is a specific directive view that I am utilizing: <div class="busy-indicator angular-animate" ng-show="busy"></div> <div class="breadcrumblist" ng-class="atTopLevel ? ['at-top-level'] : null"> <div ...

Aligning the right menu of Ant Design from the Layout component

How can I align the menu to the right in antd? Please Note: The image has been edited using Photoshop and is not real. I attempted to declare a className = "Menu" and set .Menu {align-items: right;} but it didn't work. Here is the code snippet I use ...

Tips for managing blur events to execute personalized logic within Formik

I am currently delving into the world of React/Next.js, Formik, and Yup. My goal is to make an API call to the database upon blurring out of an input field. This call will fetch some data, perform database-level validation, and populate the next input fiel ...

Identify and sort JSON objects based on keys with multiple values

My JSON file contains objects structured like this: [ { "name" : "something", "brand": "x", "category" : "cars" }, { "name" : "something2 ...

Avoid making GET requests when clicking on a link

[UPDATE] I need help troubleshooting an issue with my ajax request. Here is the code snippet that I am working on: <a href="" class="undo_feedback">Undo</a> When I click on the link, it triggers an ajax POST request, but I encounter an error ...

The URL in $.mobile.changePage has not been updated

One of the challenges I encountered was switching between two pages - a menu list page and a menu detail page. The transition from the menu to the menu detail page was achieved using $.mobile.changePage. While everything seemed to be working correctly, the ...

Is there a way to connect a CSS external file that is saved on Dropbox?

Is it possible to link an external CSS file stored in Dropbox with HTML? I have followed all the instructions I could find online, including clicking and pressing "Copy Dropbox Link" to generate an href link. However, it doesn't seem to be working. ...

Angular JS Troubleshooting: Application Not Functioning Properly

I've been learning AngularJS on codeSchool and I ran into an issue with my simple hello world app. It was working fine at first but then stopped completely. I can't seem to find the bug, so any help would be appreciated. Here is the HTML code: & ...