CSS Validated Input
Shows a red error state only after the user has typed something invalid, with no JavaScript.
HTML
<label class="field-validate">
<span>Email</span>
<input type="email" required placeholder="you@site.dev" value="not-an-email">
<small>Please enter a valid email address.</small>
</label>
CSS
.field-validate {
display: grid;
gap: 6px;
width: 240px;
font: 14px/1.3 system-ui, sans-serif;
color: #27272a;
}
.field-validate span {
font-weight: 600;
}
.field-validate input {
padding: 10px 12px;
border: 1.5px solid #d4d4d8;
border-radius: 10px;
background: #fff;
font: inherit;
outline: none;
transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.field-validate input:focus {
border-color: #6366f1;
box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}
.field-validate small {
display: none;
color: #dc2626;
}
/* Invalid, but only once the field has text in it */
.field-validate input:invalid:not(:placeholder-shown) {
border-color: #dc2626;
box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.15);
}
.field-validate input:invalid:not(:placeholder-shown) + small {
display: block;
}
About this CSS Validated Input
This CSS Validated Input is 41 lines of pure CSS (0.8 KB) with no JavaScript and no dependencies. It uses transitions, box shadows and CSS Grid. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.
CSS features used
Customize it
The colors in this snippet. Search and replace them to match your brand:
#27272a#d4d4d8#fff#6366f1#dc2626
How to use this validated input
- Copy the HTML and paste it where you want the element to appear.
- Copy the CSS into your stylesheet, or inside a
<style>tag. - Keep the
.field-validateclass name, or rename it in both places. Then tweak colors, sizes and timing to match your design. - Using React or Next.js? Change
classtoclassNamein the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).
Building with an AI tool?
“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.