Photo by Brooke Cagle on Unsplash

Taking a simple Contact Form to the next level with css animation and three.js

Today I’m going to make a boring Contact Form fun. Here is glimpse of what we will start building today :

In this post I will discuss the HTML and css part with a bit of JavaScript to make the form fully functional and in the next post I’ll discuss how I made the interactive star-field background with three.js.

Let’s Start with HTML

The HTML should be pretty strait forward, two label and two input-box with type="text"

Two things to notice here,

1 ) first notice I wrapped both the label and input element with divs because I wanted to make the labels to look like permanent placeholders.

2) The required and minlength in text input-boxes. require enforces that those input fields needs to be filled before submitting the form and minlength=3 enforces the input to be at least 3 character long.

You might be wondering why bother with it tho? —I promise it will make sense when we will style them.

CSS to Make it awesome

It could be nice I were to explain my css line by line but that will make this post too long which I definitely want to avoid, but I will walk you through most of important bits and pieces.

Styling the form

I made the form a flex container with flex-direction:column; and centered them horizontally centered with align-items:center; also I made the form centered in the middel of the page with display:grid; and place-items:center; and DON’T forget to use min-height:100vh on the grid-container.

To make the label to look like a placeholder made the position:relative; and style the label with following properties:

First three properties will make it all capital with bold font and a grayish color, 2nd set of properties will place the label vertically centered and horizontally left aligned inside the input-box. The last two will bring it on top of the input element and remove all pointer events, i.e. it will not interact with any pointing device.

Now for placeholder to work properly the input element will need a padding-left:6ch; I also added additional styling to the input element with :focus and :valid . “focus” pseudo-class is simple with someone clicks it it gains focus and activates the set of styling, in my case I added a deep-orange colored left border to it.

The “valid” is triggered when the form becomes valid, for my case the form is valid when name is more than 3 character long and correctly formatted email address is entered. Now that should explain why I added those attributes( required & minlength) 😉. I also added border transition to make the transition between border-colors smoother.

Animating the Loading-in animation

To do that I will use that hidden class to hide elements with clip-path. To make it work properly we need to add clip-path property to the elements too which have the hidden class and make it smooth we’ll add in a transition too.

Notice I have put var(--delay) as the delay of the transition, well where does it come from ?

It came form the css custom properties I defined within the style attribute of the elements. it is replaced with thouse defined values (100ms, 200ms & 400ms). It will make the transition start at different times.

Now we will use JS to remove the hidden class to complete the process.

Making the 🚀 launch

For this I have used the span with the id “rocket”, first we’ll add styling to it. We will add in animation later.

The heightis zero so we wont be able to see it for now. Here the important bit is the linear-gradient which will make it look like a space ship with burning fuel form the bottom. and it is centered in the middle right over the form with display:absolute and

Now we should focus on the form animation where I decided to squeeze it down to a line with again clip-path then bringing the opacity to 0. This keyframes animation will do just that :

To add the animation to the form we need to include the following form element.

and the rocket keyframe animation will be :

It will make the height to 200px within 25% of the animation-duration so it will become visible and it will maintain the height throughout the animation. The transform will make it shoot out of the screen but it will not start immediately after the animation starts, it will delayed by 15% of the animation-duration.Now lets attach it to the #roket with following properties to the style of it:

the 750ms delay will make the room for the previous “form-closing” animation and start this at the right time. Both the animation are in paused state, with the help of JS we will change the animation-play-state to running to play the animation and it will play the the animation once as the animation-fill-mode is set to forwards.

The last three lines will make the span with the msg class appear from behind.

That’s it we have done it. The HMTL css part is over, now the contact form should look like this, but without the interactive background, which I’ve discussed in my next post Adding a Custom Star-Field Background with threejs .

I have used clippy to make my clip-path which makes it lot easier to use clip-path. This made for Scrimba’s Weekly Web Dev Challenge 3rd week of April, 2021.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store