有许多CSS样例可以用于样式链接的颜色。
html5boilerplate.com提供了以下用于链接的CSS代码:
a { color: #00e; }
a:visited { color: #551a8b; }
a:hover { color: #06e; }
这种方法对大多数情况是否足够好用?
或者还有更好的CSS代码可以为链接设置样式颜色吗?
有许多CSS样例可以用于样式链接的颜色。
html5boilerplate.com提供了以下用于链接的CSS代码:
a { color: #00e; }
a:visited { color: #551a8b; }
a:hover { color: #06e; }
这种方法对大多数情况是否足够好用?
或者还有更好的CSS代码可以为链接设置样式颜色吗?
那绝对足以应付绝大多数情况。
只需记住链接样式的正确顺序:
a:link { color: #c00 } /* unvisited links */
a:visited { color: #0c0 } /* visited links */
a:hover, a:focus { color: #00c } /* user hovers, or focus */
a:active { color: #ccc } /* active links */
outline
可能对你来说看起来不太美观,但这是一个非常重要的辅助功能。如果你去掉它,请注意提供其他的方法来正确区分当前元素(更大/更粗的字体,高对比度背景等)。
a
和a:link
选择器之间的区别:https://dev59.com/L3E85IYBdhLWcg3wvGER - Adriano我经常重置可能在不同浏览器之间不同的设置。
我还喜欢通过添加一个图片(类似于维基百科中的图片)来不同地标记外部网站的链接。
a,
a:link,
a:active,
a:visited,
a:hover {
color: #d30;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* Links to external websites */
a.external:before {
content: url(./pics/external.png);
}
永远不要删除那个轮廓线,或者至少只在a:active时删除它。如果你将它应用到所有锚点上,那么键盘导航所使用的a:focus也会被删除。过度依赖悬停状态非常糟糕,因为触摸屏幕上没有悬停功能。
我喜欢让所有链接与其他内容区分明显。这是我的个人偏好:
2016版本
/* The order is important! Do not use fixed values like px! Always check contrast between text and background for accessibility! */
a { border-bottom: thin solid;
color: rgb(0,0,192);
font-weight: bolder;
text-decoration: none;
}
a:visited { color: rgb(160,0,160); }
a:active { color: rgb(192,0,0); }
a:active, a:focus, a:hover { border-bottom-width: medium; }
2015年版本
a { border-bottom: thin solid;
color: rgb(0,0,192);
font-weight: 700;
text-decoration: none;
}
a:visited { color: rgb(128,0,128); }
a:active { color: rgb(192,0,0); } /* :active MUST come after :visited */
a:active, a:focus, a:hover { border-bottom-width: medium; }
2014年版本
a { border-bottom: 1px solid;
color: rgb(0,0,166);
font-weight: 700;
text-decoration: none;
}
a:visited { color: rgb(122,0,122); }
a:active { color: rgb(166,0,0); } /* :active MUST come after :visited */
a:active, a:focus, a:hover { border-bottom: 3px solid; }
2013版本
a { color: rgb(0,0,166);
font-weight: 700;
border-bottom: 1px dotted;
text-decoration: none;
}
a:visited { color: rgb(122,0,122); }
a:hover, a:focus, a:active { border-bottom: 2px solid; }
a:focus, a:active { color: rgb(166,0,0); }
我发现在编程中加入以下代码总是很好的选择:
a { outline: none; }
因为有些浏览器在点击链接时会添加令人讨厌的轮廓线。