CSS 链接
使用 CSS,链接可以以多种不同的方式进行样式化。
文本链接 文本链接 链接按钮 链接按钮
链接样式
链接可以使用任何 CSS 属性进行样式化(例如 color
、font-family
、background
等)。
此外,链接可以根据它们所处的状态进行不同的样式化。
四个链接状态是
a:link
- 一个普通、未访问的链接a:visited
- 用户已访问过的链接a:hover
- 用户将鼠标悬停在其上方的链接a:active
- 用户点击时的链接
示例
/* 未访问的链接 */
a:link {
color: red;
}
/* 已访问的链接 */
a:visited {
color: green;
}
/* 鼠标悬停在链接上 */
a:hover {
color: hotpink;
}
/* 选中的链接 */
a:active {
color: blue;
}
自己动手试一试 »
设置多个链接状态的样式时,存在一些顺序规则
- 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;
}
自己动手试一试 »
背景颜色
background-color
属性可用于为链接指定背景色
示例
a:link {
background-color: yellow;
}
a:visited {
background-color: cyan;
}
a:hover {
background-color: lightgreen;
}
a:active {
background-color: hotpink;
}
自己动手试一试 »
链接按钮
此示例展示了一个更高级的示例,我们将结合多个 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;
}
自己动手试一试 »
更多示例
示例
此示例演示了如何为超链接添加其他样式
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;}
自己动手试一试 »
示例
创建链接框/按钮的另一个示例
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;
}
自己动手试一试 »
示例
此示例演示了不同类型的光标(可用于链接)
<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>
自己动手试一试 »