首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

:focus-within

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

代码语言:javascript
复制
/* Selects any <div> when one of its descendants is focused */
div:focus-within {
  background: yellow;
}

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

语法

代码语言:javascript
复制
:focus-within

实例

代码语言:javascript
复制
.name-container { 
  padding: 4px; 
}

.name-container:focus-within {
  background: yellow;
}

input { 
  margin: 4px; 
}
代码语言:javascript
复制
<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>

规范

Specification

Status

Comment

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

Working Draft

Initial definition.

浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

601

No support2

52.0 (52.0)3

No support

47

10.1

Feature

Android

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Basic support

601

52.0 (52.0)3

No support

47

10.3

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com