CSS :in-range Selector

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 4 Examples
Form Validation

What You’ll Learn

The :in-range pseudo-class styles form inputs whose value falls within the allowed min and max range. It gives users instant visual feedback during data entry.

01

Valid range

Within min–max.

02

number

Age, quantity.

03

range

Slider inputs.

04

:out-of-range

Invalid values.

05

Real-time

Live feedback.

06

Forms

No JavaScript.

Introduction

The CSS :in-range selector is used to select form elements whose values fall within a specified range defined by min and max attributes.

It typically applies to <input type="number"> and <input type="range">, allowing you to style inputs that are within the valid range and give users clear feedback about acceptable values.

Definition and Usage

Use :in-range on its own or combined with an element selector: input:in-range or input[type="number"]:in-range. Styles apply while the current value is between the defined minimum and maximum.

💡
Beginner Tip

Always pair :in-range with :out-of-range so users see green for valid values and red for invalid ones. Both selectors require min and/or max on the input.

📝 Syntax

The syntax for the :in-range pseudo-class is:

syntax.css
:in-range {
  /* CSS properties */
}

The :in-range pseudo-class targets any form control whose current value is within the constraints set by min and max.

Basic Example

in-range.css
:in-range {
  border: 2px solid #16a34a;
  background-color: #dcfce7;
}

:out-of-range {
  border: 2px solid #dc2626;
  background-color: #fef2f2;
}
input:in-range input[type="number"]:in-range input[type="range"]:in-range :out-of-range

Supported Input Types

Input typeRequiresExample
numbermin and/or maxAge 18–60
rangemin and maxSlider 1–100
datemin and/or maxBooking dates
timemin and/or maxBusiness hours

Syntax Rules

  • min and/or max must be set on the input for range matching to work.
  • An empty input is neither :in-range nor :out-of-range in most browsers.
  • Combine with type selectors for targeted styling: input[type="range"]:in-range.
  • Pair with :out-of-range for complete valid/invalid feedback.
  • Do not rely on CSS alone — always validate on the server too.

Related Selectors

  • :out-of-range — matches values outside min/max
  • :valid — matches inputs that pass all validation constraints
  • :invalid — matches inputs that fail validation
  • :disabled — matches inactive form controls
  • :focus — matches the currently focused input

⚡ Quick Reference

QuestionAnswer
Selector typePseudo-class (UI state)
When it appliesValue is within min/max range
Required HTMLmin and/or max attributes
Best paired with:out-of-range
Common propertiesborder, background-color, box-shadow
Browser supportAll modern browsers

When to Use :in-range

:in-range is ideal for real-time form range feedback:

  • Age or quantity fields — Show green when a number falls within allowed limits.
  • Range sliders — Highlight sliders when the selected value is acceptable.
  • Date pickers — Style dates within a booking window.
  • Price filters — Indicate when a entered price is within min/max bounds.
  • Progressive forms — Give instant cues without writing JavaScript validation UI.

👀 Live Preview

Enter a value between 18 and 60 to see :in-range (green) or type outside the range for :out-of-range (red):

Examples Gallery

Practice :in-range with number inputs, range sliders, date fields, and paired out-of-range styling.

📜 Core Patterns

Style valid and invalid range values with paired pseudo-classes.

Example 1 — Basic :in-range and :out-of-range

Style all range-constrained inputs green when valid and red when out of range.

in-range-form.html
<style>
  :in-range {
    border: 2px solid #16a34a;
    background: #dcfce7;
  }
  :out-of-range {
    border: 2px solid #dc2626;
    background: #fef2f2;
  }
  input {
    padding: 0.5rem;
    border-radius: 0.4rem;
    width: 100%;
  }
</style>

<form>
  <label for="age">Age (18–60):</label>
  <input type="number" id="age" min="18" max="60" value="25">

  <label for="quantity">Quantity (1–100):</label>
  <input type="range" id="quantity" min="1" max="100" value="50">
</form>
Try It Yourself

How It Works

When the age is between 18 and 60, :in-range applies green styling. Values below 18 or above 60 trigger :out-of-range with red styling instead.

Example 2 — Age validation with number input

Target only number inputs with a scoped selector and add a subtle box-shadow on valid range.

in-range-age.css
input[type="number"]:in-range {
  border-color: #16a34a;
  background: #f0fdf4;
  box-shadow: 0 0 0 3px rgba(22, 163, 74, 0.2);
}

input[type="number"]:out-of-range {
  border-color: #dc2626;
  background: #fef2f2;
}
Try It Yourself

How It Works

Scoping to input[type="number"] avoids affecting range sliders or other inputs. The box-shadow adds an extra visual cue beyond border color.

📄 Sliders & Dates

Apply range styling to sliders and date pickers.

Example 3 — Range slider accent color

Style range inputs when the thumb value is within bounds using accent-color.

in-range-slider.css
input[type="range"]:in-range {
  accent-color: #16a34a;
}

input[type="range"]:out-of-range {
  accent-color: #dc2626;
}

input[type="range"] {
  width: 100%;
  margin: 0.5rem 0;
}
Try It Yourself

How It Works

accent-color tints the slider thumb and track in supporting browsers. Combined with :in-range, the slider turns green when the value is valid.

Example 4 — Date within booking window

Style date inputs when the selected date falls within an allowed range.

in-range-date.css
input[type="date"]:in-range {
  border: 2px solid #16a34a;
  color: #15803d;
}

input[type="date"]:out-of-range {
  border: 2px solid #dc2626;
  color: #b91c1c;
}
Try It Yourself

How It Works

With min and max set on a date input, :in-range highlights dates inside the booking period and :out-of-range flags invalid selections.

💬 Usage Tips

  • Pair with :out-of-range — Provide visual feedback for both valid and invalid range values.
  • Set min and max — Without these attributes, :in-range has no effect.
  • Real-time validation — Users see feedback as they type or drag, without waiting for form submit.
  • Scope by type — Use input[type="number"]:in-range to avoid styling unrelated inputs.
  • Server-side backup — CSS validation is a UX enhancement; always validate on the server.

⚠️ Common Pitfalls

  • Missing min/max — The selector only works when range constraints are defined in HTML.
  • Wrong input types — Text, email, and checkbox inputs do not support range-based pseudo-classes.
  • Empty fields — An empty input may not match either :in-range or :out-of-range; use :required and :invalid too.
  • CSS-only security — Never rely on styling alone to enforce business rules; validate server-side.
  • Confusing with :valid — :in-range only checks min/max, not pattern or required constraints.

♿ Accessibility

  • Do not rely on color alone — Pair green/red borders with clear label text explaining the allowed range.
  • Associate labels — Use <label for="..."> and include the range in the label: “Age (18–60)”.
  • aria-invalid — For screen readers, consider adding aria-invalid="true" via JavaScript when out of range.
  • Contrast — Ensure border and background colors meet WCAG contrast requirements.
  • Error messages — Provide text explanations alongside visual cues for users who cannot perceive color changes.

🧠 How :in-range Works

1

Input has min/max

HTML defines the allowed range with min and max attributes.

HTML
2

User enters a value

The browser compares the current value against the defined range.

Check
3

:in-range or :out-of-range matches

Valid values match :in-range; out-of-bounds values match :out-of-range.

Match
=

Instant visual feedback

Users know immediately if their value is acceptable.

🖥 Browser Compatibility

The :in-range pseudo-class is supported in all modern browsers.

Baseline · Modern browsers

Range validation styling everywhere

:in-range works in Chrome, Firefox, Safari, Edge, and Opera.

97% Global support
Google Chrome 10+ · Desktop & Mobile
Full support
Mozilla Firefox 29+ · Desktop & Mobile
Full support
Apple Safari 5.1+ · macOS & iOS
Full support
Microsoft Edge 12+
Full support
Opera 16+
Full support
:in-range pseudo-class 97% supported

Bottom line: Safe for modern form validation UI. Always validate on the server as a fallback.

🎉 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 helping users enter data within the expected limits.

When paired with :out-of-range, form fields dynamically reflect valid and invalid states. Combine with proper labels, server-side validation, and accessible error messages for the best experience.

💡 Best Practices

✅ Do

  • Always set min and max on range inputs
  • Pair :in-range with :out-of-range
  • Include the allowed range in the label text
  • Validate on the server as well as in CSS
  • Scope selectors by input type when needed

❌ Don’t

  • Rely on color alone for validation feedback
  • Assume CSS validation replaces server checks
  • Use :in-range on inputs without min/max
  • Confuse :in-range with :valid
  • Forget empty fields may match neither state

Key Takeaways

Knowledge Unlocked

Five things to remember about :in-range

Use these points when styling range-valid inputs.

5
Core concepts
# 02

min / max

Required attrs.

HTML
📊 03

number & range

Common types.

Inputs
:out 04

Pair it

Invalid state.

Tip
🌐 05

97% support

All browsers.

Compat

❓ Frequently Asked Questions

The :in-range pseudo-class matches form controls whose current value falls within the range defined by their min and max attributes. It provides visual feedback that the input value is acceptable.
:in-range applies to inputs with range constraints, including type="number", type="range", type="date", type="time", type="datetime-local", and type="month". It requires min and/or max attributes to be set.
:in-range checks only whether the value is between min and max. :valid is broader and also considers other validation rules like required or pattern. An empty required field can be :in-range but not :valid.
Yes. Pair them for clear green/red feedback — :in-range for acceptable values and :out-of-range for values outside the allowed range.
:in-range is supported in all modern browsers including Chrome, Firefox, Safari, and Edge. Always provide server-side validation as a fallback.

Practice in the Live Editor

Open the HTML editor and experiment with :in-range, :out-of-range, and form validation styling.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful