How to Style Invalid Inputs in CSS

You can style input elements with CSS based on their validity with the in-range and out-of-range pseudo-class selectors:

<!-- ๐Ÿ“— This will make the background of the input green -->
<style>
    input:in-range {
        background-color: green;
    }
</style>

<input type="number" min="1" max="10" value="5" />
in-range.html
Copied to clipboard!
<!-- ๐Ÿ“• This will make the background of the input red -->
<style>
    input:out-of-range {
        background-color: red;
        border: 2px solid red;
    }
</style>

<input type="number" min="1" max="10" value="12" />
out-of-range.html
Copied to clipboard!

Make sure you always give feedback to your users about the action they take.

How to keep image aspect ratio in CSS
If you would like to see more Webtips, follow @flowforfrank

10 Best Practices for Quickly Improving Your CSS

Resources:

Remove ads
Remove ads

๐Ÿ“š Get access to exclusive content

Want to get access to exclusive content? Support webtips with the price of a coffee to get access to tips, checklists, cheatsheets, and much more. โ˜•

Get access Support us
Remove ads Read more on
Remove ads
Remove ads
๐ŸŽ‰ Thank you for subscribing to our newsletter. x This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.