在线文档教程
CSS

:focus-within

:focus-within

:focus-withinCSS伪类匹配任何:focus伪类所匹配的元素或后代中有:focus伪类所能匹配的节点的元素。(这包括shadow trees中的后代)

/* Selects any <div> when one of its descendants is focused */ div:focus-within { background: yellow; }

举个例子来说,当用户关注于其表单输入之一时,这对于突出显示整个表单容器是有用的。

语法

:focus-within

实例

.name-container { padding: 4px; } .name-container:focus-within { background: yellow; } input { margin: 4px; }

<p>The div below will have a yellow background if either input inside is focused.</p> <div class="name-container"> <label for="given_name"> Given Name: <input id="given_name" type="text"> </label> <label for="family_name"> Family Name: <input id="family_name" type="text"> </label> </div>

规范

SpecificationStatusComment
Selectors Level 4The definition of ':focus-within' in that specification.Working DraftInitial definition.

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support601No support252.0 (52.0)3No support4710.1

FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support60152.0 (52.0)3No support4710.3