Styling Valid and Invalid Email Input Using CSS

Styling Valid and Invalid Email Input Using CSS
Code Snippet:CSS Only Status Input Stroke
Author: Jhey
Published: March 22, 2024
Last Updated: March 22, 2024
Downloads: 40
License: MIT
Edit Code online: View on CodePen
Read More

This CSS code snippet is helpful for styling valid and invalid email input. It applies background colors to inputs based on validation states. It helps enhance user experience by visually indicating valid and invalid inputs.

You can use this code in web forms that require email input validation. It enhances user interaction by providing visual feedback on input validity. Additionally, it improves accessibility by ensuring users can easily identify valid and invalid inputs.

How to Create Styling Valid And Invalid Email Input Using CSS

1. First of all, load the Normalize CSS by adding the following CDN link into the head tag of your HTML document.

<link rel="stylesheet" href="">

2. Create an HTML form with an input field for email. Ensure the input type is set to “email” for built-in validation support.

<form action="">
  <div class="form-group">
    <label for="email">Email</label>
    <input type="email" id="email" spellcheck=false required placeholder="Email" />

3. Now, let’s style the email input using CSS. We’ll define background colors for valid and invalid states to provide visual feedback to users.

@font-face {
  font-family: "Geist Sans";
  src: url("") format("truetype");

:root {
  --valid: hsl(140 80% 40%);
  --invalid: hsl(10 80% 40%);
  --input: hsl(0 0% 0%);

* {
  box-sizing: border-box !important;

  min-height: 100vh;
  display: grid;
  place-items: center;
  font-family: "Geist Sans", "SF Pro", sans-serif;
  font-weight: 100;
  background-color: hsl(0 0% 6%) !important;
  color: hsl(0 0% 98%);

.form-group {
  --active: 0;
  container-type: inline-size;
  flex: 1;

form {
  width: 40ch;
  display: flex;

input {
  --is-valid: 0;
  --is-invalid: 0;
  border: 0;
    linear-gradient(var(--input), var(--input)) padding-box,
    linear-gradient(var(--invalid), var(--invalid)) calc((1 - var(--is-invalid)) * -100cqi) 0 / 100% 100% border-box,
    linear-gradient(var(--valid), var(--valid)) calc((1 - var(--is-valid)) * 100cqi) 0 / 100% 100% border-box,
  border: 2px solid transparent;
  font-size: 2rem;
  padding: 1rem 2rem;
  background-repeat: no-repeat;
  background-size: 100% 100%;
/*   background-position:
    0 0,
    calc((1 - var(--is-invalid)) * -100cqi) 0,
    calc((1 - var(--is-valid)) * 100cqi) 0,
    0 0; */
  max-width: 100%;
  width: 100cqi;
  color: hsl(0 0% 80%);
  font-family: "Geist Sans", "SF Pro", sans-serif;
  font-weight: 40;
  border-radius: 12px;
  outline: none;
  box-shadow: 0 1px hsl(0 0% 100% / 0.35) inset,
    0 -1px hsl(0 0% 0% / 1) inset,
    0 10px 20px -5px hsl(0 0% 0% / 1);

label {
  margin-bottom: 0.5rem;
  display: inline-block;
  padding-left: 2rem;
  opacity: calc(var(--active) + 0.45);
  transition: opacity 0.5s;

.form-group:focus-within {
  --active: 1;

input:invalid:not(:-moz-placeholder-shown):not(:focus-visible) {
  --is-invalid: 1;

input:invalid:not(:-ms-input-placeholder):not(:focus-visible) {
  --is-invalid: 1;

input:invalid:not(:placeholder-shown):not(:focus-visible) {
  --is-invalid: 1;

input:valid {
  --is-valid: 1;

input::-moz-placeholder {
  color: transparent;

input:-ms-input-placeholder {
  color: transparent;

input::placeholder {
  color: transparent;

@media(prefers-reduced-motion: no-preference) {
  input {
    transition: background-position 0.5s;

Test your form by entering both valid and invalid email addresses. Notice how the input field changes color to indicate its validity status.

That’s all! hopefully, you have successfully created Styling Valid And Invalid Email Input on your website. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link