在CSS中,
.a > .b
与.a>.b
是相同的,但这两种表示法哪一种更正确呢?我看在Chrome开发者工具中使用了第二种变体。.a > .b
与.a>.b
是相同的,但这两种表示法哪一种更正确呢?我看在Chrome开发者工具中使用了第二种变体。>
)、加号(U+002B,+
)和波浪号(U+007E,~
)。组合符与其周围的简单选择器之间可以有空白。空白只能由“空格”(U+0020)、“制表符”(U+0009)、“换行符”(U+000A)、“回车符”(U+000D)和“换页符”(U+000C)构成。其他类似空格的字符,如“全角空格”(U+2003)和“表意文字空格”(U+3000),永远不属于空白。”>
可以被空格包围,也可以没有区别。>
的部分也是如此。再次强调,规范甚至明确提到省略空格的位置,以证明它完全是可选的:
正如其他人所提到的,在编写CSS时,空格对于可读性是很有用的;如果文件大小是一个问题,请考虑在后期制作期间进行缩小和/或压缩样式表(缩小通常会为您剥离非重要的空格,其中可能包括组合器周围的空格)。您选择不使用空格来编写CSS的唯一原因是编码风格的问题,即使这被认为是不好的,但从技术上讲仍然是有效的。Examples:
The following selector represents a
p
element that is child ofbody
:body > p
The following example combines descendant combinators and child combinators.
div ol>li p
It represents a
p
element that is a descendant of anli
element; theli
element must be the child of anol
element; theol
element must be a descendant of adiv
. Notice that the optional white space around the ">" combinator has been left out.
.a > .b
然而,如果你希望样式表加载的更快(虽然速度提升不会很明显),你可以尝试使用以下代码:
.a>.b