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

2018-11-19  本文已影响13人  优秀的人A
p[title]{
    background-color:red;
}
选择器为title的背景变为红色

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的背景变为橙色

子元素选择器

1.选择第一个子元素
:first-child{
    color:red;
}
2.选择最后一个子元素
:last-child{
    color:yellow;
}
3.选择指定位置的子元素
:nth-child(3){
    background-color:green;
}
位置是第3个的标签背景为绿色

:nth-child(even){
    background-color:red;
}
位置是偶数的背景为红色

:nth-child(odd){
    background-color:yellow;
}
位置为基数的背景为黄色
4.选择指定类型的子元素
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的第三个标签背景为黄色
上一篇 下一篇

猜你喜欢

热点阅读