CSS - Pseudo-class :out-of-range



The CSS pseudo-class selector :out-of-range represents an <input>element whose value is outside the range limited by the min and max attributes.

The selector :out-of-range is useful, as it gives a visual indication to the user about the current value of a field, that it is outside the permitted range of values.

The pseudo-class :out-of-range can only apply to the elements that can either have or take values in a range limitation. When there is no such limitation, the element is said to be neither in-range or out-of-range.

Syntax

:out-of-range {
   /* ... */ 
}

CSS :out-of-range Example

Here is an example of :out-of-range pseudo-class:

<html>
<head>
<style>
   input:in-range { 
      background-color: beige; 
      padding: 5px;
      border: 2px solid black;
   } 

   input[type="number"]:out-of-range {  
      border: 2px solid red;
      padding: 5px;
   }
   input:out-of-range + label::after {
      content: "Sorry! Value is out of specified range.";
      color: red;
      background-color: aqua ;
   }

   p {
      color: green;
   }
   </style>
   </head>
   <body>
      <h2>:out-of-range pseudo-class example</h2>
      <p>Enter a number in the range of 1 and 50.</p>
      <p> 
         <input type="number" min="1" max="50" value="1" id="inrange">
         <label for="inrange"></label>
      </p>
</body>
</html>
Advertisements