使用 CSS,可以通过许多不同的方式设置链接样式。
样式链接
链接可以使用任何 CSS 属性(例如color
、font-family
、background
等)设置样式。
a {
color: hotpink;
}
![](https://img.haomeiwen.com/i21922576/e73d8a12ef307612.png)
此外,可以设置链接样式 根据它们所处的状态而有所不同。
四个链接状态是:
-
a:link
- 一个正常的、未访问的链接 -
a:visited
- 用户访问过的链接 -
a:hover
- 当用户将鼠标悬停在它上面时的链接 -
a:active
- 点击链接的那一刻
/* unvisited link */
a:link { color: red; }
/* visited link */
a:visited { color: green; }
/* mouse over link */
a:hover { color: hotpink; }
/* selected link */
a:active { color: blue; }
![](https://img.haomeiwen.com/i21922576/8200af42c19b6e3a.png)
为多个链接状态设置样式时,有一些顺序规则:
-
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;
}
![](https://img.haomeiwen.com/i21922576/800c2aa9da87986d.png)
背景颜色
background-color
属性可用于指定链接的背景色:
a:link {
background-color: yellow;
}
a:visited {
background-color: cyan;
}
a:hover {
background-color: lightgreen;
}
a:active {
background-color: hotpink;
}
![](https://img.haomeiwen.com/i21922576/b797fba19e166c36.png)
链接按钮
此示例演示了一个更高级的示例,其中我们组合了多个 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;
}
![](https://img.haomeiwen.com/i21922576/11df2f706197051e.png)
其他示例
a.one:link {color: #ff0000;}
a.one:visited {color: #0000ff;}
a.one:hover {color: #ffcc00;}
a.two:link {color: #ff0000;}
a.two:visited {color: #0000ff;}
a.two:hover {font-size: 150%;}
a.three:link {color: #ff0000;}
a.three:visited {color: #0000ff;}
a.three:hover {background: #66ff66;}
a.four:link {color: #ff0000;}
a.four:visited {color: #0000ff;}
a.four:hover {font-family: monospace;}
a.five:link {color: #ff0000; text-decoration: none;}
a.five:visited {color: #0000ff; text-decoration: none;}
a.five:hover {text-decoration: underline;}
![](https://img.haomeiwen.com/i21922576/ae03724c197d17d1.png)
如何创建串流框/按钮的另一个示例(这个挺好看的):
a:link, a:visited {
background-color: white;
color: black;
border: 2px solid green;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
}
a:hover, a:active {
background-color: green;
color: white;
}
![](https://img.haomeiwen.com/i21922576/3b5d99cb7132d1d8.png)
此示例演示不同类型的游标(对于链接很有用):
<span style="cursor: auto">auto</span><br>
<span style="cursor: crosshair">crosshair</span><br>
<span style="cursor: default">default</span><br>
<span style="cursor: e-resize">e-resize</span><br>
<span style="cursor: help">help</span><br>
<span style="cursor: move">move</span><br>
<span style="cursor: n-resize">n-resize</span><br>
<span style="cursor: ne-resize">ne-resize</span><br>
<span style="cursor: nw-resize">nw-resize</span><br>
<span style="cursor: pointer">pointer</span><br>
<span style="cursor: progress">progress</span><br>
<span style="cursor: s-resize">s-resize</span><br>
<span style="cursor: se-resize">se-resize</span><br>
<span style="cursor: sw-resize">sw-resize</span><br>
<span style="cursor: text">text</span><br>
<span style="cursor: w-resize">w-resize</span><br>
<span style="cursor: wait">wait</span>
![](https://img.haomeiwen.com/i21922576/38d8e25d82ec5820.png)
网友评论