:valid
Quick Summary for :valid
The :valid CSS pseudo-class represents any <input> or other <form> element whose contents validate successfully. This allows to easily make valid fields adopt an appearance that helps the user confirm that their data is formatted properly.
Code Usage for :valid
/* Selects any valid <input> */ input:valid {   background-color: powderblue; } 
More Details for :valid

:valid

The :valid CSS pseudo-class represents any <input> or other <form> element whose contents validate successfully. This allows to easily make valid fields adopt an appearance that helps the user confirm that their data is formatted properly.

/* Selects any valid <input> */ input:valid {   background-color: powderblue; } 

This pseudo-class is useful for highlighting correct fields for the user.

Syntax

:valid

Examples

Indicating valid and invalid form fields

In this example, we use structures like this, which include extra <span>s to generate content on; we'll use these to provide indicators of valid/invalid data:

<div>   <label for="fname">First name *: </label>   <input id="fname" name="fname" type="text" required>   <span></span> </div> 

To provide these indicators, we use the following CSS:

input + span {   position: relative; }  input + span::before {   position: absolute;   right: -20px;   top: 5px; }  input:invalid {   border: 2px solid red; }  input:invalid + span::before {   content: '✖';   color: red; }  input:valid + span::before {   content: '✓';   color: green; } 

We set the <span>s to position: relative so that we can position the generated content relative to them. We then absolutely position different generated content depending on whether the form's data is valid or invalid — a green check or a red cross, respectively. To add a bit of extra urgency to the invalid data, we've also given the inputs a thick red border when invalid.

Note: We've used ::before to add these labels, as we were already using ::after for the "required" labels.

You can try it below:

Notice how the required text inputs are invalid when empty, but valid when they have something filled in. The email input on the other hand is valid when empty, as it is not required, but invalid when it contains something that is not a proper email address.

Accessibility concerns

The color green is commonly used to indicate valid input. People who have certain types of color blindness will be unable to determine the input's state unless it is accompanied by an additional indicator that does not rely on color to convey meaning. Typically, descriptive text and/or an icon are used.

MDN Understanding WCAG, Guideline 1.4 explanations Understanding Success Criterion 1.4.1 | W3C Understanding WCAG 2.0

Specifications

Specification
HTML Standard # selector-valid
Selectors Level 4 # validity-pseudos

See also

Other validation-related pseudo-classes: :required, :optional, :invalid Form data validation Accessing the validity state from JavaScript

Last modified: Jan 3, 2022, by MDN contributors

Select your preferred language English (US)EspañolFrançais日本語Português (do Brasil)Русский中文 (简体) Change language

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Categories in CSS
css
Search CSS
Search CSS by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote

"Olivia, my eldest daughter, caught measles when she was seven years old. As the illness took its usual course I can remember reading to her often in bed and not feeling particularly alarmed about it. Then one morning, when she was well on the road to recovery, I was sitting on her bed showing her how to fashion little animals out of coloured pipe-cleaners, and when it came to her turn to make one herself, I noticed that her fingers and her mind were not working together and she couldn’t do anything. 'Are you feeling all right?' I asked her. 'I feel all sleepy,' she said. In an hour, she was unconscious. In twelve hours she was dead. The measles had turned into a terrible thing called measles encephalitis and there was nothing the doctors could do to save her. That was...in 1962, but even now, if a child with measles happens to develop the same deadly reaction from measles as Olivia did, there would still be nothing the doctors could do to help her. On the other hand, there is today something that parents can do to make sure that this sort of tragedy does not happen to a child of theirs. They can insist that their child is immunised against measles. ...I dedicated two of my books to Olivia, the first was ‘James and the Giant Peach’. That was when she was still alive. The second was ‘The BFG’, dedicated to her memory after she had died from measles. You will see her name at the beginning of each of these books. And I know how happy she would be if only she could know that her death had helped to save a good deal of illness and death among other children."

I just checked google books for BFG, and the dedication is there. 

https://www.google.com.au/books/edition/_/quybcXrFhCIC?hl=en&gbpv=1 


Roald Dahl, 1986
Random CSS Property

paint()

Experimental: This is an experimental technologyCheck the Browser compatibility table carefully before using this in production.
paint() css reference