css组合选择器_CSS组合器

css组合选择器

Combinators in CSS are used to explain a relationship between two selectors. A selector can be simple to complex and we can combine any two selectors through combinator.

CSS中的组合器用于解释两个选择器之间的关系。 选择器可以简单到复杂,我们可以通过组合器组合任意两个选择器。

There are four different combinators in CSS,

CSS中有四种不同的组合器

  1. General sibling selector (~)

    通用同级选择器( )

  2. Adjacent sibling selector (+)

    相邻的同级选择器( + )

  3. Child selector (>)

    子选择器( > )

  4. Descendant selector (space)

    后代选择器( 空格 )

Let us understand these combinators one by one...

让我们一一理解这些组合器...

1)通用同级选择器(〜) (1) General sibling selector (~))

It is denoted by ~. The general sibling selector as its name suggests is useful to select the elements that are the sibling to a specified element. This can also be used to select the elements who share the same parent element.

用〜表示。 顾名思义,通用的同级选择器对于选择指定元素的同级元素很有用。 这也可以用于选择共享相同父元素的元素。

Syntax:

句法:

    element1 ~ element2{
        //CSS styling
    }

Example:

例:

<!DOCTYPE html>

<html>

<head>
    <style>
        div ~ p {
            font-weight: bold;
            background-color: #f40;
            color: #fff;
            font-size: 32px;
        }
    </style>
</head>

<body>
    <p>A</p>
    <div>
        <p>B</p>
    </div>
    <p>C</p>
    <p>D</p>
</body>

</html>

Output

输出量

CSS Combinators Example 1

In the above example, two <p> are selected i.e. "C" and "D" because they are a sibling to specified element <div>.

在上面的示例中,选择了两个<p> ,即“ C”“ D”,因为它们是指定元素<div>的同级。

2)相邻的兄弟选择器(+) (2) Adjacent sibling selector (+))

It is denoted by +. As its name suggests adjacent sibling selector is used to selecting the element that is adjacent to a specified element. This combinator only selects one element that is next to a specified tag.

用+表示。 顾名思义,相邻兄弟选择器用于选择与指定元素相邻的元素。 该组合器仅选择一个位于指定标签旁边的元素。

Syntax:

句法:

    element1 + element2{
        //CSS styling
    }

Example:

例:

<!DOCTYPE html>

<html>

<head>
    <style>
        div + p {
            font-weight: bold;
            background-color: #f40;
            color: #fff;
            font-size: 32px;
        }
    </style>
</head>

<body>
    <p>A</p>
    <div>
        <p>B</p>
    </div>
    <p>C</p>
    <p>D</p>
</body>

</html>

Output

输出量

Output

输出量

CSS Combinators Example 2

In the above example, the <p> i.e. "C" is selected as it is a sibling to the specified element <div>.

在上面的示例中,选择<p>即“ C” ,因为它是指定元素<div>的同级。

3)子选择器(>) (3) Child selector (>))

It is denoted by >. The child selector selects all the elements that are children of a specified element. It can select more than one element.

用>表示。 子选择器选择所有属于指定元素的子元素。 它可以选择多个元素。

Syntax:

句法:

    element1 > element2{
        //CSS styling
    }

Example:

例:

<!DOCTYPE html>

<html>

<head>
    <style>
        div > p {
            font-weight: bold;
            background-color: #f40;
            color: #fff;
            font-size: 32px;
        }
    </style>
</head>

<body>
    <p>A</p>
    <div>
        <p>B</p>
    </div>
    <p>C</p>
    <p>D</p>
</body>

</html>

Output

输出量

CSS Combinators Example 3

In the above example <p> element selected as it is a child of the specified element <div>.

在上面的示例中,选择的<p>元素是指定元素<div>的子元素。

4)后代选择器(空格) (4) Descendant selector (space))

It is denoted by space between two elements. The descendant selector selects all the elements that are descendant to a specified element. This combinator is useful to combine the two selectors such that selected elements have an ancestor the same as the first selector element.

两个元素之间的间距表示。 后代选择器选择属于指定元素的所有后代元素。 该组合器可用于组合两个选择器,以使所选元素的祖先与第一个选择器元素相同。

Syntax:

句法:

    element1  element2 {
        //CSS styling
    }

Example:

例:

<!DOCTYPE html>

<html>

<head>
    <style>
        div p {
            font-weight: bold;
            background-color: #f40;
            color: #fff;
            font-size: 32px;
        }
    </style>
</head>

<body>
    <p>A</p>
    <div>
        <p>B</p>
    </div>
    <p>C</p>
    <p>D</p>
</body>

</html>

Output

输出量

CSS Combinators Example 4

In the above example <p> is selected because it is a descendant of specified element <div>.

在上面的示例中,选择<p>是因为它是指定元素<div>的后代。

翻译自: https://www.includehelp.com/code-snippets/css-combinators.aspx

css组合选择器

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值