The art of CSS animation in web design - Exploring the magic

Here is a code snippet I am looking to implement:

/* Stylish Form */

form input {
  width: 80%;
  font-size: 2rem;
}

.form-label {
  position: absolute;
  left: 1rem;
  font-size: 1.5rem;
  color: #5a5a5a;
  background-color: white;
  transition: 0.5s;
}

form input:focus ~ .form-label {
  position: absolute;
  font-size: 0.7rem;
  top: -0.6rem;
  padding: 0.2em;
  left: 0.5rem;
  color: #0051ff;
  background-color: white;
  transition: 0.2s;
  z-index: 2;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <section>
    <form action="">
      <div class="input-container">
        <input type="text" name="" id="my-input">
        <label for="my-input" class="form-label">hello world</label>
      </div>
    </form>
  </section>
</body>

</html>

I want to add this animation feature to the contact form I have previously mentioned. Any suggestions on how to integrate these two sets of code to achieve my desired outcome?

Answer №1

I found inspiration from webdevtrick.com and morioh.com and decided to customize the code using vanilla JavaScript instead

/** Modified Code By Webdevtrick ( https://webdevtrick.com )  **/

const inputs = document.querySelectorAll("input");

inputs.forEach((input) => {
  input.addEventListener("focusin", (e) => {
    e.target.previousElementSibling.classList.add("active");
  });

  input.addEventListener("focusout", (e) => {
    e.target.previousElementSibling.classList.remove("active");
  });
});
/** Modified Code By Webdevtrick ( https://webdevtrick.com ) **/

* {
  letter-spacing: 3px;
  font-family: "Alegreya Sans SC", sans-serif;
}

body {
  color: white;
  background-color: #f05855;
}

h1 {
  font-size: 60px;
  margin: 0px;
  padding: 0px;
  text-align: center;
}

div.main {
  width: 700px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  vertical-align: middle;
}

div.main div {
  position: relative;
  margin: 40px 0;
}

label {
  position: absolute;
  top: 0;
  font-size: 30px;
  margin: 10px;
  padding: 0 10px;
  background-color: #f05855;
  -webkit-transition: top 0.2s ease-in-out, font-size 0.2s ease-in-out;
  transition: top 0.2s ease-in-out, font-size 0.2s ease-in-out;
}

.active {
  top: -25px;
  font-size: 20px;
  font-weight: 900;
  letter-spacing: 6px;
  text-transform: uppercase;
}

input[type="text"] {
  width: 100%;
  padding: 20px;
  border: 2px solid white;
  font-size: 20px;
  font-weight: 800;
  background-color: #f05855;
  color: white;
}

input[type="text"]:focus {
  outline: none;
}
<!DOCTYPE html>
<!--Modified Code By Webdevtrick ( https://webdevtrick.com )-->
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <title>Customized CSS Input Label Animation | Webdevtrick.com</title>

  <link href="https://fonts.googleapis.com/css?family=Alegreya+Sans+SC&display=swap" rel="stylesheet" />
  <link rel="stylesheet" href="styles.css" />
</head>

<body>
  <div class="main">
    <h1>CSS Label Animation</h1>
    <div>
      <label for="FirName">First Name</label>
      <input id="FirName" type="text" class="styl" />
    </div>

    <div>
      <label for="LasName">Last Name</label>
      <input id="LasName" type="text" class="styl" />
    </div>

    <div>
      <label for="Messag">Your Message</label>
      <input id="Messag" type="text" class="styl" />
    </div>
  </div>
  <script src="script.js"></script>
</body>

</html>

Answer №2

Give this a try, I've made some adjustments to your template and added an animation effect.

* {
  --input-height: 3rem;
}
.contactform {
  position: relative;
  border-radius: 50px;
  background-color: #f2f2f2;
  padding: 5px;
  z-index: 2;
  display: block;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: auto;
  margin-top: 1%;
  width: 100%;
  animation-name: gradient;
  animation-duration: 3s;
  animation-iteration-count: infinite;
}
.contactform:hover {
  animation-name: gradient;
  animation-duration: 15s;
  animation-iteration-count: infinite;
}
.row:after {
  content: "";
  display: table;
  clear: both;
}
.column {
  float: center;
  width: 50%;
  margin-top: 6px;
  padding: 20px;
  display: block;
  margin-left: auto;
  margin-right: auto;
}
section {
  height: 100vh;
  width: 100vw;
  display: grid;
  place-content: center;
}

.input-container input {
  height: var(--input-height);
  width: 80vw;
  font-size: 2rem;
}

.input-container {
  position: relative;
  display: grid;
  place-items: center start;
  margin-top: 6px;
  margin-bottom: 16px;
  width:50%;
}

.input-container label {
  position: absolute;
  left: 1rem;
  font-size: 1.5rem;
  color: rgb(90, 90, 90);
  background-color: white;
  transition-duration: 0.5s;

}

.input-container input:focus~label {
  position: absolute;
  font-size: 0.7rem;
  top: -0.6rem;
  padding: 0.2em;
  left: 0.5rem;
  color: rgb(0, 81, 255);
  background-color: white;
  transition-duration: 0.2s;
  z-index: 2;
  
}
input[type=submit] {
  background-color: #0563bb;
  color: white;
  padding: 12px 20px;
  border: none;
  cursor: pointer;
}

input[type=submit]:hover {
  opacity: 0.9;
}
@media screen and (max-width: 600px) {
  .column,
  input[type=submit] {
    width: auto;
    margin-top: 0;
  }
}

@keyframes shake {
  10%,
  90% {
    transform: translate3d(-1px, 0, 0);
  }
  20%,
  80% {
    transform: translate3d(2px, 0, 0);
  }
  30%,
  50%,
  70% {
    transform: translate3d(-4px, 0, 0);
  }
  40%,
  60% {
    transform: translate3d(4px, 0, 0);
  }
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
</br></br>
  <section id="contact">
  <div class="container" data-aos="fade-up">
    <div class="contactform">
      <div style="text-align:center">
        <div class="section-title">
          <h2><br/>Get In Touch</h2>
        </div>
        <p>Feel Free To Reach Out To Me Through This Form! </p>
      </div>
  <div class="row">
        <div class="column">
    <form name="myform" action="thankyou.html" method="POST" novalidate>
      <div class="input-container">
     <input type="text" id="first name" name="firstname" required>
        <label for="firstname">First Name</label>
      </div>
      <div class="input-container">
     <input type="text" id="lastname" name="lastname" required>
         <label for="lastname">Last Name</label>
      </div>
      
      <input type="submit" value="Submit">
    </form>
    </div>
    </div>
    </div>
    
  </section>
</body>

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

Utilize the same variable within a React functional component across multiple components

Within a React functional component, I am utilizing the following constant: const superHeroPowers = [ { name: 'Superman', status: superManPowerList }, { name: 'Batman', status: batmanPowerList }, { name: 'Wonder Woman&a ...

Using GULP and NODE, what is the best way to retrieve the Jenkins BUILD_NUMBER in JavaScript?

I am having an issue with my gulp task named getBuildNumber. This task utilizes the Child Process module to run a script. gulp.task('getBuildNumber', function() { var buildNumber = child_process.execSync("echo $BUILD_NUMBER").toString(). ...

Angular data binding is not reflecting updates

Currently delving into the depths of Angular 2+, I find myself perplexed by the intricacies of bindings. Despite my best efforts on Google, the missing piece still eludes me after half an hour of searching. On a page, there lies a humble button whose text ...

React Typescript: exploring the power of dynamic types

Can dynamic typing be implemented? The JSON structure I am working with looks like this: { "fieldName": "Some text", "type": String, "inputType": "text" }, { "fieldName": "Some bool&q ...

Counting Numbers with jQuery Animation from Zero to Percentage Value

I am currently working on a project where I aim to incorporate a jQuery Animated Number Counter, starting from zero and ending at a specified percentage value. The values of the counter are dynamically retrieved from a database. function timer() { if ...

GeoJson with timestamp and marked directional indicator

I am looking to create an animation of a boat traveling along a specific route. Currently, I am able to display the route as a line and the boat as a circle using TimestampedGeoJson: # circle with following line features = [ { 'type': ...

What could be causing this Javascript code to not increase by 1?

I previously had a piece of code that effectively incremented and decremented the input value by 1 whenever the + or - buttons were clicked. I made some changes to the design recently, and now I'm struggling to target the input element. Any assistance ...

Use a conditional statement for each element within the array

Below is the code I am currently using: if (res[0].toString() == "hello") { res[0] = "string"; }; While it works, I would like this logic to apply to all elements rather than just the first one. Is there a way to achieve this for every element in the ar ...

Troubleshooting the canLoad problem caused by returning an observable with a 404 error

I have implemented a check to call an API in the canLoad event for a module in my Angular application. However, even if the API returns a 404 error in the network, my page continues to load. I want to change this behavior so that if a 404 error is encount ...

Unpredictable Results with JSON Data

Can anyone shed some light on why all my JSON data is showing up as Undefined? Here is the JSON snippet in question: {"273746":[{"name":"Darius's Wizards","tier":"GOLD","queue":"RANKED_SOLO_5x5","entries":[{"playerOrTeamId":"273746","playerOrTeamName ...

Leverage JQuery Mobile for smooth sliding and effortless deletion of list elements

Currently, I am testing a JQuery Mobile webpage that features a simple list setup: Upon clicking the list items, they get highlighted and their ids are saved in a local array. Is there an easy (or not so easy) way to transition the selected elements slidi ...

Refresh the Express Server following the file upload

This is a simple express server setup that includes file uploading and reading functionality. const express = require("express") const fileUpload = require("express-fileupload") const cors = require("cors") const morgan = require("morgan") const fs = requi ...

Is there a way to set a server-side string value using the Dynamic UI Label on page load?

Is there a way to set the value of a Dynamic UI Label to a server-side string during page load? Below is the code snippet in question: // ASP.NET Label element <html> <body> <asp:Label ID="dynmcHiddenLabel" runat="server" Text= ...

Make sure to invoke the navigate() function within a React.useEffect() hook, rather than calling it during the initial rendering of

I am new to ReactJS. When the page initially loads, I need to check if the state is null, and if it is, redirect to a different login Page Below is the code for the component: export default function Addblousesalwar() { const navigate = useNavigate(); ...

Approach for safeguarding unrecorded data in the data management system

In my current setup, I have a repeater control that contains various input controls such as dropdowns, buttons, and checkboxes for search functionality. The issue arises when these controls trigger postbacks, leading to potential data loss within the repea ...

Enhance your Rails 5 application with a dynamic live search feature powered by Keyup. Say goodbye to

Currently, I have a Rails 5.1.3 application with a simple contact model that stores names and phone numbers. To enable searching within the index view/page, I am utilizing Ransack. A keyup event listener written in Coffeescript captures user input as they ...

Guide to creating a ReactJS higher-order component using modern ES6 syntax

I´m attempting to create a ReactJS higher-order component using ES6 syntax. This is what I have so far: export const withContext = Component => class AppContextComponent extends React.Component { render() { return ( ...

The headers in the div section are not properly aligned on the webpage

My current project's website has a homepage with two rows of content. Each row consists of 2 paragraphs, each with a heading and corresponding buttons. However, the issue is that I want the headers in each row to align with one another so that the fir ...

a service that utilizes $http to communicate with controllers

My situation involves multiple controllers that rely on my custom service which uses $http. To tackle this issue, I implemented the following solution: .service('getDB', function($http){ return { fn: function(){ return $http({ ...

Issues with Wordpress Ajax request on customized page are causing functionality to fail

Everything appears to be in order, but I am still unable to populate the destination. So far, I have confirmed that the PHP and JS files within my plugin are being located and can even generate output in XML. This is evident when observing the default beh ...