后代选择器和子代选择器的区别在于匹配的层级。
| 写法 | 匹配范围 |
|---|---|
A B | A 内任何层级的 B |
A > B | A 的直接子元素 B |
<div class="note">
<p>第一段</p>
<section><p>第二段</p></section>
</div>
html .note p { color: gray; } /* 两段都匹配 */
.note > p { font-weight: bold; } /* 只匹配第一段 */
css 后代选择器和子代选择器的区别在于匹配的层级。
| 写法 | 匹配范围 |
|---|---|
A B | A 内任何层级的 B |
A > B | A 的直接子元素 B |
<div class="note">
<p>第一段</p>
<section><p>第二段</p></section>
</div>
html .note p { color: gray; } /* 两段都匹配 */
.note > p { font-weight: bold; } /* 只匹配第一段 */
css