CSS Form Styling

Forms play a fundamental role in web development, serving as a means for individuals to engage with websites. While their functionality is essential, their visual presentation is equally important. This tutorial delves into the art of designing forms with CSS, enhancing both usability and aesthetic appeal for users.

Before delving into the complexities of form styling, it's fundamental to handle the essential structure of HTML forms. A basic form ordinarily comprises elements like <form>, <input>, <label>, and different other input types. In the example, we have a basic form with a username and password field alongside a submit button. The related labels (<label>) are utilized for availability and styling purposes. Here is an example:

Example

<!DOCTYPE html>
<html lang = "en">
<head>
  <meta charset = "UTF-8">
  <meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
  <title>Styled Form</title>
  <link rel = "stylesheet" href = "styles.css">
</head>
<body>

  <form>
    <label for = "username">Username:</label>
    <input type = "text" id = "username" name = "username" required>

    <label for = "password">Password:</label>
    <input type = "password" id = "password" name = "password" required>

    <input type = "submit" value = "Submit">
  </form>

</body>
</html>

Resetting the Default Styles

Browsers come with pre-set styles for form components, and a crucial step in styling is often to override these defaults to establish a consistent foundation across different browsers. This can be achieved by using a CSS reset or, more precisely, by adding styles to specific elements.

Example

/* styles.css */

/* Resetting default styles for form elements */
form, label, input {
  margin: 0;
  padding: 0;
  border: 0;
}

/* Ensuring elements are displayed as block */
label, input {
  display: block;
}

/* Adding some spacing between elements */
label {
  margin-bottom: 8px;
}

In this snippet of code, we've zeroed out the margin, padding, and border of the form, label, and input components to remove any inherent spacing. Additionally, we've configured the showcase attribute to force labels and inputs onto separate lines, enhancing readability. To create clearer visual distinction, we've introduced spacing between the labels.

Styling Form Inputs

Given the opportunity for a new beginning, it is essential to prioritize the customization of form input elements. An established convention involves applying distinct styles to text fields, password fields, and submission buttons to create a clear visual hierarchy.

Example

/* styles.css */

/* Styling text inputs */
input[type = "text"], input[type = "password"] {
  width: 100%;
  padding: 10px;
  margin-bottom: 16px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}

/* Styling the submit button */
input[type = "submit"] {
  width: 100%;
  padding: 12px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

In this styling example, a standard style has been implemented for message and password input fields. These fields are set to occupy the full width of their container, include padding for improved spacing, and feature a seamless border radius for a polished look. The box-sizing property is configured as border-box to ensure that the padding and border are included in the element's total width calculation.

The submit button, on the other hand, features a distinct design. It spans the entire width with a green background, white text, and a cursor indicating its interactivity. This design choice creates a clear visual distinction between regular inputs and the submit button.

Upgrading User Experience with Pseudo-classes

Pseudo-classes within CSS enable us to select and format elements based on their current state. In the context of forms, :focus and :hover pseudo-classes play a significant role in enhancing user interaction.

Example

/* styles.css */

/* Highlighting input fields on focus */
input[type = " text "]:focus, input[type = " password "]:focus {
  outline: none;
  border-color: #6a7eff;
  box-shadow: 0 0 5px rgba(106, 126, 255, 0.5);
}

/* Changing button color on hover */
input[type = " submit "]:hover {
  background-color: #45a049;
}

The focus pseudo-class highlights input fields when they are selected. We have removed the default outline, modified the border color, and included a concealed box shadow to provide a clear indication to the user about the active input field. On hovering over the submit button, the background color alters, signaling to the user that the button is interactive.

Styling Labels for Clarity

Labels are crucial for guiding users through forms, and effectively styling them can enhance both their appearance and the user experience. It is important to focus on improving the visual presentation of our labels.

Example

/* styles.css */

/* Styling labels for clarity */
label {
  font-size: 16px;
  color: #333;
  font-weight: bold;
}

/* Aligning labels with their corresponding inputs */
label, input {
  width: 100%;
  box-sizing: border-box;
}

In the preceding code, we have increased the size of labels, applied a lighter text shade for improved contrast, and boldened the text for emphasis. Additionally, we ensure that labels and input fields span the entire width of their container using the width: 100% property, ensuring a consistent layout.

Making a Responsive Form Layout

With the rise of mobile devices, it is crucial to ensure that your forms are responsive. This can be achieved by using media queries to adjust styles based on the screen size.

Example

/* styles.css */

/* Responsive form layout */
@media screen and (max-width: 600px) {
  input[type = "text"], input[type = "password"] {
    width: calc(100% - 20px);
    margin-bottom: 12px;
  }

  input[type = "submit"] {
    width: calc(100% - 24px);
  }
}

In this code excerpt, we make use of a media query to implement specific styles when the viewport width is 600 pixels or below. The calc function is employed to adjust the width, considering margins on both sides. This ensures that on smaller screens, the form components are not stretched to the entire width, enhancing readability and overall aesthetic appeal.

Adding a Splash of Color

Color plays a vital role in drawing attention to specific components. Let's enhance our form's visual appeal by incorporating a color. In this case, we've opted for a light, subtle background color for the body to create a subtle contrast with the form. The form itself features a white background, padding for spacing, rounded corners for a softer appearance, and a box shadow for a subtle elevation effect.

Example

/* styles.css */

/* Adding color to the form */
body {
  background-color: #f4f4f4;
}

form {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

Example

We need to consolidate all the styles we have created into a single CSS file.

Example

/* styles.css */

/* Resetting default styles for form elements */
form, label, input {
  margin: 0;
  padding: 0;
  border: 0;
}

/* Ensuring elements are displayed as block */
label, input {
  display: block;
}

/* Adding some spacing between elements */
label {
  margin-bottom: 8px;
}

/* Styling text inputs */
input[type = "text"], input[type = "password"] {
  width: 100%;
  padding: 10px;
  margin-bottom: 16px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}

/* Styling the submit button */
input[type = "submit"] {
  width: 100%;
  padding: 12px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

/* Highlighting input fields on focus */
input[type = "text"]:focus, input[type = "password"]:focus {
  outline: none;
  border-color: #6a7eff;
  box-shadow: 0 0 5px rgba(106, 126, 255, 0.5);
}

/* Changing button color on hover */
input[type = "submit"]:hover {
  background-color: #45a049;
}

/* Styling labels for clarity */
label {
  font-size: 16px;
  color: #333;
  font-weight: bold;
}

/* Aligning labels with their corresponding inputs */
label, input {
  width: 100%;
  box-sizing: border-box;
}

/* Responsive form layout */
@media screen and (max-width: 600px) {
  input[type = "text"], input[type = "password"] {
    width: calc(100% - 20px);
    margin-bottom: 12px;
  }

  input[type = "submit"] {
    width: calc(100% - 24px);
  }
}

/* Adding color to the form */
body {
  background-color: #f4f4f4;
}

form {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

Conclusion

Becoming proficient in CSS form design is a crucial skill for web developers who aim to create visually pleasing and user-friendly interactive elements. Over the course of these two parts, we have discussed the fundamentals of resetting default styles, customizing form inputs and labels, handling responsive form layouts, and incorporating color schemes.

By acquiring and implementing these techniques, you can elevate your forms, offering users a cohesive and seamless experience. As web development trends progress, staying abreast of best practices and continuously honing your design skills will contribute to the success of your projects.

Input Required

This code uses input(). Please provide values below: