CSS :out-of-range Selector

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

What You’ll Learn

The :out-of-range pseudo-class styles form inputs whose value falls outside the allowed min and max range. It gives users instant visual feedback when they enter an unacceptable value.

01

Invalid range

Outside min–max.

02

number

Age, quantity.

03

date

Booking windows.

04

:in-range

Valid values.

05

Real-time

Live feedback.

06

Forms

No JavaScript.

Introduction

The CSS :out-of-range selector targets form elements whose current value falls outside the range defined by min and max attributes.

It is the counterpart to :in-range. When a user types a number below the minimum, above the maximum, or picks a date outside an allowed window, :out-of-range lets you highlight the problem immediately with red borders, backgrounds, or other error styling.

Definition and Usage

Use :out-of-range on its own or combined with an element selector: input:out-of-range or input[type="number"]:out-of-range. Styles apply while the current value violates the defined range constraints.

💡
Beginner Tip

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

📝 Syntax

The syntax for the :out-of-range pseudo-class is:

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

The :out-of-range pseudo-class targets any form control whose current value violates the constraints set by min and max.

Basic Example

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

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

Supported Input Types

Input typeRequiresOut-of-range example
numbermin and/or maxAge 17 or 61 when range is 18–60
rangemin and maxValue set programmatically outside bounds
datemin and/or maxDate before booking opens
timemin and/or maxTime outside business hours

Syntax Rules

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

Related Selectors

  • :in-range — matches values within min/max
  • :invalid — matches inputs that fail any validation constraint
  • :valid — matches inputs that pass all validation constraints
  • :required — matches fields that must be filled in
  • :focus — matches the currently focused input

⚡ Quick Reference

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

When to Use :out-of-range

:out-of-range is ideal for highlighting values that violate range rules:

  • Age or quantity fields — Show red when a number is too low or too high.
  • Date pickers — Flag dates outside a booking or event window.
  • Price or budget inputs — Warn when an entered amount exceeds limits.
  • Time slots — Highlight appointments outside allowed hours.
  • Progressive forms — Give instant error cues without writing JavaScript validation UI.

👀 Live Preview

The field below starts at 70 (outside 18–60). Change it to a valid age to see :in-range (green), or keep an invalid value for :out-of-range (red):

Try 25 for green, or 10 / 70 for red.

Examples Gallery

Practice :out-of-range with number inputs, error emphasis, date fields, and comparisons with :invalid.

📜 Core Patterns

Highlight out-of-range values with clear red error styling.

Example 1 — Basic :out-of-range styling

Style range-constrained inputs red when the value is outside allowed limits, green when valid.

out-of-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="70">
</form>
Try It Yourself

How It Works

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

Example 2 — Scoped number input errors

Target only number inputs and add a red glow on :out-of-range for stronger error emphasis.

out-of-range-age.css
input[type="number"]:out-of-range {
  border-color: #dc2626;
  background: #fef2f2;
  box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.2);
  color: #b91c1c;
}

input[type="number"]:in-range {
  border-color: #16a34a;
  background: #f0fdf4;
}
Try It Yourself

How It Works

Scoping to input[type="number"] avoids affecting date pickers or other inputs. The red box-shadow draws extra attention to the error state.

📄 Error Emphasis & Dates

Make out-of-range errors more noticeable and apply to date fields.

Example 3 — Pulsing error on :out-of-range:focus

Combine :out-of-range with :focus to intensify the error when the user is still editing an invalid value.

out-of-range-focus.css
input:out-of-range {
  border: 2px solid #dc2626;
  background: #fef2f2;
}

input:out-of-range:focus {
  outline: none;
  box-shadow: 0 0 0 4px rgba(220, 38, 38, 0.35);
}

input:in-range:focus {
  outline: none;
  box-shadow: 0 0 0 4px rgba(22, 163, 74, 0.25);
}
Try It Yourself

How It Works

When the user focuses an out-of-range input, the stronger box-shadow reinforces that the value needs correction before they move on.

Example 4 — Date outside booking window

Style date inputs red when the selected date falls outside an allowed range.

out-of-range-date.css
input[type="date"]:out-of-range {
  border: 2px solid #dc2626;
  color: #b91c1c;
  background: #fef2f2;
}

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

How It Works

With min and max set on a date input, :out-of-range flags dates outside the booking period. Valid dates inside the window match :in-range instead.

💬 Usage Tips

  • Pair with :in-range — Provide visual feedback for both valid and invalid range values.
  • Set min and max — Without these attributes, :out-of-range has no effect.
  • Real-time validation — Users see errors as they type, without waiting for form submit.
  • Scope by type — Use input[type="number"]:out-of-range to avoid styling unrelated inputs.
  • Combine with :focus — Intensify error styling while the user is still editing an invalid value.
  • 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 :invalid — :out-of-range only checks min/max, not pattern or required constraints.
  • Color-only feedback — Add text hints or labels so users who cannot see red/green still understand the error.

♿ Accessibility

  • Do not rely on color alone — Pair 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 :out-of-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

Value is outside bounds

If the value is below min or above max, :out-of-range matches.

Match
=

Instant error feedback

Users know immediately that their value needs correction.

🖥 Browser Compatibility

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

Baseline · Modern browsers

Range error styling everywhere

:out-of-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
:out-of-range pseudo-class 97% supported

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

🎉 Conclusion

The :out-of-range selector is an excellent tool for improving user interface feedback in forms that require range validation. By highlighting out-of-range inputs, you guide users toward acceptable values and create a smoother, more intuitive form experience.

When paired with :in-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 :out-of-range with :in-range
  • Include the allowed range in the label text
  • Validate on the server as well as in CSS
  • Add text hints alongside red error styling

❌ Don’t

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

Key Takeaways

Knowledge Unlocked

Five things to remember about :out-of-range

Use these points when styling range-invalid inputs.

5
Core concepts
# 02

min / max

Required attrs.

HTML
📅 03

number & date

Common types.

Inputs
:in 04

Pair it

Valid state.

Tip
🌐 05

97% support

All browsers.

Compat

❓ Frequently Asked Questions

The :out-of-range pseudo-class matches form controls whose current value falls outside the range defined by their min and max attributes. It lets you highlight invalid values with CSS as the user types.
:out-of-range applies to inputs with range constraints, including type="number", type="range", type="date", type="time", type="datetime-local", and type="month". The input must have min and/or max attributes set.
:out-of-range only checks whether the value is outside min/max. :invalid is broader and also covers empty required fields, pattern mismatches, and other validation rules. A field can be :out-of-range but still :valid in some edge cases, and vice versa.
Yes. Pair them for clear feedback — :in-range styles acceptable values (often green) and :out-of-range styles values outside the allowed range (often red).
:out-of-range is supported in all modern browsers including Chrome, Firefox, Safari, and Edge. Always validate on the server as a fallback.

Practice in the Live Editor

Open the HTML editor and experiment with :out-of-range, :in-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