From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/css/_colon_focus-within/index.html | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 files/zh-cn/web/css/_colon_focus-within/index.html (limited to 'files/zh-cn/web/css/_colon_focus-within/index.html') diff --git a/files/zh-cn/web/css/_colon_focus-within/index.html b/files/zh-cn/web/css/_colon_focus-within/index.html new file mode 100644 index 0000000000..e638f478b5 --- /dev/null +++ b/files/zh-cn/web/css/_colon_focus-within/index.html @@ -0,0 +1,97 @@ +--- +title: ':focus-within' +slug: 'Web/CSS/:focus-within' +tags: + - ':focus-within' + - CSS + - Layout + - Pseudo-class + - 伪类 + - 参考 + - 选择器 +translation_of: 'Web/CSS/:focus-within' +--- +
{{CSSRef}}
+ +

:focus-within 是一个CSS 伪类 ,表示一个元素获得焦点,或,该元素的后代元素获得焦点。换句话说,元素自身或者它的某个后代匹配 {{CSSxRef(":focus")}} 伪类。(shadow DOM 树中的后代也包括在内)

+ +
/* 当 <div> 的某个后代获得焦点时,匹配 <div> */
+div:focus-within {
+  background: cyan;
+}
+ +

该选择器非常实用。举个通俗的例子:表单中的某个 {{htmlElement("input")}} 字段获得焦点时,整个表单的 {{HTMLElement("form")}} 元素都可被高亮。

+ +

语法

+ +
{{csssyntax}}
+ +

Example

+ +

此例子中,当表单某个文本输入框获得焦点后,表单会被设置颜色样式。

+ +

HTML

+ +
<p>试试在这个表单中输入点什么。</p>
+
+<form>
+  <label for="given_name">Given Name:</label>
+  <input id="given_name" type="text">
+  <br>
+  <label for="family_name">Family Name:</label>
+  <input id="family_name" type="text">
+</form>
+ +

CSS

+ +
form {
+  border: 1px solid;
+  color: gray;
+  padding: 4px;
+}
+
+form:focus-within {
+  background: #ff8;
+  color: black;
+}
+
+input {
+  margin: 4px;
+}
+
+ +

结果

+ +

{{EmbedLiveSample("Example", 500, 150)}}

+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName("CSS4 Selectors", "#the-focus-within-pseudo", ":focus-within")}}{{Spec2("CSS4 Selectors")}}Initial definition.
+ +

浏览器兼容性

+ + + +

{{Compat("css.selectors.focus-within")}}

+ +

参见

+ + -- cgit v1.2.3-54-g00ecf