标签选择器
type selector
div {width: 100px;height: 100px;background-color: lightblue;margin-bottom: 10px;}p {width: 50px;height: 70px;background-color: greenyellow;}
ID选择器
ID selector
#mydiv {background-color: indianred;}#myp {background-color: aquamarine;}
类选择器
class selector
.big-tag {width: 200px;height: 200px;}.red-tag {background-color: red;}
:::danger
id与class的区别:
一般来讲id唯一定义
class可以用于多处
:::
伪类选择器
pseudo-classes
伪类用于定义元素的特殊状态。
链接伪类选择器:
:link:链接访问前的样式:visited:链接访问后的样式:hover:鼠标悬停时的样式:active:鼠标点击后长按时的样式:focus:聚焦后的样式
位置伪类选择器:
:nth-child(n):选择是其父标签第n个子元素的所有元素。:nth-child(an + b):选择是其父标签的第an + b个元素的所有元素
目标伪类选择器:
:target:当url指向该元素时生效。 ```css .effert:hover { transform: scale(1.1); transition: 200ms; }
mydiv:hover {
background-color: aqua;transition: 200ms;
}
a:link { color: red; }
a:visited { color: blue; }
a:hover { color: purple; }
a:active { color: orange; }
input:focus { background-color: aqua; }
p:nth-child(2n + 1) { background-color: rebeccapurple; }
p:target { transform: scale(1.2); background-color: aqua; transition: 2000ms; }
<a name="qrgJ5"></a>## 复合选择器由两个及以上基础选择器组合而成的选择器。- `element1, element2`:同时选择元素element1和元素element2(并联)。- `element.class`:选则包含某类的element元素。(可级联)- `element1 + element2`:选择紧跟element1的element2元素(前后关系)。- `element1 element2`:选择element1内的所有element2元素(祖先关系)。- `element1 > element2`:选择父标签是element1的所有element2元素(父子关系)。```css// 实例1div,p {background-color: aqua;}// 实例2div.bigger.real {transform: scale(1.2);}// 实例3div+p {background-color: lightgreen;}p+#myp {background-color: lightpink;}// 实例4ul ul {color: lightgreen;}// 实例5li>ul>li {color: lightgreen;}
通配符选择器
*:选择所有标签[attribute]:选择具有某个属性的所有标签[attribute=value]:选择attribute值为value的所有标签[attribute*='value']:选择attribute内包含value的所有标签
* {font-size: 24px;}input[id] {background-color: aqua;}input[type=text] {background-color: blueviolet;}
伪元素选择器
将特定内容当做一个元素,选择这些元素的选择器被称为伪元素选择器。
::first-letter:选择第一个字母::first-line:选择第一行::selection:选择已被选中的内容::after:可以在元素后插入内容::before:可以在元素前插入内容 ```css // 实例1 p::first-letter { color: red; font-size: 150%; }
// 实例2 p::first-line { color: red; font-size: 150%; }
// 实例3 p::selection { color: aqua; background-color: blanchedalmond; }
// 实例4 h1::before { content: “《”; color: red; }
h1::after { content: “》”; color: red; } ```
样式渲染优先级
- 权重大小,越具体的选择器权重越大:
!important> 行内样式 > ID选择器 > 类与伪类选择器 > 标签选择器 > 通用选择器 - 权重相同时,后面的样式会覆盖前面的样式
- 继承自父元素的权重最低
