例如:

div > p.some_class {
  /* Some declarations */
}

>号的确切含义是什么?

推荐答案

>child combinator,有时被错误地称为直系后代组合1

这意味着 Select 器div > p.some_class只匹配嵌套在directly insidediv中的.some_class的段落,而不匹配嵌套在其中的任何段落.这意味着匹配div > p.some_class的每个元素必然也匹配div p.some_class,与descendant combinator(空格)匹配,所以这两个元素经常混淆是可以理解的.

比较子组合子与子组合子的图示:

div > p.some_class { 
    background: yellow;
}

div p.some_class { 
    color: red;
}
<div>
    <p class="some_class">Some text here</p>     <!-- [1] div > p.some_class, div p.some_class -->
    <blockquote>
        <p class="some_class">More text here</p> <!-- [2] div p.some_class -->
    </blockquote>
</div>

哪些元素与哪些 Select 器匹配?

  1. Matched by both div > p.some_class and div p.some_class
    This p.some_class is located directly inside the div, hence a parent-child relationship is established between both elements. Since "child" is a type of "descendant", any child element is by definition also a descendant. Therefore, both rules are applied.

  2. Matched by only div p.some_class
    This p.some_class is contained by a blockquote within the div, rather than the div itself. Although this p.some_class is a descendant of the div, it's not a child; it's a grandchild. Therefore, only the rule with the descendant combinator in its selector is applied.


1 Many people go further to call it "direct child" or "immediate child", but that's completely unnecessary (and incredibly annoying to me), because a child element is immediate by definition anyway, so they mean the exact same thing. There's no such thing as an "indirect child".

Css相关问答推荐

如何使用基于嵌套子类的CSS邻近 Select 器?

在React Native中使用绝对位置对图像进行居中

我控制的自定义单选按钮无法重新 Select

沿浮动形状—面元素外路径将文字垂直居中""

如何在css中创建文件div效果

无法在 React 组件 (TypeScript) 中应用 Tailwind CSS 类

包含 N 个 React 组件的砌体网格

动态覆盖 Vuetify 类

如何在使用 bslib 5 的shiny 应用程序中使用样式参数排列 ui 元素

有没有办法在 CSS 字体速记中使用数字字体粗细?

如何使用 Sass to CSS 创建 H1 到 H6 标题并减小字体大小

semantic-ui-react 使输入使用全宽度可用

当我在几个月之间更改时,有什么方法可以在react-dates-range上添加淡入淡出或幻灯片效果?

如何从 表格单元格中创建链接

Div Size 自动调整内容大小

为什么在 CSS3 中启用硬件加速会降低性能?

位置固定宽度 100%

CSS - 使 div 水平对齐

CSS 中的星号属性是什么意思?

我如何*真正*证明 HTML+CSS 中的水平菜单?