How to Build Interactive Forms Using HTML and CSS

How to Build Interactive Forms Using HTML and CSS

In modern web applications, forms are a very important portal that enables communication between users and website owners. Making the forms interactive is a crucial task for web developers, as these forms have the potential to attract more customers and increase lead conversions. In this article, we discuss how to create interactive forms using HTML and CSS.

Prerequisites

Before proceeding with this article, make sure you understand the basics of HTML and CSS, such as building the grid layout, creating transitions, animations, and so on.

What are the HTML form elements

Let's begin with a brief review of the HTML form elements.

The <form> element

First of all, there is a <form> element that acts as the container for the entire form.

html
1<form action="some_program.php" method="POST">. . .</form>

The element accepts two attributes, action and method. action points to the program that will be processing the form data after it has been submitted. And method defines the corresponding HTTP method that will be used to transmit the form data.

The input fields

Inside the <form> element, there should be input fields and their associated labels.

html
1<form action="some_program.php" method="POST">
2  <label for="username">Name:</label>
3  <input type="text" id="username" name="username" />
4</form>

Note that the for attribute of <label> matches the id of the <input> element.

text input

When designing your form, you should remember that it might be used by different audiences, so it is very important that you do not forget to add labels for the input fields to ensure accessibility.

The <input> element comes in many different types, and you can specify it using the type attribute.

html
1<form action="some_program.php" method="POST">
2  <label for="username">Name:</label>
3  <input type="text" id="username" name="username" />
4
5  <input type="button" />
6  <input type="checkbox" />
7  <input type="color" />
8  <input type="date" />
9  <input type="datetime-local" />
10  <input type="email" />
11  <input type="file" />
12  <input type="hidden" />
13  <input type="image" />
14  <input type="month" />
15  <input type="number" />
16  <input type="password" />
17  <input type="radio" />
18  <input type="range" />
19  <input type="reset" />
20  <input type="search" />
21  <input type="submit" />
22  <input type="tel" />
23  <input type="text" />
24  <input type="time" />
25  <input type="url" />
26  <input type="week" />
27</form>

input fields

Besides the <input>, there is also <textarea>, which allows you to define a multi-line input field.

html
1<textarea name="message" rows="10" cols="30">
2  Lorem ipsum . . .
3</textarea>

The rows and cols attributes are used to define the initial size of the <textarea> element when it is first loaded.

And lastly, there is a <select> field that allows the user to select one or more options from a list.

html
1<label for="programming-languages">Choose a programming language:</label>
2<select id="programming-languages" name="programming-languages">
3  <option value="javascript">JavaScript</option>
4  <option value="python">Python</option>
5  <option value="java">Java</option>
6  <option value="csharp">C#</option>
7</select>

select field

The <option> element defines the options for the select field. When the form is submitted, the corresponding value will be passed to the backend to be processed.

Building your first form

For this tutorial, let's build a signup form. We'll start with the HTML structure.

html
1<form action="/some_program.php" method="POST">
2  <label for="username">Username:</label>
3  <input type="text" name="username" id="username" />
4
5  <label for="email">Email:</label>
6  <input type="email" name="email" id="email" />
7
8  <label for="password">Password:</label>
9  <input type="password" name="password" id="password" />
10
11  <input type="submit" value="Submit" />
12</form>

HTML form

This form contains a username field, an email field, a password field, as well as a submit button.

Aside from the submit field, which is a special case, each <input> field has a corresponding <label>, which describes the purpose of that field. The for attribute of <label> matches the id of the <input> field.

When the submit button is clicked, the browser will gather all the user inputs and create a key/value data structure. With the name attribute of each field being the key, and the corresponding user inputs being the value. Then, the data will be transmitted to the some_program.php program to be processed using a POST method.

How to style the form using CSS

As you can see, this form is only a skeleton. So next, let's make the form look better by adding some CSS. We'll start by removing all the default paddings and margins and also set box-sizing to border-box, making resizing the elements easier.

css
1* {
2  box-sizing: border-box;
3  padding: 0px;
4  margin: 0px;
5}

And then, make sure the form is centered both horizontally and vertically. You may skip this step if you don't need it to be centered.

css
1body {
2  font-family: Arial, Helvetica, sans-serif;
3  background-color: #f4f4f4;
4  height: 100vh;
5
6  /* Center the entire form */
7  display: flex;
8  justify-content: center; /* Horizontally */
9  align-items: center; /* Vertically */
10}

Line 7 to 9 is a commonly used method to center elements using CSS. Of course, you can go with any other methods explained in the linked article.

Using a flexbox layout

Since this form is fairly basic, you could use a flexbox layout to create a single-column form like this:

css
1form {
2  display: flex;
3  flex-direction: column;
4}

And, of course, don't forget to style the individual input fields and their labels.

css
1label,
2input {
3  margin-bottom: 15px;
4  width: 400px;
5}
6
7input[type="text"],
8input[type="email"],
9input[type="password"],
10input[type="submit"] {
11  padding: 10px;
12  border: 1px solid #ccc;
13  border-radius: 4px;
14}
15
16input[type="submit"] {
17  background-color: #3498db;
18  color: #fff;
19  cursor: pointer;
20}

styled form

Using a grid layout

For more complex forms, we recommend using a grid layout instead.

html
1<form>
2  <div class="grid-container">
3    <div class="grid-item">
4      <label for="first-name">First Name:</label>
5      <input type="text" id="first-name" name="first-name" />
6    </div>
7    <div class="grid-item">
8      <label for="last-name">Last Name:</label>
9      <input type="text" id="last-name" name="last-name" />
10    </div>
11    <div class="grid-item">
12      <label for="email">Email:</label>
13      <input type="email" id="email" name="email" />
14    </div>
15    <div class="grid-item">. . .</div>
16  </div>
17
18  <input type="submit" value="Submit" />
19</form>
css
1.grid-container {
2  display: grid;
3  grid-template-columns: repeat(2, 1fr);
4  gap: 20px;
5}
6
7.grid-item {
8  margin-bottom: 15px;
9}

