CSS Basic
CSS :invalid Selector
Photo Credit to CodeToFun
π Introduction
The :invalid
selector in CSS is used to select form elements whose values fail to meet validation criteria. It is especially useful for styling form inputs that have incorrect or incomplete data based on input type, pattern, or required fields.
This selector helps enhance form usability and provides visual feedback to users during input.
π‘ Syntax
The signature of the :invalid
Selector is as follows:
:invalid {
/* CSS properties */
}
The :invalid
selector applies to form elements such as <input>
, <textarea>
, and <select>
that have built-in validation rules, including type
, pattern
, min
, max
, or the required
attribute.
π Example
Hereβs a practical example that demonstrates the use of the :invalid
selector:
β οΈ HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS :invalid Selector Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<form>
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Enter your email" required>
<label for="age">Age:</label>
<input type="number" id="age" name="age" min="18" max="65" placeholder="Enter your age" required>
<button type="submit">Submit</button>
</form>
</body>
</html>
π¨ CSS
/* Style for invalid form elements */
:invalid {
border: 2px solid red;
background-color: #ffe0e0;
}
/* Style for valid form elements */
:valid {
border: 2px solid green;
background-color: #e0ffe0;
}
In this example:
- If the email input field is left empty or contains an invalid email address, it will be styled with a red border and a light red background.
- The age input must be a number between 18 and 65. If itβs outside this range or left empty, it will also be marked as invalid.
π¬ Usage Tips
- The
:invalid
selector works in conjunction with built-in HTML5 validation attributes liketype
,pattern
,required
,min
, andmax
. - You can combine
:invalid
with other selectors to create more specific styling rules. For instance,input:invalid
will apply only to invalid input elements. - Pair
:invalid
with:valid
to provide users with feedback when they correct their input.
β οΈ Common Pitfalls
- The
:invalid
selector will not work if the form or input fields lack validation rules such asrequired
,pattern
, ortype
. - Different browsers may show their own validation styles (such as tooltips or borders), so itβs important to test your styles across browsers to ensure consistent behavior.
- If JavaScript is used for custom validation, it might override the default browser validation and affect how
:invalid
works.
π Conclusion
The :invalid
selector is a key component of modern form design, allowing developers to provide instant feedback when users input invalid data.
By using this selector, you can improve form accessibility, enhance user experience, and make data validation clearer and more visually intuitive.
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS :invalid Selector), please comment here. I will help you immediately.