:in-range
/* Selects any <input>, but only when it has a range */
/* specified, and its value is inside that range */
input:in-range {
background-color: rgba(0, 255, 0, 0.25);
}
注意:这个伪类仅适用于具有范围限制的元素。在没有这种限制的情况下,元素既不能在“范围内”,也不能在“范围之外”。
语法
:in-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>
</ul>
</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!';
}
规范
Specification | Status | Comment |
---|---|---|
HTML Living StandardThe definition of ':in-range' in that specification. | Living Standard | Defines when :in-range matches elements in HTML. |
Selectors Level 4The definition of ':in-range' in that specification. | Working Draft | Initial specification. |
浏览器兼容性
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Support on <input> | 10.01 | (Yes) | 29.0 (29.0)2 | No support | 11.01 | 5.23 |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Support on <input> | 2.31 | (Yes) | 16.0 (16.0)2 | No support | (Yes)1 | (Yes)3 |
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com