
CSS Topics
- CSS Intro
- CSS How To
- CSS Editors
- CSS Properties
- CSS Selectors
- .class
- .class1.class2
- .class1 .class2
- #id
- * (all)
- element
- element.class
- element,element
- element element
- element>element
- element+element
- element1~element2
- [attribute]
- [attribute=value]
- [attribute~=value]
- [attribute|=value]
- [attribute^=value]
- [attribute$=value]
- [attribute*=value]
- :active
- ::after
- ::before
- :checked
- :default
- :disabled
- :empty
- :enabled
- :first-child
- ::first-letter
- ::first-line
- :first-of-type
- :focus
- :fullscreen
- :has()
- :hover
- :in-range
- :indeterminate
- :invalid
- :lang()
- :last-child
- :last-of-type
- :link
- ::marker
- :not()
- :nth-child()
- :nth-last-child()
- :nth-last-of-type()
- :nth-of-type()
- :only-of-type
- :only-child
- :optional
- :out-of-range
- ::placeholder
- :read-only
- :read-write
- :required
- :root
- ::selection
- :target
- :valid
- :visited
- CSS Comments
- CSS Length
- CSS Image Sprites
- CSS Grid Layout
- CSS Grid Flexbox
- CSS @charset Rule
- CSS @font-face Rule
- CSS @import Rule
- CSS @keyframes Rule
- CSS @media Rule
CSS :out-of-range Selector
Photo Credit to CodeToFun
🙋 Introduction
The CSS :out-of-range selector is used to select <input> elements whose values are outside the range defined by their min and max attributes.
This selector allows you to apply custom styles to inputs when users enter values that don't meet the specified constraints, helping to create better form validation experiences.
💡 Syntax
The signature of the :out-of-range Selector is as follows:
input:out-of-range {
/* CSS properties */
}The :out-of-range pseudo-class applies to form elements like <input> that have defined ranges, typically using the min and max attributes.
📝 Example
Here is an example of how to use the :out-of-range selector in CSS:
☠️ HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS :out-of-range Selector Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<form>
<label for="age">Enter your age (between 18 and 60):</label>
<input type="number" id="age" name="age" min="18" max="60">
</form>
</body>
</html>🎨 CSS
/* Style for inputs that are within the range */
input:in-range {
border: 2px solid green;
}
/* Style for inputs that are out of range */
input:out-of-range {
border: 2px solid red;
background-color: #ffe0e0;
}In this example:
- When a valid number between 18 and 60 is entered, the input gets a green border.
- When the number entered is outside the range (less than 18 or greater than 60), the input gets a red border and a light red background.
💬 Usage Tips
- The
:out-of-rangeselector is most commonly used with<input type="number">or<input type="range">, where theminandmaxattributes define the valid range. - Combine
:out-of-rangewith other pseudo-classes like:in-rangefor complete styling of both valid and invalid input ranges. - You can also use
:out-of-rangeon date inputs withminandmaxattributes to enforce a date range.
⚠️ Common Pitfalls
- The
:out-of-rangeselector only works with form elements that support theminandmaxattributes. Using it with other input types that don't have a range won't produce any effect. - Be cautious when relying solely on visual feedback from the
:out-of-rangeselector. Ensure proper server-side validation to handle edge cases or unsupported browsers. - Some older browsers may not support this selector, so test across multiple browsers to ensure consistent styling.
🎉 Conclusion
The :out-of-range selector in CSS is an excellent tool for improving user interface feedback in forms that require range validation.
By using this pseudo-class, you can highlight out-of-range inputs and guide users to provide the correct data, ensuring a smoother and more intuitive form experience.
👨💻 Join our Community:
Author

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee












If you have any doubts regarding this article (CSS :out-of-range Selector), please comment here. I will help you immediately.