【发布时间】:2018-06-03 08:32:11
【问题描述】:
有人能解释一下这背后的理论吗? 我有这个代码; https://jsfiddle.net/vikaskulkarni/cnLdwxza/1/
<html>
<head>
<meta charset="utf-8">
<title>Articles</title>
<style>
.bodyCls {
float: left;
}
.bodyCls article {
width: 50%;
float: left;
}
article:first-child {
background-color: red;
}
article:nth-child(2) {
background-color: yellow;
}
article:nth-child(3) {
background-color: blue;
}
article:last-child {
background-color: green;
}
</style>
</head>
<body class="bodyCls">
<div>
<article>First</article>
<article>Second</article>
<article>Third</article>
<article>Fourth</article>
</div>
</body>
</html>
这很好用。 但是,如果我在此链接中从正文中删除 DIV,则第一个子选择器不起作用, https://jsfiddle.net/vikaskulkarni/95gbpmf8/1/
<html>
<head>
<meta charset="utf-8">
<title>Articles</title>
<style>
.bodyCls {
float: left;
}
.bodyCls article {
width: 50%;
float: left;
}
article:first-child {
background-color: red;
}
article:nth-child(2) {
background-color: yellow;
}
article:nth-child(3) {
background-color: blue;
}
article:last-child {
background-color: green;
}
</style>
</head>
<body class="bodyCls">
<article>First</article>
<article>Second</article>
<article>Third</article>
<article>Fourth</article>
</body>
</html>
我在网上搜索,发现在使用选择器时总是涉及到一个 DIV。所以我对这些选择器的工作方式有点困惑。
【问题讨论】:
标签: css css-selectors selector