链接样式可以使用多种 CSS 属性进行设置(例如 color
、font-family
、 background
等)。
链接状态
根据链接所处的状态,可以采用不同的样式。例如:
- a:link- 正常的、未访问过的链接
- a:visited- 用户访问过的链接
- a:hover- 当用户鼠标悬停在链接上时
- a:active- 点击链接
在为多个链接状态设置样式时,有一些顺序规则:
- a:hover 必须位于 a:link 和 a:visited 之后
- a:active 必须位于 a:hover 之后
参考代码:
/* 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;
}
文本修饰
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: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;
}
综合应用
/*鼠标移过,文字变色*/
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;
}
鼠标光标
还可改变鼠标移至链接上时的光标样式。例如:
<span style="cursor: text">text</span><br>
- default:默认光标,通常是一个箭头。
- none:没有光标。
- auto:浏览器根据当前情况自动确定鼠标光标类型。
- crosshair:十字线光标。
- pointer 或
hand
:手形光标,通常用于链接上。 - wait:表示程序忙,用户需要等待的光标,通常是沙漏或手表的形状。
- help:带有问号标记的箭头,用于标示有帮助信息存在。
- progress:带有沙漏标记的箭头光标,用于标示一个进程正在后台运行。
- move:十字箭头光标,用于标示对象可被移动。
- e-resize、n-resize、ne-resize、nw-resize、s-resize、se-resize、sw-resize、w-resize:用于标示对象可被改变尺寸方向的箭头光标。
- text:用于标示可编辑的水平文本的光标,通常是大写字母 I 的形状。
- vertical-text:用于标示可编辑的垂直文本的光标,通常是大写字母 I 旋转 90 度的形状。
- no-drop:带有一个被斜线贯穿的圆圈的手形光标,用于标示被拖起的对象不允许在光标的当前位置被放下。
- not-allowed:禁止标记(一个被斜线贯穿的圆圈)光标,用于标示请求的操作不允许被执行。
- url (url):用户自定义光标,使用绝对或相对 url 地址指定光标文件(扩展名为. cur 或者. ani)。
Last updated on