CSS Portal

:invalid CSS Pseudo Class

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
:invalid
CSS Pseudo ClassThis pseudo class is supported by all modern browsers.
Updated 31st December 2025

Description

The :invalid pseudo-class in CSS is used to select form elements that fail to meet the constraints set by HTML attributes such as input type, pattern, minlength, maxlength, or other validation-related attributes. This pseudo-class allows developers to apply specific styles to form fields that contain invalid data, providing immediate visual feedback to users before the form is submitted. It is particularly useful in improving user experience by highlighting errors in real time.

The :invalid pseudo-class works in conjunction with the :valid pseudo-class, enabling developers to define complementary styles for valid and invalid inputs. It is commonly used with border-color, background-color, color, and box-shadow properties to make invalid fields stand out. For example, setting a red border on an input field when it fails validation can alert the user immediately that their input requires correction.

The behavior of :invalid depends on the browser’s native form validation rules and the specific input type. For example, an input with type="email" will automatically trigger :invalid if the value entered does not match the correct email format. Additionally, :invalid can be combined with other pseudo-classes such as :focus to style an element differently when it is both focused and invalid.

Example:

<form>
  <label for="email">Email:</label>
  <input type="email" id="email" required>
  <button type="submit">Submit</button>
</form>
input:invalid {
  border: 2px solid red;
  background-color: #ffe6e6;
}

input:valid {
  border: 2px solid green;
  background-color: #e6ffe6;
}

In this example, the input field will have a red border and light red background whenever the entered value is not a valid email address. Once the user enters a correctly formatted email, the field changes to green, indicating validity. Using :invalid improves form usability and reduces the likelihood of submitting incorrect data.

Syntax

:invalid {
  /* ... */
}

Example

<form>
<div class="field-group">
<label for="user-email">Email Address:</label>
<input
type="email"
id="user-email"
placeholder="e.g., name@example.com"
required
>
<span class="error-message">Please enter a valid email address.</span>
</div>

<div class="field-group">
<label for="user-age">Age (18+):</label>
<input
type="number"
id="user-age"
min="18"
max="120"
required
>
<span class="error-message">You must be at least 18.</span>
</div>

<button type="submit">Submit</button>
</form>
/* Default styling for the input */
input {
border: 2px solid #ccc;
padding: 8px;
border-radius: 4px;
outline: none;
display: block;
margin-bottom: 5px;
}

/* 1. Style the input when it is invalid */
input:invalid {
border-color: #ff4d4d;
background-color: #fff5f5;
}

/* 2. Hide error messages by default */
.error-message {
color: #ff4d4d;
font-size: 0.8rem;
display: none;
}

/* 3. Show the error message only when the input is invalid */
input:invalid + .error-message {
display: block;
}

/* Optional: Style for valid input for contrast */
input:valid {
border-color: #2ecc71;
}
Open in editor

Browser Support

The following information will show you the current browser support for the CSS :invalid pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS pseudo class.

This pseudo class is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee