CSS

必须选择器 | :required

:required

:requiredCSS伪类 表示 任意 <input>元素表示任意拥有required属性的 <input> 或 <textarea> 元素使用它. 它允许表单在提交之前容易的展示必填字段并且渲染其外观. 

/* Selects <input> elements, but only if they */ /* have got required set on them */ input:required { border: 1px dashed red; }

:optional伪类可用于表单可选元素提供外观.

语法

:required

示例

查看示例:invalid.

规范

SpecificationStatusComment
HTML Living StandardThe definition of ':required' in that specification.Living StandardNo change.
HTML5The definition of ':required' in that specification.RecommendationDefines the semantic regarding HTML and constraint validation.
Selectors Level 4The definition of ':required' in that specification.Working DraftNo change.
CSS Basic User Interface Module Level 3The definition of ':required' in that specification.Candidate RecommendationDefines the pseudo-class, but not the associated semantic.

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support10.0(Yes)4.0 (2)1010.05.0

FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support4.4.4(Yes)4.0 (2)No support10.05.0