200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 【重识 HTML + CSS】CSS 伪类 伪元素

【重识 HTML + CSS】CSS 伪类 伪元素

时间:2022-09-23 00:20:53

相关推荐

【重识 HTML + CSS】CSS 伪类 伪元素

CSS 伪类、伪元素

伪类 (pseudo-classes)动态伪类 (dynamic pseudo-classes):link、:visited、:hover、:active:focus目标伪类 :target语言伪类 :lang元素状态伪类 :enable、:disable、:checked结构伪类 (structural pseudo-classes):nth-child()、:nth-last-child():nth-of-type()、:nth-last-of-type():empty等价描述否定伪类 :not()伪元素 (pseudo-elements)::first-line 对首行文本设置属性::first-letter 对首字母设置属性::before、::after 在元素内容之前、之后插入其他内容

博文集合:【重识 HTML + CSS】知识点目录

本章 Gitee 代码:/szluyu99/html_css_note/tree/master/day04

伪类 (pseudo-classes)

常见的伪类有:

动态伪类(dynamic pseudo-classes)

:link:visited:hover:active:focus目标伪类(target pseudo-classes)

:target语言伪类(language pseudo-classes)

:lang()元素状态伪类 (UI element states pseudo-classes)

:enabled:disabled:checked结构伪类 (structural pseudo-classes)

:nth-child( ):nth-last-child( ):nth-of-type( ):nth-last-of-type( )

:first-child:last-child:first-of-type:last-of-type

:root:only-child:only-of-type:empty否定伪类 (negation pseudo-classes)

:not()

动态伪类 (dynamic pseudo-classes)

动态伪类的细节:直接给 a 元素设置样式,相当于给 a 元素的所有动态伪类都设置

下图效果相当于:a:linka:visiteda:hovera:activea:focus的 color 都是 red

:link、:visited、:hover、:active

使用举例:

a:link:未访问的链接a:visited:已访问的链接a:hover:鼠标挪动到链接上a:active:激活的链接(鼠标在链接上长按住未松开)

使用注意:动态伪类的使用是有顺序的,因为后面的样式会覆盖前面的样式

:hover必须放在:link:visited后面才能完全生效:active必须放在:hover后面才能完全生效建议的编写顺序::link:visited:hover:active

除了 a 元素,:hover:active也能用在其他元素上

:focus

:focus指当前拥有输入焦点的元素(能接收键盘输入)

动态伪类编写顺序::link:visited:focus:hover:active

效果示例:

文本输入框一聚焦后,背景就会变红色:

因为链接 a 元素可以被键盘的 Tab 键选中聚焦,所以:focus也适用于 a 元素

去除 a 元素默认的:focus效果:

设定元素不会被 TAB 选中,可以将 tabindex 属性设置为-1

目标伪类 :target

目标伪类:当元素被锚点链接当作目标跳转之后起作用

语言伪类 :lang

元素状态伪类 :enable、:disable、:checked

:enabled:启用状态

:disabled:禁用状态

:checked:被选中状态

结构伪类 (structural pseudo-classes)

:nth-child()、:nth-last-child()

用法:nth-child(an + b)

nth-child(1):父元素中的第1个子元素

:nth-child(2n):父元素中第偶数个子元素

等价于nth-child(even):nth-child(2n + 1):父元素中的第奇数个子元素

等价于:nth-child(odd):nth-child(-n + 2):父元素中最前面 2 个子元素

nth-last-child()语法跟:nth-child()类似,不同点是:nth-last-child()从最后一个子元素开始往前计数

:nth-last-child(1):倒数第一个子元素:nth-last-child(-n + 2):最后 2 个子元素

思考:如何表示第 2 个 ~ 倒数第 2 个元素(去除头和尾元素)

:nth-of-type()、:nth-last-of-type()

:nth-of-type()用法跟:nth-child()类似

不同点是:nth-of-type()计数时只计算同种类型的元素

:nth-last-of-type()用法跟:nth-of-type()类似不同点是:nth-last-of-type()从最后一个这种类型的子元素开始往前计数

:empty

:empty代表里面完全空白的元素

等价描述

:first-child等同于:nth-child(1):last-child等同于:nth-last-child(1):first-of-type等同于:nth-of-type(1):last-of-type等同于:nth-last-of-type(1):only-child是父元素中唯一的子元素

等同于:first-child:last-child或者:nth-child(1):nth-last-child(1):only-of-type是父元素中唯一的这种类型的子元素

等同于first-of-type:last-of-type或者:nth-of-type(1):nth-last-of-type(1):root根元素就是 HTML 元素

否定伪类 :not()

:not()的格式是:not(x)

x是一个简单选择器

元素选择器、通用选择器、属性选择器、类选择器、id选择器、伪类(除否定伪类):not(x)表示除 x 以外的元素

:not()支持简单选择器,不支持组合。比如下面的写法是不支持

伪元素 (pseudo-elements)

常用的伪元素有:

:first-line::first-line:first-letter::first-letter:before::before:after::after

为了区分伪元素和伪类,建议伪元素使用 2 个冒号,比如::first-line

::first-line 对首行文本设置属性

::first-line可以针对首行文本设置属性

只有下列属性可以应用在::first-line伪元素

字体属性、颜色属性、背景属性word-spacingletter-spacingtext-decorationtext-transformline-height

::first-letter 对首字母设置属性

::first-letter可以针对首字母设置属性

只有下列属性可以应用在::first-letter伪元素:

字体属性、margin属性、padding属性、border属性、颜色属性、背景属性text-decoration、text-transform、letter-spacing、word-spacing(适当的时候)、line-height、float、vertical-align(只有当float是none时)

::before、::after 在元素内容之前、之后插入其他内容

::before::after用来在一个元素的内容之前或之后插入其他内容(可以是文字、图片)

在 CSS 属性值中,使用url(图片的URL)来引用图片:

url(dot.png);url('dot.png');url("dot.png");,建议使用这种

content中,还可以使用attr(属性名)来获得元素的属性值

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。