Line 3 creates a two-column grid layout with equal sizes.

How to make the form more interactive

And now, we are at the most important part of this tutorial. Let's make this form more interactive, which means you need to create more feedback for user actions. The first tool you could utilize is the pseudo-selectors.

Using pseudo-selectors

Pseudo-selectors are used to select HTML elements based on their states. For example, :hover selects an element only when the cursor is hovered on top of that element.

html
1<div>Hover over me</div>
css
1div {
2  padding: 10px;
3  margin: auto;
4  border: 2px solid darkviolet;
5  border-radius: 10px;
6  font-family: "Trebuchet MS", "Lucida Sans Unicode", "Lucida Grande",
7    "Lucida Sans", Arial, sans-serif;
8
9  color: darkviolet;
10  width: 200px;
11}
12
13div:hover {
14  color: white;
15  background-color: darkviolet;
16  width: 400px;
17}

Hover over me

You can make the form more interactive by utilizing these pseudo-selectors. For instance:

css
1input[type="submit"]:hover {
2  background-color: #2980b9;
3}
4
5input:focus {
6  outline: solid #3498db;
7}

The :hover selector activates when a cursor hovers on top of the elements, and in this case, the submit button will turn into a darker blue when the cursor hovers on top. The :focus selector activates when the input field is in focus, and the element will be given an extra outline. Together, they ensure that proper feedback will be returned whenever the user clicks on an input field.

input field outline

Form input validation

Validating the user inputs on the client side before the data reaches the server is a very important task when designing your form. It can be done by specifying additional attributes to the corresponding form fields.

When designing your form, you should make sure that when the user types in a wrong input, proper feedback is sent back to the user. Let's take a look at an example.

html
1<input
2  type="text"
3  name="username"
4  id="username"
5  minlength="10"
6  maxlength="20" />

Here, we added some validation rules for the input field. minlength and maxlength each specify the minimum and maximum length of the input string.

When the user input passes the rules, it will have the :valid state. If not, it will acquire the :invalid state. We can use that to specify different styles for the field under different state.

css
1input:valid {
2  border: solid #3498db;
3}
4
5input:invalid {
6  border: solid red;
7  outline: solid red;
8}

form input validate

Using transitions and animations

You can even take it one step further and add some transition or animation effects. For example, you can add a transition effect to the input fields, making them grow longer when in focus and smoothly transitioning back to normal when not in focus.

css
1label,
2input {
3  margin-bottom: 15px;
4  width: 400px;
5
6  transition-property: width;
7  transition-duration: 1s;
8}
9
10input[type="text"]:focus,
11input[type="email"]:focus,
12input[type="password"]:focus {
13  width: 500px;
14}

form field growing

Of course, you can get more creative with this, such as adding a shaking effect combined with color change when the user types something wrong, or a sliding/fading effect when the user submits the form.

Using SVG animations

SVG animation is also something you could add to your form to make it more interactive.

SVG (Scalable Vector Graphics) is an XML-based format used to describe vector images. Unlike regular image formats such as JPEG and PNG, SVGs are smaller, faster, and easier to render. As a result, it is often used to create icons, logos, and illustrations.

For demonstration purposes, let's create a loading spinner that you can add to the form to tell the user that something is being loaded.

html
1<!-- prettier-ignore -->
2<svg id="loading-spinner" xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 48 48"><defs><linearGradient id="spinner-gradient-a" x1="49.892%" x2="55.03%" y1="58.241%" y2="89.889%"><stop offset="0%"/><stop offset="22.44%" stop-opacity=".59"/><stop offset="100%" stop-opacity="0"/></linearGradient></defs><g fill="none" transform="translate(-8 -8)"><path d="M32,56C18.745166,56,8,45.254834,8,32C8,18.745166,18.745166,8,32,8C45.254834,8,56,18.745166,56,32C56,45.254834,45.254834,56,32,56ZM32,52C43.045695,52,52,43.045695,52,32C52,20.954305,43.045695,12,32,12C20.954305,12,12,20.954305,12,32C12,43.045695,20.954305,52,32,52Z"/><path fill="url(#spinner-gradient-a)" d="M56,32C56,33.1045695,55.1045695,34,54,34C52.8954305,34,52,33.1045695,52,32C52,20.954305,43.045695,12,32,12C20.954305,12,12,20.954305,12,32C12,43.045695,20.954305,52,32,52C33.1045695,52,34,52.8954305,34,54C34,55.1045695,33.1045695,56,32,56C18.745166,56,8,45.254834,8,32C8,18.745166,18.745166,8,32,8C45.254834,8,56,18.745166,56,32Z" transform="rotate(45 32 32)"/></g></svg>
css
1#loading {
2  animation: loading-spinner 1s linear infinite;
3}
4
5@keyframes loading-spinner {
6  from {
7    transform: rotate(0deg);
8  }
9  to {
10    transform: rotate(360deg);
11  }
12}

loading spinner

Conclusion

In this article, we went over some essential techniques to use when designing your form and making it more interactive for the users, including pseudo-selectors, transitions, animations, and SVGs. Together, they create endless possibilities.

Interested in Learning Web Development?

Starting from HTML, CSS to JavaScript, from language syntaxes to frameworks, from the frontend to the backend, we'll guide you through every step of your coding journey.

Get started from here 🎉