今天我在查看我发现的一些示例代码时注意到开发人员使用“+”而不是“,”来选择两个类。
.region + .region{
border-left: 1px solid lightgray;
padding-left: 3.5%;
margin-left: 4%;
}
“+”有什么作用而“,”没有?你为什么要用它?奇怪的是,同一个类(class)在这里被选中了两次。为什么?
这里是代码笔的链接... http://codepen.io/adobe/pen/vKixh
我刚刚学习 CSS3,如有任何帮助,我们将不胜感激。谢谢
它们不一样!
+
是邻接选择器,例如选择 .region
元素之后的 .region
元素。
逗号分隔的列表只允许您将一个样式 block 应用于多个类,因此:
.region, .region{}
实际上不会执行 .region{}
本身不会执行的任何操作。
邻接选择器 ( MDN article )
(+) This is referred to as an adjacent selector. It will select only the
specified element that immediately follows the former specified
element.
记住这一点,.region + .region{}
只会将样式应用于 .region
之后的另一个,而不是孤立的,或第一个.