使用 CSS 设置链接颜色

cssweb developmentfront end technology更新于 2025/2/2 11:56:17

CSS 允许我们为链接设置颜色。color 属性用于设置元素中文本的颜色。

伪选择器的顺序如下::link、:visited、:hover、:active。

语法

CSS color 属性的语法如下 −

Selector {
   color: /*value*/
}

示例

以下示例说明 CSS 颜色属性 −

<!DOCTYPE html>
<html>
<head>
<style>
#demo::after {
   content: " (visited) ";
   font-style: italic;
}
a:link {
   color: lime;
}
a:visited {
   color: green;
}
a:hover {
   color: orange;
}
a:active {
   color: gold;
}
</style>
</head>
<body>
<h2>Demo Links</h2>
<a id="demo" href="">This is demo text 1.</a>
<hr>
<p>
<a href="#">This is demo text 2.</a>
</p>
</body>
</html>

输出

这将给出以下输出 −

鼠标悬停在第二个链接上时,我们得到以下输出 −

点击第二个链接时,我们得到以下输出 −

示例

<!DOCTYPE html>
<html>
<head>
<style>
div {
   margin: auto;
   font-size: 1.4em;
   background-color: lightseagreen;
   width: 100px;
   text-align: center;
}
a:link {
   color: rebeccapurple;
}
</style>
</head>
<body>
<div>
<a id="demo" href="">Demo link.</a>
</div>
</body>
</html>

输出

这将给出以下输出 −


相关文章


有用资源