博客列表 >css选择器总结

css选择器总结

amin
amin原创
2020年06月16日 16:22:34634浏览

id选择器:#id
类选择器:.class
选择器叠加:.class1.class2 (选择同时含有这两个class的元素)
元素选择器:eg: div p (选择所有满足条件的后代)
父子选择器:div>p (选择满足条件的儿子)
兄弟选择器:div+p (选择紧邻div之后的p元素)
同级选所有:div ~ p (选择所有div之后的兄弟元素中有P的元素)
伪类选择器::nth-child(n)该选择器选取父元素的第n 个子元素,不论元素的类型。

  1. /* 选择第一个子元素 */
  2. .container > :first-child
  3. .container > .item:first-child
  4. /* 选择最后一个子元素 */
  5. .container > :last-child
  6. /* 选第3个: 索引是从1开始 */
  7. .container > :nth-child(3)
  8. /* 选择偶数行元素 */
  9. .container > :nth-child(even)
  10. .container > :nth-child(2n)
  11. /* 选择奇数行元素 */
  12. .container > :nth-child(odd)
  13. .container > :nth-child(2n-1)
  14. /* 从指定位置(从第4个开始),选择剩下的所有元素 */
  15. .container > .item:nth-child(n + 4)
  16. /* 选择前三个元素 */
  17. .container .item:nth-child(-n + 3)
  18. /* 选择最后三个元素 */
  19. .container .item:nth-last-child(-n + 3)
  20. /* 选择倒数第二个元素 */
  21. .container .item:nth-last-child(2)

结构伪类选择器: :nth-of-type(n)选择器选取父元素的第 n 个指定类型的子元素,eg .class p:nth-of-type(1)

  1. /*选择满足条件第一个元素*/
  2. :nth-of-type1
  3. /*选择满足条件最后一个元素*/
  4. :last-of-type
  5. /*选择满足条件前两个元素*/
  6. :nth-of-type(-n + 2)
  7. /*选择满足条件最后两个元素*/
  8. :nth-last-of-type(-n + 2)

伪类和伪元素:

  1. 常见伪类——:hover,:link,:active,:target,:not(),:focus
  2. 常见伪元素——::first-letter,::first-line,::before,::after,::selection
  3. :link a:link 选择所有未被访问的链接
  4. :visited a:visited 选择所有已被访问的链接
  5. :active a:active 选择活动链接
  6. :hover a:hover 选择鼠标指针位于其上的链接
  7. :focus input:focus 选择获得焦点的 input 元素
  8. :first-letter p:first-letter 选择每个 <p> 元素的首字母
  9. :first-line p:first-line 选择每个 <p> 元素的首行
  10. :before p:before 在每个 <p> 元素的内容之前插入内容
  11. :after p:after 在每个 <p> 元素的内容之后插入内容
  12. :lang(language) p:lang(it) 选择带有以 "it" 开头的 lang 属性值的每个 <p> 元素。
  13. element1~element2 p~ul 选择前面有 <p> 元素的每个 <ul> 元素。
  14. [attribute^=value] a[src^="https"] 选择其 src 属性值以 "https" 开头的每个 <a> 元素。
  15. [attribute$=value] a[src$=".pdf"] 选择其 src 属性以 ".pdf" 结尾的所有 <a> 元素。
  16. [attribute*=value] a[src*="abc"] 选择其 src 属性中包含 "abc" 子串的每个 <a> 元素。
  17. :empty p:empty 选择没有子元素的每个 <p> 元素(包括文本节点)
  18. :target #news:target 选择当前活动的 #news 元素
  19. :enabled input:enabled 选择每个启用的 <input> 元素。
  20. :disabled input:disabled 选择每个禁用的 <input> 元素
  21. :checked input:checked 选择每个被选中的 <input> 元素
  22. :not(selector) :not(p) 选择非 <p> 元素的每个元素
  23. ::selection ::selection 选择被用户选取的元素部分
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议