CSS选择器

2017-10-11  本文已影响0人  土旦土旦

基础选择器

* {
  color:red;
}
p {
 color:red;
}
 #box{
  color:red;
}
.content {
  color:red;
}
[name=lht] {
  color:red;
}

后代选择器

.content #box p {
  font-size: 20px;
}

子元素选择器

#odiv>p {
  color: red;
}

相邻兄弟元素

h3+p {
  color: green;
}

交集选择器

h3.content {
  color: red;
}
h3#h3 {
  background-color: yellow;
}

组合选择器

#h3,h1,p,span {
  color: #cceeaa;
}

动态伪类选择器

a:link {
  color:green;
}
a:visited {
  color:yellow;
}
a:active {
  color:pink;
}
a:hover {
  color:red;
}

伪元素选择器

p:before {
  content: "HELLO";
}
p:after {
  content: "see you";
}
p:first-line {
  color: red;
  font-size: 20px;
}
p:first-letter {
  font-size: 32px;
  color: black;
}

CSS3新增选择器

child系列选择器

p:first-child {
  color:#f00;
}
p:last-child {
  color:#f00;
}
li:nth-child(7) {
  color:#f00;
}
li:nth-last-child(3n+1) {
  color:#f00;
}  
li:only-child {
  color:#f00;
}

of-type系列选择器

span:first-of-type {
  color:#f00;
}
p:last-of-type {
  color:#f00;
}
dt:nth-of-type(2) {
  color:#f00;
} 
dt:nth-last-of-type(3n+1) {
  color:#f00;
}
dt:only-of-type {
  color:#f00;
}

兄弟选择器

span~span {
  color:#f00;
}

伪对象选择器

p::first-line {
  color:red;
}
p::first-letter {
  color:green;font-size:25px;
}
a::before {
  content:url("qvod.jpg");
}
a::after {
  content:url("qvod.jpg");
}
span::selection {
  background:#F0F;
}

UI状态伪类选择器

input:disabled {
  background:yellow;color:green; /*被禁止状态下状态下的颜色*/
}
input:enabled {
  background:#f90;color:red;
}
input:checked+span {
  background:red; /*选中状态下的颜色*/
}

属性选择器

p[id] {
background:red;
}
p[id="green"] {
background:green;
}
p[class~="yellow"] {
background:yellow;
}
p[class|="a"] {
background:blue;
}
a[href^="http://"] {
  color:red;text-decoration:none;
}
a[href$="rar"] {
  color:pink;text-decoration:none;
}
a[href$="rar"]:after {
  content:url(images/rar.jpg);
}
a[href*="o"] {
  background:green;
}
上一篇 下一篇

猜你喜欢

热点阅读