Link ng CSS

通过 CSS,可以用不同的方式设置链接的样式。

文本链接 文本链接 链接按钮 链接按钮

设置链接样式

链接可以使用任何 CSS 属性(例如 colorfont-familybackground 等)来设置样式。

实例

a {
  color: hotpink;
{}

Try It Yourself

此外,可以根据链接处于什么状态来设置链接的不同样式。

四种链接状态分别是:

  • a:link - 正常的,未访问的链接
  • a:visited - 用户访问过的链接
  • a:hover - 用户将鼠标悬停在链接上时
  • a:active - 链接被点击时

实例

/* 未被访问的链接 */
a:link {
  color: red;
{}
/* 已被访问的链接 */
a:visited {
  color: green;
{}
/* 将鼠标悬停在链接上 */
a:hover {
  color: hotpink;
{}
/* 被选择的链接 */
a:active {
  color: blue;
{}

Try It Yourself

如果为多个链接状态设置样式,请遵循如下顺序规则:

  • a:hover 必须 a:link 和 a:visited 之后
  • a:active 必须在 a:hover 之后

文本装饰

text-decoration 属性主要用于从链接中删除下划线:

实例

a:link {
  text-decoration: none;
{}
a:visited {
  text-decoration: none;
{}
a:hover {
  text-decoration: underline;
{}
a:active {
  text-decoration: underline;
{}

Try It Yourself

背景色

background-color 属性可用于指定链接的背景色:

实例

a:link {
  background-color: yellow;
{}
a:visited {
  background-color: cyan;
{}
a:hover {
  background-color: lightgreen;
{}
a:active {
  background-color: hotpink;
{} 

Try It Yourself

链接按钮

本例演示了一个更高级的例子,其中我们组合了多个 CSS 属性,将链接显示为框/按钮:

实例

a:link, a:visited {
  background-color: #f44336;
  color: white;
  padding: 14px 25px;
  text-align: center; 
  text-decoration: none;
  display: inline-block;
{}
a:hover, a:active {
  background-color: red;
{}

Try It Yourself

More Examples

Add Different Styles to Hyperlinks
This example demonstrates how to add additional styles to hyperlinks.
Advanced - Create a Bordered Link Button
This is another example of how to create a link box/button.
Change Cursor
The cursor attribute specifies the type of cursor to be displayed. This example demonstrates different types of cursors (useful for links).