CSS

:out-of-range

:out-of-range

:out-of-range匹配如下元素:当一个元素的value属性值超出了这个元素的指定范围限制时,CSS伪类匹配该元素。它允许页面给出一个反馈,即当前使用该元素定义的值超出范围限制。如果值大于最大值或者小于最小值,则该值在限制范围之外。

/* Selects any <input>, but only when it has a range */ /* specified, and its value is outside that range */ input:out-of-range { background-color: rgba(255, 0, 0, 0.25 }

注意:这个伪类仅适用于具有(并且可能需要)范围限制的元素。在没有这种限制的情况下,元素既不能说是在“范围内”,也不能说在“范围之外”。

语法

:out-of-range

实例

HTML

<form action="" id="form1">     <ul>Values between 1 and 10 are valid.         <li>             <input id="value1" name="value1" type="number" placeholder="1 to 10" min="1" max="10" value="12">             <label for="value1">Your value is </label>         </li> </form>

CSS

li {     list-style: none;     margin-bottom: 1em; } input {     border: 1px solid black; } input:in-range {     background-color: rgba(0, 255, 0, 0.25 } input:out-of-range {     background-color: rgba(255, 0, 0, 0.25     border: 2px solid red; } input:in-range + label::after {     content:' OK'; } input:out-of-range + label::after {     content:'out of range!'; }

规范

SpecificationStatusComment
HTML Living StandardThe definition of ':out-of-range' in that specification.Living StandardDefines when :out-of-range matches elements in HTML.
Selectors Level 4The definition of ':out-of-range' in that specification.Working DraftInitial definition.

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Support on <input>10.0(Yes)29.0 (29.0)No support11.05.2

FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Support on <input>2.3(Yes)16.0 (16.0)No support(Yes)(Yes)