回到:CSS 伪类(Pseudo-classes) | CSS教程
定义和使用
:required 选择器在表单元素是必填项时设置指定样式。
表单元素可以使用 required 属性来设置必填项。
注意: :required 选择器只适用于表单元素: input, select 和 textarea。
浏览器支持
表格中的数字表示支持该属性的第一个浏览器版本号。
| 选择器 | 谷歌 | IE | 火狐 | 苹果 | opera |
|---|---|---|---|---|---|
| :required | 4.0 | 9.0 | 3.5 | 3.2 | 9.6 |
CSS 语法
:required
{
css 声明;
}实例
如果 input 元素设置了 “required” 属性,设置其为黄色:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程(Web176.com)</title>
<style>
input:required
{
background-color: yellow;
}
</style>
</head>
<body>
<h3>A demonstration of the :required selector.</h3>
<p>An optional input element:<br><input></p>
<p>A required input element:<br><input required></p>
<p> :required选择器选择表单元素有"需要"属性.</p>
</body>
</html>作者:terry,如若转载,请注明出处:https://www.web176.com/cssprop/5407.html
支付宝
微信