The valid and invalid pseudo-classes are used to format controls based on whether their field values pass a validation test or not. For example, the following style rule displays all the input elements containing invalid data with a light red background:
In put: invalid {
background-color: rgb (255, 232, 233);
}
On the other hand, the following style rule displays all the input elements containing valid data with a light green background:
input: valid {
background-color: rgb (220, 255, 220);
}
Both of these style rules set the background color whether the input element has the focus or not. Displaying a form full of input backgrounds with different background colors can be confusing and distracting to the user. As a result, it is better practice to highlight invalid field values only when those input controls have the focus, as shown in the following style rule that combines both the focus and invalid pseudo-classes:
input:focus:invalid {
background-color: rgb (255, 232, 233);
}