CSS 中的链接伪类


使用 CSS 伪类选择器,即 :active、:hover、:link 和 :visited,我们可以设计链接/锚的各种状态。为了实现正确功能,这些选择器的顺序应如下所示 −

  • :link
  • :visited
  • :hover
  • :active

CSS 伪类属性的语法如下 −

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

示例

让我们通过一个示例了解如何使用 CSS 锚伪类 −

 在线演示

<!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>
<h2>Your favourite sports?</h2>
<a href="#">Football</a>
<a href="">Cricket</a>
</div>
</body>
</html>

输出

初始输出 −

将鼠标悬停在第一个链接上,我们得到以下输出

单击第一个链接时,返回以下输出 −

我们来看另一个 CSS 锚伪类的示例 −

示例

 在线演示

<!DOCTYPE html>
<html>
<head>
<title>CSS Anchor Pseudo Classes</title>
</head>
<style>
div {
   color: #000;
   padding:20px;
   background-image: linear-gradient(135deg, grey 0%, white 100%);
   text-align: center;
}
.anchor {
   color: #FF8A00;
}
.anchor:last-child {
   color: #03A9F4;
}
.anchor:visited {
   color: #FEDC11;
}
.anchor:hover {
   color: #C303C3;
}
.anchor:active {
   color: #4CAF50;
}
</style>
<body>
<div>
<h1>Search Engines</h1>
<a class="anchor" href="https://www.google.com" target="_blank">Go To Google</a>
<a class="anchor" href="https://www.bing.com" target="_blank">Go To Bing</a>
</div>
</body>
</html>

输出

更新日期: 08-Jul-2020

1K+ 浏览

开启你的 职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.