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="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
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" /> </div> </form>
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("https://assets.codepen.io/605876/GeistVF.ttf") format("truetype"); } :root { --valid: hsl(140 80% 40%); --invalid: hsl(10 80% 40%); --input: hsl(0 0% 0%); } * { box-sizing: border-box !important; } body{ 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; background: 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, var(--input); 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.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.