Maison  >  Article  >  interface Web  >  最容易让人误解的10个 CSS 选择器_html/css_WEB-ITnose

最容易让人误解的10个 CSS 选择器_html/css_WEB-ITnose

WBOY
WBOYoriginal
2016-06-24 11:57:291165parcourir

这些选择器包括 first-child、last-child、only-child、nth-child(n)、nth-last-child(n) 和 first-of-type、last-of-type、only-of-type、nth-of-type(n)、nth-last-of-type(n)。在所有的选择器中,这十个选择器大概是最容易被人误解的选择器。


一、关于n

1)n 的表达式为:xn + y 。如1、2n、2n+1。

2)nth-child(n) 系列的 n 是对父元素的所有子元素进行编号;nth-type-child(n) 中的 n 仅对同一类型的元素进行编号,忽略中间夹杂的其他元素。


二、E:first-child 与 E :first-child

上面的两种写法并不相同,区别在于 :first-child 前有无空格,有无空格,差别很大,这也正是最易让人误解的地方。

1)没有空格的 E:first-child 表示“选择这样的E元素,E元素是其父元素的第一个子元素”;

2)有空格的 E :first-child 表示”选择 E 元素的第一个子元素“。

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn