
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 :in-range Selector

Photo Credit to CodeToFun
Introduction
The CSS :in-range
selector is used to select form elements whose values fall within a specified range.
This selector typically applies to input elements like <input type="number">
and <input type="range">
, where a min and max value has been defined. It allows you to style inputs that are within the valid range, providing users with clear feedback about the acceptability of their input.
Syntax
The signature of the :in-range
Selector is as follows:
:in-range {
/* CSS properties */
}
The :in-range
pseudo-class targets any form control whose current value is within the constraints set by the min
and max
attributes.
Example
Here is an example of how to use the :in-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 :in-range Selector Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<form>
<label for="age">Enter your age (18-60):</label>
<input type="number" id="age" name="age" min="18" max="60">
<label for="quantity">Select quantity (1-100):</label>
<input type="range" id="quantity" name="quantity" min="1" max="100">
</form>
</body>
</html>
CSS
/* Style for in-range inputs */
:in-range {
border: 2px solid green;
background-color: #e0ffe0;
}
/* Style for out-of-range inputs */
:out-of-range {
border: 2px solid red;
background-color: #ffe0e0;
}
In this example:
- Inputs that are within the defined range (like age between 18 and 60) are styled with a green border and a light green background.
- If the user inputs a value outside the valid range, the input will be styled with a red border and a light red background using the :out-of-range selector.
Usage Tips
- The
:in-range
selector works well when combined with the:out-of-range
selector to provide visual feedback on both valid and invalid inputs. - It only applies to input elements that have a
min
andmax
attribute. Without these attributes, the selector will not have any effect. - This selector is most useful for form validation, where real-time feedback about valid input is desired.
Common Pitfalls
- Not all browsers provide native support for input validation. Ensure that you have a fallback method for older browsers that do not fully support the
:in-range
pseudo-class. - The
:in-range
selector works only with numeric input types such asnumber
,range
,date
, andtime
. Other input types will not respond to this pseudo-class.
Conclusion
The :in-range
selector is a valuable tool for styling input elements based on their value range, improving user interaction and form validation. It provides a clear visual cue for users, helping them input data within the expected range. When paired with :out-of-range
, it ensures that form fields dynamically reflect valid and invalid input states, enhancing both functionality and accessibility.
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 :in-range Selector), please comment here. I will help you immediately.