Change colours when required fields complete

Is there a way to change the form background and input background and input text colours once all the Required fields are complete. even though not all of the fields are completed and before the submit, all with CSS? Regards Pete
18 Replies
Simon
Simon7d ago
Halfway there https://jsfiddle.net/Lyp5j4sz/1/ Seems like the browser(chrome) does not handle the valid/invalid conditions correctly on page load – not sure if there is a way around that "neutral state" in CSS 🫠 sorry, I dont have time for more right now
ἔρως
ἔρως7d ago
actually, you are using :user-valid and :user-invalid as you should be the goal of those is to let the user interact first with the elements then, after the first interaction/submit, it will style as valid or invalid so, when the page loads, and since the user didnt interact, the styles arent applied: as it should be if you want it to show as invalid on refresh ... please dont
Chris Bolson
Chris Bolson7d ago
You could try this:
form:not(:has(:required:invalid)) {
background: pink;
}
form:not(:has(:required:invalid)) {
background: pink;
}
ἔρως
ἔρως7d ago
but please, dont do that
Simon
Simon7d ago
Honestly, at some point I shuffled :not and :has around like a maniac 🤣
ἔρως
ἔρως7d ago
there is a good reason to do use :user-invalid
Simon
Simon7d ago
But the styles get applied on load, when I look at the fiddle. What is your solution to prevent the default invalid and/or user-invalid visual? perhaps focus-within could help ...
ἔρως
ἔρως7d ago
no :user-invalid with that, it is supposed to only show the invalid status when the user interacts
Simon
Simon7d ago
I think we have a misunderstanding. But I'd love to see a corrected version using user-invalid which I am already using 🌭 could be handy someday
ἔρως
ἔρως7d ago
you are using the correct one then
Simon
Simon7d ago
The issue is, that the form is pink and yellow on load because the pseudo apparently does not prevent the conditions from taking effect, or the CSS in the jsfiddle is faulty. In the optimal case the form is colorless and only highlighted when the conditions, that were demanded by the OP, meet due to the users interaction.
Chris Bolson
Chris Bolson7d ago
You can't use user-valid (or user-invalid) for this as, by their very nature, those selectors require the user to interact with the fields which they won't have done on page load. If I understood the initial question correctly, the OP wants something like this
Simon
Simon7d ago
There we go
Londonpeteharrison
Chris, your solution works they way I was looking for. ἔρως solution focuses on the placeholder. Fields might not have a placeholder.
MarkBoots
MarkBoots6d ago
maybe i'm missing something, but wouldn't the selector form:valid be sufficient here?
Chris Bolson
Chris Bolson6d ago
you are right 🤦‍♂️
MarkBoots
MarkBoots6d ago
it happens, sometimes im way overthinking pseudo selectors too, while form has its on valid state
clevermissfox
clevermissfox6d ago
you can always use a single space as a placeholder so it doesnt visually show one but you can still take advantage of the :placeholder-shown pseudo class

Did you find this page helpful?