属性选择器和子元素选择器

p[title]{
    background-color:red;
}
选择器为title的背景变为红色

---4.
p[title="abc"]{
background-color:skyblue;
}
选择器是title等于abc的背景变为天蓝色


p[title^="a"]{
    background-color:green;
}
选择器是title以a开头的背景变为绿色

p[title$="c"]{
    background-color:yellow;
}
选择器是title以c结尾的背景变为黄色

p[title*="b"]{
    background-color:orange;
}
选择器是title包含b的背景变为橙色

子元素选择器

选择第一个子元素

:first-child{
    color:red;
}

选择最后一个子元素

:last-child{
    color:yellow;
}

选择指定位置的子元素

  :nth-child(3){
      background-color:green;
  }
位置是第3个的标签背景为绿色
:nth-child(even){
    background-color:red;
}
位置是偶数的背景为红色

:nth-child(odd){
    background-color:yellow;
}
位置为基数的背景为黄色

选择指定类型的子元素

p:first-of-type{
    background-color:blue;
}
类型是p的第一个标签背景为蓝色

p:last-of-type{
    background-color:skyblue;
}
类型是p的最后一个标签背景为天蓝色

p:nth-of-type(3){
    background-color:yellow;
}
类型是p的第三个标签背景为黄色
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容