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:
Copied to clipboard! Playground
<!-- π 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" />
Copied to clipboard! Playground
<!-- π 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" />
Make sure you always give feedback to your users about the action they take.
Resources:
π More Webtips
Rocket Launch Your Career
Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies:
Courses
CSS - The Complete Guide (including Flexbox, Grid and Sass)
Including Flexbox, Grid, and Sass
Whether you're learning CSS for the first time or brushing up on your CSS skills and diving even deeper, this course is for you. Every web developer has to know CSS.
The HTML & CSS Bootcamp
From Zero to Expert!
This course covers flexbox, CSS grid, animations, responsive design, and much more! You will find tons of exercises & projects inside this course.
The Creative HTML5 & CSS3 Course
Build Awesome Websites
Learn HTML5 and CSS3 by creating three amazing, well-designed and animated websites from scratch.