伪类 伪元素 css3_伪类| CSS

迈不过友情╰ 2023-03-05 10:59 155阅读 0赞

伪类 伪元素 css3

These classes are basically used to add special effects to some selectors or in other words, we can say that A pseudo-class is basically used to define a special state of an element.

这些类基本上是用来为某些选择器添加特殊效果的,换句话说,可以说伪类基本上是用来定义元素的特殊状态的。

Through pseudo-classes, we don’t require javascript or any other script for these effects.

通过伪类 ,我们不需要javascript或任何其他脚本来实现这些效果。

The basic syntax of pseudo-classes as follows:

伪类的基本语法如下:

  1. selector:pseudo-class {property: value}

We can also write it in another way such as follows:

我们还可以用另一种方式编写它,如下所示:

  1. selector.class:pseudo-class {property: value}

The most commonly used pseudo-classes are:

最常用的伪类是:

  1. :link

    :链接

  2. :visited

    :已访问

  3. :hover

    :徘徊

  4. :active

    :活性

  5. :focus

    :焦点

  6. :first-child

    :第一个孩子

When we are defining pseudo classes in between opening and closing style tag following points should be noted.

在打开和关闭样式标签之间定义伪类时,应注意以下几点。

  1. The name of pseudo classes are not case sensitive.

    伪类的名称不区分大小写。

  2. These are basically different from CSS classes but then can be combined.

    这些基本上与CSS类不同,但是可以合并。

  3. In order to make CSS definition more effective following points should be keep in mind.

    为了使CSS定义更有效,请牢记以下几点。

    1. a:hover MUST come after a:link and a:visited
    2. a:active MUST come after a:hover

Basic Uses of Pseudo-Classes are as follows

伪类的基本用法如下

  1. It can be used to style an element when moves move over it.

    当在其上移动时,可用于设置元素的样式。

  2. It can style they visited and unvisited links differently.

    它可以使访问和未访问链接的样式不同。

  3. We can style an element when it gets focus.

    当元素获得焦点时,我们可以为其设置样式。

Let’s discuss commonly used pseudo classes one by one:

让我们一一讨论常用的伪类:

1. :link

1 .:链接

This is basically used to add special style to an unvisited link. The following example shows how to use the :link class to set the color of link.

基本上,这是用来为未访问的链接添加特殊样式。 以下示例显示如何使用:link类设置链接的颜色。

  1. <html>
  2. <head>
  3. <style type="text/css">
  4. a:visited {
  5. color: #FF00FF
  6. }
  7. </style>
  8. </head>
  9. <body><a href="">INCLUDEHELP!!!</a>
  10. </body>
  11. </html>

2. :visited

2.:拜访

This is basically used to add style to the previously visited link. The following example shows how to use the :visited class to set the color of previously visited link.

基本上,这是用于向以前访问的链接添加样式。 以下示例显示如何使用:visited类设置以前访问的链接的颜色。

  1. <html>
  2. <head>
  3. <style type="text/css">
  4. a:visited {
  5. color: #FF00FF
  6. }
  7. </style>
  8. </head>
  9. <body><a href="">INCLUDEHELP</a>
  10. </body>
  11. </html>

3. :hover

3.:悬停

This is basically used to add special style to the element when we move mouse over it. The following example shows how to use the :hover class to change the color of links when mouse moves over it.

当我们将鼠标移到元素上时,这基本上是用来为元素添加特殊样式的。 以下示例显示了如何在鼠标移到:hover类时更改链接的颜色。

  1. <html>
  2. <head>
  3. <style type="text/css">
  4. a:hover {
  5. color: #0000FF
  6. }
  7. </style>
  8. </head>
  9. <body><a href="">Move Mouse here</a>
  10. </body>
  11. </html>

4. :active

4.:活跃

This class is basically used to add special style to the element that are active. The following example shows how to use the :active class to change the color of all active links.

此类基本上用于向活动的元素添加特殊样式。 以下示例显示了如何使用:active类更改所有活动链接的颜色。

  1. <html>
  2. <head>
  3. <style type="text/css">
  4. a:active {
  5. color: #FF00FF
  6. }
  7. </style>
  8. </head>
  9. <body><a href="">INCLUDEHELP</a>
  10. </body>
  11. </html>

5. :focus

5.:重点

This class is basically used to add the special style to the element that are focused. The following example shows how to use the :focus class to change the color of the link when it is focused.

此类基本上用于向特殊元素添加特殊样式。 下面的示例演示如何使用:focus类在链接处于焦点状态时更改其颜色。

  1. <html>
  2. <head>
  3. <style type="text/css">
  4. a:focus {
  5. color: #FF00FF
  6. }
  7. </style>
  8. </head>
  9. <body><a href="">INCLUDEHELP</a>
  10. </body>
  11. </html>

6. :first-child

6.:第一个孩子

This class is basically used to add special style to the element that is the first child of another element.

此类基本上用于向元素添加特殊样式,该元素是另一个元素的第一个子元素。

  1. <html>
  2. <head>
  3. <style type="text/css">
  4. div >p:first-child {
  5. text-indent: 25px;
  6. color: #FF00FF;
  7. }
  8. </style>
  9. </head>
  10. <body>
  11. <div>
  12. <p>Here INCLUDEHELP is intendent</p>
  13. <p>Here INCLUDEHELP is not indented</p>
  14. </div>
  15. <p>But this will not change the paragraph in HTML document:
  16. </p>
  17. <div>
  18. <p>Here INCLUDEHELP will not be effected.
  19. </p>
  20. </div>
  21. </body>

翻译自: https://www.includehelp.com/code-snippets/pseudo-classes-in-css.aspx

伪类 伪元素 css3

发表评论

表情:
评论列表 (有 0 条评论,155人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CSS元素

    定义 伪类: CSS 伪类 是添加到选择器的关键字,指定要选择的元素的特殊状态。 例如,:hover 可被用于在用户将鼠标悬停在按钮上时改变按钮的颜色。

    相关 css 元素

    一、伪类、伪元素的共同点 1. 伪类、伪元素存在的原因: css引入伪类和伪元素概念是为了格式化文档树以外的信息。也就是说,伪类和伪元素是用来修饰不在文档树中的部分,比

    相关 CSS元素

    为什么要引入伪类与伪元素? css引入伪类和伪元素概念是为了格式化文档树以外的信息。也就是说,伪类和伪元素是用来修饰不在文档树中的部分,比如,一句话中的第一个字母,或是列

    相关 css元素

    在css1中就引入了伪类和伪元素的概念,伪类可以对一个元素的不同状态或者类型进行区分,添加特殊效果。伪元素能为元素的组成部分,或是文本结点内容添加特殊效果。