Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Style elements with a value outside a specified range with CSS
The CSS :out-of-range pseudo-class selector is used to style <input> elements when their value is outside the specified min and max range. This selector only applies to input elements that have range limitations defined.
Syntax
input:out-of-range {
/* styles */
}
Example: Styling Out-of-Range Input
The following example demonstrates how to style a number input when the value is outside the specified range −
<!DOCTYPE html>
<html>
<head>
<style>
input:out-of-range {
border: 3px dashed orange;
background-color: yellow;
color: red;
}
input:in-range {
border: 2px solid green;
background-color: lightgreen;
}
.container {
padding: 20px;
font-family: Arial, sans-serif;
}
label {
display: block;
margin: 10px 0 5px;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<label for="number-input">Enter a number (5-10):</label>
<input type="number" id="number-input" min="5" max="10" value="15">
<p>The input has an orange dashed border and yellow background when the value is out of range (less than 5 or greater than 10).</p>
<label for="range-input">Select a value (20-80):</label>
<input type="range" id="range-input" min="20" max="80" value="100">
<p>Range inputs also support the :out-of-range selector.</p>
</div>
</body>
</html>
A styled form appears with: - A number input with value 15 (out of range 5-10) showing orange dashed border and yellow background - A range slider with maximum value that exceeds the allowed range - When valid values are entered, inputs show green styling
Applicable Input Types
| Input Type | Description |
|---|---|
number |
Numeric inputs with min/max attributes |
range |
Range sliders with defined limits |
date |
Date inputs with min/max date constraints |
time |
Time inputs with time range limits |
Conclusion
The :out-of-range selector provides an effective way to visually indicate when user input exceeds defined boundaries. Combine it with :in-range for comprehensive validation styling.
Advertisements
