P first
情况一:这个P first是你打错了,正确的是 P .first,那么表示p元素的所有class属性值为first的后代元素
情况二:如果你没打错,P first这个我也没见到过,无法给你答案。
根据情况一再结合第一句话。他们之间的区别,一个是后代选择器即P .first,一个是类选择器即P.first,当然这个是大致的划分开他们之间的区别。
css2里的伪类均摊符,意思是:匹配父元素的第一个子元素你可以在css手机里可以查到这个伪类选择符
比如:
<ul>
<li>列表项一</li>
<li>列表项二</li>
<li>列表项三</li>
<li>列表项四</li>
</ul>
在上述代码中,如果我们要设置第一个li的样式,那么代码应该写成li:first-child{sRules},而不是ul:first-child{sRules}。其它相关的伪类都如此。
发现 last-child 和 first child 中的样式不生效。然后把 html 中 class 为 userinfo-item 的 div 前后的元素都去掉,last-child 和 first-child 样式才生效。
当时实际使用中还是需要 div 前后的元素,此时把 first-child 和 last-child 分别改成 first-of-type 和 last-of-type 即可,样式都正常生效。
first-child :指的是父元素的第一个元素,在上面的例子中,要实现样式的话需要保证 class 为 userinfo-item 的 div 元素没有兄弟元素,或者在创建一个单独 div 包裹起来;
first-of-type :指的是父元素下,相同类型子元素中的第一个,上面的例子中因为 class 为 userinfo-item 的 div 元素是第一个 div 元素,所以生效了,此时如果前面有其他 div 元素,则样式还是不生效的;
last-child 和 last-of-type 原理类似。
first-of-type mdn: https://developer.mozilla.org/zh-CN/docs/Web/CSS/:first-of-type
first-child mdn: https://developer.mozilla.org/zh-CN/docs/Web/CSS/:first-child