CSS Link
设置链接样式
链接可以使用任何 CSS 属性(例如 color
、font-family
、background
等)来设置样式。
实例
a { color: hotpink; }
此外,可以根据链接处于什么状态来设置链接的不同样式。
四种链接状态分别是:
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; }
مثالهای بیشتر
- استایلهای مختلف برای لینکهای اضافی
- این مثال نشان میدهد که چگونه میتوان به لینکهای اضافی استایل اضافه کرد.
- پیشرفته - ایجاد دکمه لینک با حاشیه
- این یک مثال دیگر از ایجاد جعبه لینک/دکمه است.
- تغییر موس
- cursor属性 specifies the type of cursor to display. This example demonstrates different types of cursors (useful for links).