使用 CSS 样式不同状态的链接


通过使用 CSS 伪类选择器(active、hover、link 和 visited)可以为不同状态的链接设置样式。顺序如下,才能正常使用这些选择器:link、visited、hover、active。

语法

CSS text-indent 属性的语法如下 −

a:(pseudo-selector) {
   /*declarations*/
}

以下是链接的一些主要伪类 −

  • :active = 选择激活的链接

  • :hover = 鼠标悬停时选择链接

  • :hover = 鼠标悬停时选择链接

  • :valid = 选择值的有效元素

  • :visited = 选择所有已访问的链接

以下示例说明为链接的不同状态设置样式 −

为链接的状态设置样式

使用伪类在此处设置链接的不同状态 −

a:link {
   color: navy;
   text-decoration: none;
}
a:visited {
   color: yellowgreen;
}
a:hover {
   color: orange;
   text-decoration: wavy underline;
}
a:active {
   color: red;
}

示例

让我们看下示例 −

<!DOCTYPE html>
<html>
<head>
   <style>
      div {
         margin-left: 20px;
         display: flex;
         float: left;
      }
      a:link {
         color: navy;
         text-decoration: none;
      }
      a:visited {
         color: yellowgreen;
      }
      a:hover {
         color: orange;
         text-decoration: wavy underline;
      }
      a:active {
         color: red;
      }
   </style>
</head>
<body>
   <div>
      <div>
         <a href="#">Demo link </a>
      </div>
      <div>
         <a href="">Demo visited link</a>
      </div>
   </div>
</body>
</html>

为链接的状态设置样式并更改阴影

链接状态用于在激活链接和其他链接时更改元素的阴影 −

a:hover {
   color: orange;
   font-size: 150%;
   font-weight: bold;
   box-shadow: 0 0 5px 1px grey;
}
a:active {
   color: red;
   box-shadow: inset 0 0 15px red;
}

示例

让我们看下示例 −

<!DOCTYPE html>
<html>
<head>
   <style>
      div {
         display: flex;
         float: left;
      }
      a {
         margin: 20px;
         padding: 10px;
         border: 2px solid black;
         text-shadow: -1px 0px black, 0px -1px black, 0px 1px black, 1px 0px black;
         font-size: 1.1em;
      }
      a:link {
         text-decoration: none;
      }
      a:visited {
         color: blueviolet;
      }
      a:hover {
         color: orange;
         font-size: 150%;
         font-weight: bold;
         box-shadow: 0 0 5px 1px grey;
      }
      a:active {
         color: red;
         box-shadow: inset 0 0 15px red;
      }
   </style>
</head>
<body>
   <div>
      <a href="#">Kapow!</a>
      <a href="">Dishoom</a>
   </div>
</body>
</html>

更新于: 27-12-2023

179 次浏览

开启 职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.