当前位置:   article > 正文

伪类选择器和伪元素选择器基础(1)

伪类选择器

伪类选择器

伪类选择器简称“伪类”伪类是一种不存在的类,伪类用来表示元素的一种状态

动态伪类

:link

链接没有被访问前的样式效果
:visited链接被访问后的样式效果
:hover鼠标悬停在元素上面时的样式效果
:active点击元素时的样式效果,即按下鼠标左键时发生的样式
:focus用于元素成为焦点时的样式效果,常用与表单元素

动态伪类
动态伪类是一类行为类样式,这些样式不存在于HTML中,只有当用户于页面进行交互才能体现出来。动态伪类伪类选择器包括两种形式:

锚点伪类,这是一种在链接中最常见的样式,如:link、:visited。

行为伪类,也成用户操作伪类,如:hover、:active、:focus。

Ⅱ  伪元素选择器

::first-letter表示第一个字母,例如:p::first-letter{}

::first-line表示第一行(第一行内容根据屏幕大小来决定显示多少字),例如: p::first-line{}

::selection表示选中的内容

::before表示元素的开始,::after表示元素的最后,before和after必须结合content属性来使用

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>2024.4.24</title>
  6. <style>
  7. p::first-letter{
  8. font-size: 30px;
  9. color: blueviolet;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <p>The Government has pledged to change the law to introduce a minimum service requirement so that, even when strikes occur, ..., more investment is needed, but passengers will not be willing to pay more indefinitely if they must also endure cramped, unreliable services, punctuated by regular chaos when timetables are changed, or planned maintenance is managed incompetently. The threat of nationalisation may have been seen off for now, but it will return with a vengeance if the justified anger of passengers is not addressed in short order.</p>
  15. </body>
  16. </html>

 效果图

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>2024.4.24</title>
  6. <style>
  7. p::first-letter{
  8. font-size: 30px;
  9. color: blueviolet;
  10. }
  11. p::first-line{
  12. color: red;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <p>The Government has pledged to change the law to introduce a minimum service requirement so that, even when strikes occur, ..., more investment is needed, but passengers will not be willing to pay more indefinitely if they must also endure cramped, unreliable services, punctuated by regular chaos when timetables are changed, or planned maintenance is managed incompetently. The threat of nationalisation may have been seen off for now, but it will return with a vengeance if the justified anger of passengers is not addressed in short order.</p>
  18. </body>
  19. </html>

效果图

 

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>2024.4.24</title>
  6. <style>
  7. p::first-letter{
  8. font-size: 30px;
  9. color: blueviolet;
  10. }
  11. p::first-line{
  12. color: red;
  13. }
  14. p::selection{
  15. color: aqua;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <p>The Government has pledged to change the law to introduce a minimum service requirement so that, even when strikes occur, ..., more investment is needed, but passengers will not be willing to pay more indefinitely if they must also endure cramped, unreliable services, punctuated by regular chaos when timetables are changed, or planned maintenance is managed incompetently. The threat of nationalisation may have been seen off for now, but it will return with a vengeance if the justified anger of passengers is not addressed in short order.</p>
  21. </body>
  22. </html>

 效果图

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>2024.4.24</title>
  6. <style>
  7. p::first-letter{
  8. font-size: 30px;
  9. color: blueviolet;
  10. }
  11. p::first-line{
  12. color: red;
  13. }
  14. p::selection{
  15. color: aqua;
  16. }
  17. p::after{
  18. content: "遇你不做智者";
  19. color: red;
  20. }
  21. p::before{
  22. content: "智者不入爱河";
  23. color: coral;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <p>The Government has pledged to change the law to introduce a minimum service requirement so that, even when strikes occur, ..., more investment is needed, but passengers will not be willing to pay more indefinitely if they must also endure cramped, unreliable services, punctuated by regular chaos when timetables are changed, or planned maintenance is managed incompetently. The threat of nationalisation may have been seen off for now, but it will return with a vengeance if the justified anger of passengers is not addressed in short order.</p>
  29. </body>
  30. </html>

 效果图

 Ⅲ 伪类选择器和伪元素选择器的比较

伪类选择器是用来向某些选择器来添加效果。不修改DOM内容,通过一些特定的选择器根据特定的状态、特定条件来修改元素的样式。比如:悬停(hover)、点击(active)以及文档中不能通过其它选择器选择的元素(这些元素没有 ID 或 class 属性),例如第一个子元素(first-child)或者最后一个子元素(last-child)。

伪元素选择器是一个附加在选择器末尾的关键词,通过伪元素您不需要借助元素的 ID 或 class 属性就可以对被选择元素的特定部分定义样式。例如通过伪元素您可以设置段落中第一个字母的样式(::first-letter),或者在元素之前(::before)、之后插入一些内容(::after)等等。伪元素可能改变DOM结构,创造了虚拟的DOM。
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/1014418
推荐阅读
相关标签
  

闽ICP备14008679号