The out-of-range selector (for styling when a value is not correct)

Have you always wanted to dynamically check the values entered by your users in a form field without using JavaScript? Here's how to change the style of a field when a number entered by the user is invalid!
•
Updated on August 14, 2026
•
2 min read
Believemy logo

Have you always wanted to dynamically validate the values entered by your users in a form field without using JavaScript? Here's how to change the style of a field when a number entered by the user is invalid!

 

Step 1: HTML

Let's start by creating a very classic form field, which has a number type:

HTML
<input type="number" min="0" max="100" placeholder="Discount Percentage" />

We will use the example of a user who must enter a discount percentage for a product. We do not accept negative percentages (that would be very strange, wouldn't it?) and percentages above 100: after all, we are not going to give money to our buyers when they purchase a product.

 

Step 2: CSS

Let's start by styling our form field very simply:

CSS
input {
  width: 300px;
  padding: 15px;
  border-radius: 5px;
  border: 3px solid silver;
  outline: none;
}

Nothing new and everything is rather simple to understand: our input will be 300px wide with a 3px wide border.

 

Step 3: Detecting an Incorrect Value

We just need to use out-of-range to apply the style to the input element:

CSS
input:out-of-range {
  border-color: red;
  background: #ff758f;
}

And that's it! Try entering a value that is not valid.

 

Step 4: Optimization

The problem is that currently all our inputs receive this selector, and that's not very efficient... We can use out-of-range on fields that contain a minimum and a maximum number.

It's therefore very easy to specify that out-of-range should only be used on inputs with a type attribute that equals number.

CSS
input[type="number"]:out-of-range {
  border-color: red;
  background: #ff758f;
}

 

We now have very simple and well-thought-out code that works wonderfully well to change the style of our fields when a value is not correct.

Category: Developers

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.

Believemy logo
Comments (0)

You need to be logged in to comment on this article: log in or sign up.

Read next

Create a character counter in an input with JavaScript

Whether it's for choosing a nickname or for any other important data, you may need to limit the number of characters your users can enter in a form field. In this article, we'll look at how to do this simply and effectively.

December 9, 2024

Detecting the shift key on a form with JavaScript

Whether it's for your registration or login forms, or even on your password change pages, warning your users when they use the shift key on their keyboard will considerably improve their experience on your site. In this article, we'll show you how to do this very easily!

December 9, 2024

All about the new :is() and :where() selectors in CSS

Let's take a look at the latest selectors offered by CSS: where and is! These selectors will allow us to reduce our code and select our elements more flexibly.

December 9, 2024

Animating a checkbox with CSS

Animating the checkboxes in your forms can greatly improve the experience your users have and will undeniably increase their desire to fill them in. A form that's more fun to fill in is a form that your users will fill in more often. In this article, we'll look at how to animate a checkbox using CSS!

December 9, 2024

Creating a form with dynamic labels

The more modern a form, the more engaging it is. In this article, we'll look at how to create a form with dynamic labels that will increase the engagement of your forms and make your users want to fill them in.

December 9, 2024

Show / Hide on a password field

The login form is the most common form found on websites. Creating a login form that gives users the choice of displaying or hiding their password is therefore an element that will considerably improve the lives of your users. Let's find out how in this article!

December 9, 2024

Creating an "Old School" button with CSS

Let's find out how to create an original button that's right up to date!

December 9, 2024

Creating a glassmorphic connection form in HTML and CSS

In this article, we're going to create a form with a glassmorphic design. Modern effect guaranteed!

December 9, 2024

Become the solopreneur you dream of being.

First project, career change, business launch: access our courses, practice on real-world cases, and move forward with clear guidance.