CSS

:focus

:focus

:focusCSS伪类表示已经接收焦点的元素(如窗体输入)。当用户点击或点击某个元素或通过键盘的tab键选择该元素时,通常会触发它。

/* Selects any <input> element only when focused */ input:focus { color: red; }

这个伪类只应用于聚焦元素本身,而不包括它的父元素,如同:checked:enabled,但与:active:hover不同。

语法

:focus

实例

HTML

<input class="red-input" value="I'll be red when focused."> <input class="lime-input" value="I'll be lime when focused.">

CSS

.red-input:focus { color: red; } .lime-input:focus { color: lime; }

结果

规范

SpecificationStatusComment
HTML Living StandardThe definition of ':focus' in that specification.Living StandardDefines HTML-specific semantics.
Selectors Level 4The definition of ':focus' in that specification.Working DraftNo change.
Selectors Level 3The definition of ':focus' in that specification.RecommendationNo change.
CSS Level 2 (Revision 1)The definition of ':focus' in that specification.RecommendationInitial definition.

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support1.0(Yes)1.0 (1.7 or earlier)8.07.01.0

FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support1.0(Yes)1.0 (1)8.06.01.0