• 微信号
目录

html笔记

您当前的位置:首页 > 我的笔记 > html笔记>css伪类选择器

css伪类选择器

超链接伪类选择器

在支持 CSS 的浏览器中,链接的不同状态都可以不同的方式显示,这些状态包括:活动状态,已被访问状态,未被访问状态,和鼠标悬停状态

选择器

  • a:visited :已被访问状态
  • a:link :未访问状态
  • a:hover :鼠标悬停状态
  • :active :用户激活

语法:

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

表单:focus

:focus表单获得焦点时触发样式

input:focus{color: red;}

first-child

:first-child 伪类来选择元素的第一个子元素

p:first-child{color: red;}

:last-child

:last-child 伪类来选择元素的最后一个子元素

p:last-child{color: red;}

:nth-child()

nth-child(number)

表示第几个元素

/* 选择第二个元素 */
p:nth-child(2){color: red;}

nth-child(odd)和nth-child(even)

nth-child(odd)表示取出奇数的子元素,即取出第1,3,5,...个子元素

nth-child(even)表示取出偶数的子元素,即取出第2,4,6,...个子元素

/* 选择奇数的子元素 */
p:nth-child(odd){color: red;}
p:nth-child(2n-1){color: red;}
/* 选择偶数子元素 */
p:nth-child(even){color: red;}
p:nth-child(2n){color: red;}

nth-child(±an+b)

表示从b点,向左或者向右按步长a(a>0)取出子元素

/* 选取每个第三段,从第二个子元素开始 */
p:nth-child(3n+2){color: red;}

所有伪类选择器

选择器 例子 例子描述
:active a:active 选择活动的链接。
:checked input:checked 选择每个被选中的 <input> 元素。
:disabled input:disabled 选择每个被禁用的 <input> 元素。
:empty p:empty 选择没有子元素的每个 <p> 元素。
:enabled input:enabled 选择每个已启用的 <input> 元素。
:first-child p:first-child 选择作为其父的首个子元素的每个 <p> 元素。
:first-of-type p:first-of-type 选择作为其父的首个 <p> 元素的每个 <p> 元素。
:focus input:focus 选择获得焦点的 <input> 元素。
:hover a:hover 选择鼠标悬停其上的链接。
:in-range input:in-range 选择具有指定范围内的值的 <input> 元素。
:invalid input:invalid 选择所有具有无效值的 <input> 元素。
:lang(language) p:lang(it) 选择每个 lang 属性值以 "it" 开头的 <p> 元素。
:last-child p:last-child 选择作为其父的最后一个子元素的每个 <p> 元素。
:last-of-type p:last-of-type 选择作为其父的最后一个 <p> 元素的每个 <p> 元素。
:link a:link 选择所有未被访问的链接。
:not(selector) :not(p) 选择每个非 <p> 元素的元素。
:nth-child(n) p:nth-child(2) 选择作为其父的第二个子元素的每个 <p> 元素。
:nth-last-child(n) p:nth-last-child(2) 选择作为父的第二个子元素的每个<p>元素,从最后一个子元素计数。
:nth-last-of-type(n) p:nth-last-of-type(2) 选择作为父的第二个<p>元素的每个<p>元素,从最后一个子元素计数
:nth-of-type(n) p:nth-of-type(2) 选择作为其父的第二个 <p> 元素的每个 <p> 元素。
:only-of-type p:only-of-type 选择作为其父的唯一 <p> 元素的每个 <p> 元素。
:only-child p:only-child 选择作为其父的唯一子元素的 <p> 元素。
:optional input:optional 选择不带 "required" 属性的 <input> 元素。
:out-of-range input:out-of-range 选择值在指定范围之外的 <input> 元素。
:read-only input:read-only 选择指定了 "readonly" 属性的 <input> 元素。
:read-write input:read-write 选择不带 "readonly" 属性的 <input> 元素。
:required input:required 选择指定了 "required" 属性的 <input> 元素。
:root root 选择元素的根元素。
:target #news:target 选择当前活动的 #news 元素(单击包含该锚名称的 URL)。
:valid input:valid 选择所有具有有效值的 <input> 元素。
:visited a:visited 选择所有已访问的链接。