CSS中的+是什么意思? [英] What does + mean in CSS?
问题描述
这个CSS规则中的 +
是什么意思?
What does the +
in this CSS rule mean?
h2 + p {
font-size: 1.4em;
font-weight: bold;
color: #777;
}
推荐答案
+
是邻近同胞组合器 。
这意味着选择器 h2 + p
只选择 p
之后 。
That means the selector h2 + p
only selects the p
that comes immediately after an h2
.
p>
An illustration:
<h2>Headline!</h2>
<p>The first paragraph.</p> <!-- Selected [1] -->
<p>The second paragraph.</p> <!-- Not selected [2] -->
<h2>Another headline!</h2>
<blockquote>
<p>A quotation.</p> <!-- Not selected [3] -->
</blockquote>
选择什么和不是什么:
-
已选择
此p
紧随第一个h2
Selected
Thisp
comes right after the firsth2
.
未选择
此 p
发生在第一个 p
之后,而不是 h2
。因为它不会立即跟随 h2
,所以没有选择。
Not selected
This p
occurs after the first p
as opposed to the h2
. Since it doesn't immediately follow the h2
, it's not selected.
但是, code> h2 元素,只是不是立即,选择器 h2 + p
将不匹配此元素,但 h2〜p
将使用一般兄弟姐妹
However, since it still follows the h2
element, just not immediately, the selector h2 + p
won't match this element, but h2 ~ p
will, using the general sibling combinator instead.
未选择
此 p
位于 blockquote
中,并且在引号内部没有 h2
Not selected
This p
is located within a blockquote
, and there's no h2
before it inside the quote to satisfy its selector.
这篇关于CSS中的+是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!