CSS Portal

:out-of-range CSS Pseudo Class

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The :out-of-range CSS pseudo-class is used to target form elements whose current value is outside the range specified by their input attributes, such as min and max. It is commonly applied to inputs of type number, range, date, and other types where a numerical or date-based range can be defined. This pseudo-class allows developers to style inputs that fail the range constraints, providing visual feedback to users without relying solely on scripting.

When an input element’s value is lower than the min attribute or higher than the max attribute, the element automatically matches :out-of-range. This can be particularly useful for creating forms that guide user input with subtle visual cues, such as changing border colors or displaying warning icons.

For example, you might want to highlight a number input in red when the user enters a value that is out of the permitted range:

<input type="number" min="1" max="10" value="15" />
input:out-of-range {
    border: 2px solid red;
    background-color: #ffe6e6;
}

In this example, the input’s value is 15, which exceeds the maximum of 10, so the :out-of-range styling is applied. Conversely, when the value is within the specified range, it does not match this pseudo-class.

It is also worth noting that :out-of-range can be combined with other pseudo-classes for more nuanced feedback. For instance, combining it with :invalid can distinguish between inputs that are syntactically invalid versus those that are semantically out of the defined range. Additionally, properties like border-color, background-color, or color are commonly used to style elements affected by this pseudo-class.

Overall, :out-of-range is a valuable tool for enhancing form usability by providing immediate visual feedback based on value constraints, improving both accessibility and user experience.

Syntax

:out-of-range {
  /* ... */
}

Example

<div class="container">
<label for="age-input">Enter your age (18 - 99):</label>
<input
type="number"
id="age-input"
name="age"
min="18"
max="99"
value="25"
>
<p class="error-message">Value must be between 18 and 99.</p>
</div>
/* Style the input when the value is valid */
input {
border: 2px solid #ccc;
padding: 8px;
border-radius: 4px;
outline: none;
}

/* Style the input when the value is outside min/max */
input:out-of-range {
border-color: #ff4d4d;
background-color: #fff2f2;
color: #d93025;
}

/* Hide error message by default */
.error-message {
display: none;
color: #d93025;
font-size: 0.8rem;
margin-top: 5px;
}

/* Show the error message only when input is out of range */
input:out-of-range + .error-message {
display: block;
}

Browser Support

The following information will show you the current browser support for the CSS :out-of-range pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS psuedo class.

This psuedo class is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 31st December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!