CSS 基础(015_Combinators)

原始网址:http://www.w3schools.com/css/css_combinators.asp

翻译:

CSS Combinators


CSS Combinators

combinator 用以解释选择器之间的关系。

CSS 选择器可以包含多个简单选择器,在简单选择器之间,我们可以包含一个 combinator 。
在 CSS3 中,有以下 4 种 combinators:

  • 后代选择器(space)
  • 子选择器(>)
  • 相邻兄弟选择器(+)
  • 通用兄弟选择器(~)

Descendant Selector

后代选择器匹配指定元素的所有后代元素。
下列示例为选择 <div> 元素内的所有 <p> 元素:
Descendant Selector

<!DOCTYPE html>
<html>
    <head>
        <style>
            div p {
                background-color: yellow;
            }
        </style>
    </head>
    <body>
        <div>
            <p>Paragraph 1 in the div.</p>
            <p>Paragraph 2 in the div.</p>
            <span><p>Paragraph 3 in the div.</p></span>
        </div>
        <p>Paragraph 4. Not in a div.</p>
        <p>Paragraph 5. Not in a div.</p>
    </body>
</html>

Child Selector

子选择器选择指定元素的所有直接子元素。
以下示例为选择 <div> 元素内的所有直接 <p> 元素:
Child Selector

<!DOCTYPE html>
<html>
    <head>
        <style>
            div > p {
                background-color: yellow;
            }
        </style>
    </head>
    <body>
        <div>
            <p>Paragraph 1 in the div.</p>
            <p>Paragraph 2 in the div.</p>
            <span><p>Paragraph 3 in the div.</p></span>
            <!-- not Child but Descendant -->
        </div>
        <p>Paragraph 4. Not in a div.</p>
        <p>Paragraph 5. Not in a div.</p>
    </body>
</html>

Adjacent Sibling Selector

相邻兄弟选择器选择指定元素的所有相邻兄弟元素。
兄弟元素必须有相同的父元素,并且“相邻”的意思是“如影相随”。
以下示例为选择紧随 <div> 元素之后的所有 <p> 元素:
Adjacent Sibling Selector

<!DOCTYPE html>
<html>
    <head>
        <style>
            div + p {
                background-color: yellow;
            }
        </style>
    </head>
    <body>
        <div>
            <p>Paragraph 1 in the div.</p>
            <p>Paragraph 2 in the div.</p>
        </div>
        <p>Paragraph 3. Not in a div.</p>
        <p>Paragraph 4. Not in a div.</p>
    </body>
</html>

General Sibling Selector

通用兄弟选择器选择指定元素的所有兄弟元素。
以下示例为选择 <div> 元素的所有兄弟 <p> 元素:
General Sibling Selector

<!DOCTYPE html>
<html>
    <head>
        <style>
            div ~ p {
                background-color: yellow;
            }
        </style>
    </head>
    <body>
        <div>
            <p>Paragraph 1 in the div.</p>
            <p>Paragraph 2 in the div.</p>
        </div>
        <p>Paragraph 3. Not in a div.</p>
        <p>Paragraph 4. Not in a div.</p>
    </body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值