aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/_colon_focus-within/index.html
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/css/_colon_focus-within/index.html
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/css/_colon_focus-within/index.html')
-rw-r--r--files/zh-cn/web/css/_colon_focus-within/index.html97
1 files changed, 97 insertions, 0 deletions
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'
+---
+<div>{{CSSRef}}</div>
+
+<p><strong><code>:focus-within</code></strong> 是一个<a href="/zh-CN/docs/Web/CSS">CSS</a> <a href="/zh-CN/docs/Web/CSS/Pseudo-classes">伪类</a> ,表示一个元素获得焦点,或,该元素的后代元素获得焦点。换句话说,元素自身或者它的某个后代匹配 {{CSSxRef(":focus")}} 伪类。(<a href="/zh-CN/docs/Web/Web_Components/Shadow_DOM">shadow DOM 树</a>中的后代也包括在内)</p>
+
+<pre class="brush: css no-line-numbers">/* 当 &lt;div&gt; 的某个后代获得焦点时,匹配 &lt;div&gt; */
+div:focus-within {
+ background: cyan;
+}</pre>
+
+<p>该选择器非常实用。举个通俗的例子:表单中的某个 {{htmlElement("input")}} 字段获得焦点时,整个表单的 {{HTMLElement("form")}} 元素都可被高亮。</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox">{{csssyntax}}</pre>
+
+<h2 id="Example">Example</h2>
+
+<p>此例子中,当表单某个文本输入框获得焦点后,表单会被设置颜色样式。</p>
+
+<h3 id="HTML">HTML</h3>
+
+<pre class="brush: html">&lt;p&gt;试试在这个表单中输入点什么。&lt;/p&gt;
+
+&lt;form&gt;
+ &lt;label for="given_name"&gt;Given Name:&lt;/label&gt;
+ &lt;input id="given_name" type="text"&gt;
+ &lt;br&gt;
+ &lt;label for="family_name"&gt;Family Name:&lt;/label&gt;
+ &lt;input id="family_name" type="text"&gt;
+&lt;/form&gt;</pre>
+
+<h3 id="CSS">CSS</h3>
+
+<pre class="brush: css">form {
+ border: 1px solid;
+ color: gray;
+ padding: 4px;
+}
+
+form:focus-within {
+ background: #ff8;
+ color: black;
+}
+
+input {
+ margin: 4px;
+}
+</pre>
+
+<h3 id="结果">结果</h3>
+
+<p>{{EmbedLiveSample("Example", 500, 150)}}</p>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">规范</th>
+ <th scope="col">状态</th>
+ <th scope="col">备注</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName("CSS4 Selectors", "#the-focus-within-pseudo", ":focus-within")}}</td>
+ <td>{{Spec2("CSS4 Selectors")}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("css.selectors.focus-within")}}</p>
+
+<h2 id="参见">参见</h2>
+
+<ul>
+ <li>{{CSSxRef(":focus")}}</li>
+ <li>{{CSSxRef(":focus-visible")}} {{Experimental_Inline}}</li>
+</ul>